【发布时间】:2016-03-15 11:18:31
【问题描述】:
我看到了这个计算误差的欧拉方法:
function [t,le,ge] = euler_errors(h)
f=@(u) u*(2-u); % this is the function for the IVP
t0=0;
tn=1;
t=t0:h:tn;%we want to find the errors along this solutions
%here is the exact solution of the IVP
u_exact=(0.2*exp(2*t))./(2+0.1*(exp(2*t)+1)); %the with initial value u(0)=0.1
n=length(t);
u_e=zeros(1,n);
u_g=zeros(1,n);
i=1;
u_e(i)=0.1;
u_g(i)=0.1;
%u_e and u_g are both values given by Euler method
%u_e is for the local error
%u_g is for the global error
while (i<n)
u_e(i+1)=u_e(i)+h*f(u_e(i));
u_g(i+1)=u_g(i)+h*f(u_exact(i));
i=i+1;
end;
%le1 is the local error
%ge1 is the global error
le=abs(u_e-u_exact);
ge=abs(u_g-u_exact);
end
我尝试将方法转换为 Heun 的方法 - 这是我的尝试:
function [t,le,ge] = heun_errors(h)
f=@(u) u*(2-u); % this is the function for the IVP
t0=0;
tn=1;
t=t0:h:tn;%we want to find the errors along this solutions
%here is the exact solution of the IVP
u_exact=(0.2*exp(2*t))./(2+0.1*(exp(2*t)+1)); %the with initial value u(0)=0.1
n=length(t);
u_e=zeros(1,n);
u_g=zeros(1,n);
i=1;
u_e(i)=0.1;
u_g(i)=0.1;
%u_e and u_g are both values given by Euler method
%u_e is for the local error
%u_g is for the global error
while (i<n)
u_e1(i+1)=u_e(i)+h*f(u_e(i));
u_e(i+1)=u_e(i)+(h/2)*(f(u_e(i))+f(u_e1(i+1)));
u_g1(i+1)=u_g(i)+h*f(u_exact(i));
u_g(i+1)=u_g(i)+(h/2)*(f(u_exact(i))+f(u_g1(i+1)));
i=i+1;
end;
%le1 is the local error
%ge1 is the global error
le=abs(u_e-u_exact);
ge=abs(u_g-u_exact);
end
但现在错误实际上增加了。谁能告诉我我做错了什么以及如何解决?
【问题讨论】:
-
据我所知,您的代码是正确的。也许巧合的是,特定的 ODE 更适合 Euler 的方法。你如何比较错误?最好的检查方法是在减少
h时计算每种方法的收敛性。 Euler 的方法应该是线性收敛的,但 Heun 的方法应该改进得更快(二次方)。 -
heun的方法是不是因为计算的部分比较多,截断误差比较大?两个步骤而不是欧拉方法中的一个?
-
不,我认为它应该有较小的截断误差但较大的舍入误差。
标签: matlab numerical-methods differential-equations runge-kutta