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