n_t =
Improved Euler's method compared to 4th order Runge - Kutta's method
Mostrar comentarios más antiguos
Is the improved Euler's method as good as the 4th order Runge - Kutta's method ? Or is it the step h that makes it almost not distinguishable ?
%--- Exercise 1 - A
%--- Solving the diff. eqn. N'(t) = N(t) - c*N^2(t) with c = 0.5 using
%--- Euler's method, Runge - Kutta method, and predictor-corrector method, and comparing in graph.
%--- I have four initial values for N0.
clc, clear all, close all
tmax = 50; % input('Enter tmax = ');
t = [0:tmax];
c = 0.5;
h = 0.1;
f = @(t,N) N - c*N.^2;
N0 = [0.1 0.5 1.0 2.0];
L = length(t)-1;
N = zeros(1,L);
for i = 1:length(N0)
figure(1)
subplot(2,2,i), hold on
title({'N'' = N - 0.5N^2',sprintf('N_0 = %.1f', N0(i))}),xlabel('t'), ylabel('N(t)'), hold on
%---------- Euler's method ----------
for j = 1:L
EulerN(1) = N0(i);
EulerN(1,j+1) = EulerN(j) + h*f(':',EulerN(j));
end
plot(t,EulerN), hold on
%---------- Improved Euler's method ----------
for j = 1:L
ImpN(1) = N0(i);
ImpN(1,j+1) = ImpN(j) + 0.5*h*( f ( t(j), ImpN(j) ) + f ( t(j+1), ImpN(j) + h * f ( t(j), ImpN(j) ) ) );
end
plot(t,ImpN,'r.'), hold on
%---------- Runge - Kutta method ----------
for j = 1:tmax
RKN(1) = N0(i);
K1 = f(t(j),RKN(j));
K2 = f(t(j) + h*0.5, RKN(j) + h*K1*0.5);
K3 = f(t(j) + h*0.5, RKN(j) + h*K2*0.5);
K4 = f(t(j) + h, RKN(j) + h*K3);
RKN(j+1) = RKN(j) + h*((K1 + K4)/6 + (K2 + K3)/3);
end
plot(t,RKN,'k-.')
legend({'Simple Euler','Improved Euler','Runge - Kutta'},'Location','Southeast')
end
1 comentario
If the equation is non-stiff and you use fixed time stepping, usually the order of the method indicates which one is "best".
If the solutions are almost identical, you are correct: probably the stepsize is already small enough that the solutions lie one upon the other.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Calculus en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


