runge-kutta ode45

20 visualizaciones (últimos 30 días)
Alvaro Mª Zumalacarregui Delgado
Alvaro Mª Zumalacarregui Delgado el 1 de Mzo. de 2021
Respondida: Meysam Mahooti el 5 de Mayo de 2021
I want to solve a two EDO system with the runge kutta method, I wrote this code but i don't get anything, someone know why this is wrong? if i use command ode45 is better
function [] = runge_kutta_sis(f,g,x0,y0,a,b,h)
t = a:h:b; n = length (t);
x = (x0); y = (y0);
for i=1:n-1
k1 = h*f(x(i),y(i),t(i));
l1 = h*g(x(i),y(i),t(i));
k2 = h*f(x(i)+k1/2,y(i)+l1/2,t(i)+h/2);
l2 = h*g(x(i)+k1/2,y(i)+l1/2,t(i)+h/2);
k3 = h*f(x(i)+k2/2,y(i)+l2/2,t(i)+h/2);
l3 = h*g(x(i)+k2/2,y(i)+l2/2,t(i)+h/2);
k4 = h*f(x(i)+k3,y(i)+l3,t(i)+h);
l4 = h*g(x(i)+k3,y(i)+l3,t(i)+h);
x (i+1) = x(i) + (1/6)*(k1+2*k2+2*k3+k4);
y (i+1) = y(i) + (1/6)*(l1+2*l2+2*l3+l4);
end
plot (t,x,t,y)
I write this in the command window but I can't see any figure
>> f = @(x,y,t) 10-0.1*x*y;
>> g = @(x,y,t) 20-0.2*x*y;
>> runge_kutta_sis(f,g,500,600,0,20,0.5)

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 1 de Mzo. de 2021
Minor modification: Function Code (Save in the same working directory, filename runge_kutta_sis.m)
function runge_kutta_sis(f,g,x0,y0,a,b,h)
t=a:h:b;
n=length(t);
x=zeros(1,length(n));
y=zeros(1,length(n));
x(1)=x0;
y(1)=y0;
for i=1:n-1
k1 = h*f(x(i),y(i),t(i));
l1 = h*g(x(i),y(i),t(i));
k2 = h*f(x(i)+k1/2,y(i)+l1/2,t(i)+h/2);
l2 = h*g(x(i)+k1/2,y(i)+l1/2,t(i)+h/2);
k3 = h*f(x(i)+k2/2,y(i)+l2/2,t(i)+h/2);
l3 = h*g(x(i)+k2/2,y(i)+l2/2,t(i)+h/2);
k4 = h*f(x(i)+k3,y(i)+l3,t(i)+h);
l4 = h*g(x(i)+k3,y(i)+l3,t(i)+h);
x(i+1)=x(i)+(1/6)*(k1+2*k2+2*k3+k4);
y(i+1)=y(i)+(1/6)*(l1+2*l2+2*l3+l4);
end
plot(t,x,t,y);
grid on;
Main Script:
f = @(x,y,t) 10-0.1*x*y;
g = @(x,y,t) 20-0.2*x*y;
runge_kutta_sis(f,g,500,600,0,20,0.5)
Output:
Hope it Helps!
  2 comentarios
Alvaro Mª Zumalacarregui Delgado
Alvaro Mª Zumalacarregui Delgado el 1 de Mzo. de 2021
it helps thanks! if I want to do it with the command ode45 how can I write it?

Iniciar sesión para comentar.

Más respuestas (1)

Meysam Mahooti
Meysam Mahooti el 5 de Mayo de 2021
https://www.mathworks.com/matlabcentral/fileexchange/61130-runge-kutta-fehlberg-rkf78?s_tid=srchtitle

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by