【问题标题】:Writing a matlab function that implements Euler's method?编写实现欧拉方法的matlab函数?
【发布时间】:2012-12-16 23:49:52
【问题描述】:

我应该编写一个 MATLAB 函数,它采用 y'(t) = a*y(t) +b 形式的一阶常微分方程,初始点 y(t0)=y0 作为输入并计算前 15 个点的解决方案。还绘制前 15 个点的解曲线。 我们要求解的方程是;y'(t) = 4*y(t)+1,初始点y(0)=0。

对于这个函数,我写了下面的代码,但这给了我一个关于 y 的错误。我应该如何正确实现欧拉函数?而且我也无法确定如何绘制解曲线..

function E=euler(f,y)
%Input - f is the function entered as a string 'f'
% - a and b are the left and right endpoints
% - ya is the initial condition y(a)
% - M is the number of steps
%Output - E=[T' Y'] where T is the vector of abscissas and
% Y is the vector of ordinates
h=0.1;
y(0)=0;
for j=0:15
Y(j+1)=Y(j)+h*feval(4*(y(t)+1));
end

【问题讨论】:

标签: matlab ode


【解决方案1】:

补丁:

h = 0.1;
y(1) = 0;
for j = 1:16
    Y(j + 1) = Y(j) + h * feval(4 * (y(t - 1) + 1));
end

好吧,我不确定数学部分,但是 - 索引需要从“1”开始。其他然后例如在 C 中,您不能使用“0”作为索引。

【讨论】:

  • 哦,我看到你正在用 2 开始 for 循环。但是你没有在代码中将 y(0) 初始化为 0,这会产生任何错误吗?
  • 仔细阅读,我初始化“y(1) = 0;” ...我基本上将“1”添加到所有索引。
  • 好的,一个小的修正让它更容易看到。此外,未定义“t”。它来自哪里?
  • 其实t来自这个y’(t) = 4*y(t)+1
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 2017-04-12
  • 1970-01-01
  • 2016-04-06
相关资源
最近更新 更多