RK2 and modified euler's method

11 vues (au cours des 30 derniers jours)
Ammar Alsenani
Ammar Alsenani le 4 Avr 2019
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.

Réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by