1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function [y,t] = RK4(f,tmin,tmax,Nint,y0) % Méthode de Runge Kutta dordre 4
% Nint - nombre de sous intervalles
% tmin - temps t0
% tmax - temps t0 + T
% f est une fonction avec comme arguments t et y : f(t,y(t))
% y0 contient les valeurs des conditions limites
h = (tmax-tmin)/Nint ; % valeur du pas (G)
t = linspace(tmin,tmax,Nint+1) ; % vecteur de t discrétisé t=[tmin,tmax]
n = length(t);
y = zeros(1,n); % on fixe la taille du tableau y
y(1,1) = y0 ;
t(1,1)=0;% y contient les solutions de y(tn)n = 1, ..., Nint + 1
for n = 2:Nint-1
t(n+1)=t(n)+h;
k1=h*f(t(n),y(n));
k2=h*f(t(n)+(h/2),y(n)+(k1/2));
k3=h*f(t(n)+(h/2),y(n)+(k2/2));
k4=h*f(t(n)+h,y(n)+k3);
y(n+1)=y(n)+(k1/6)+(k2/3)+(k3/3)+(k4/6);
end
end |
Partager