RK2 and modified euler's method

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 件)

カテゴリ

ヘルプ センター および File ExchangeGraph and Network Algorithms についてさらに検索

質問済み:

2019 年 4 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by