RK2 and modified euler's method

11 次查看(过去 30 天)
Ammar Alsenani
Ammar Alsenani 2019-4-4
a=1;
b=2;
w0=1;
h=[0.5 0.1 0.05 0.01 0.005 0.001];
f=@(t,w) (w/t)-(w/t)^2;
wnA=zeros(1,6);
RE_mod=zeros(1,6);
wnB=zeros(1,6);
RE_mid=zeros(1,6);
for i=1:6
w=w0;
t=a;
for j=1:(b-a)/h(i)
w=w+h(i)*f(t,w);
t=a+j*h(i);
end
wnA(i)=w;
RE_mod(i)=abs(t/(log(t)+1));
end
for i=1:6
w=w0;
t=a;
tic
for j=1:(b-a)/h(i)
w=w+h(i)*f(t+h/2,w+h/2*f(t,w));
t=a+j*h(i);
time=toc;
end
wnB(i)=w;
RE_mid(i)=abs(t*log(t)+2*t);
end
loglog(h,RE_mid,'-+',h,RE_mod,'-o')
legend('Midpoint=RK2','Modified Euler')
xlabel('h')
ylabel('Relative Error')
%%this is my code but somthing is wrong with it, the graphs are strirtght horizental lines.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by