Sunteți pe pagina 1din 9

Mtodo de Runge-Kutta

Programa en MatLab de Runge-Kutta de orden dos.


function f
fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA
DE ORDEN 4\n')
f=input('\n Ingrese la ecuacion diferencial dy/dx=\n','s');
x0=input('\n Ingrese el primer punto x0:\n');
x1=input('\n Ingrese el segundo punto x1:\n');
y0=input('\n Ingrese la condicion inicial y(x0):\n');
n=input('\n Ingrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
fprintf('\n''it x0 y(x1)');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
y=y0;
k1=h*eval(f);
x=xs(i+1);
y=y0+k1;
k2=h*eval(f);
y0=y0+(k1+k2)/2;
fprintf('\n%2.0f%10.6f%10.6f\n',it,x0,y0);
end
fprintf('\n El punto aproximado y(x1) es = %8.6f\n',y0);

Programa de Runge-Kutta de orden cuatro.


function f
fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA
DE ORDEN 4\n')
f=input('\n Ingrese la ecuacion diferencial\n','s');
x0=input('\n Ingrese el primer punto x0:\n');
x1=input('\n Ingrese el segundo punto x1:\n');
y0=input('\n Ingrese la condicion inicial y(x0):\n');
n=input('\n Ingrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
fprintf('\n''it x0 y(x1)');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
y=y0;
k1=h*eval(f);
x=x0+h/2;
y=y0+k1/2;
k2=h*eval(f);
x=x0+h/2;
y=y0+k2/2;
k3=h*eval(f);
x=x0+h;
y=y0+k3;
k4=h*eval(f);
y0=y0+(k1+2*k2+2*k3+k4)/6;
fprintf('\n%2.0f%10.6f%10.6f\n',it,x0,y0);
end
fprintf('\n El punto aproximado y(x1) es = %8.6f\n',y0);

Solucion
2. Respuesta

>> runge2

RESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE ORDEN


4

Ingrese la ecuacion diferencial dy/dx=


4*exp(0.8*x)-0.5*y

Ingrese el primer punto x0:


0

Ingrese el segundo punto x1:


4

Ingrese la condicion inicial y(x0):


2

Ingrese el numero de pasos n:


4

'it x0 y(x1)
0 0.000000 6.701082

1 1.000000 16.319782

2 2.000000 37.199249

3 3.000000 83.337767

El punto aproximado y(x1) es = 83.337767

respuesta
>> runge4

RESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE ORDEN


4

Ingrese la ecuacion diferencial


-2*x^3+12*x^2-20*x+8.5

Ingrese el primer punto x0:


0

Ingrese el segundo punto x1:


0.5

Ingrese la condicion inicial y(x0):


1

Ingrese el nmero de pasos n:


5

'it x0 y(x1)
0 0.000000 1.753950

1 0.100000 2.331200

2 0.200000 2.753950
3 0.300000 3.043200

4 0.400000 3.218750

El punto aproximado y(x1) es = 3.218750

Esto se crea en otra pestaa con el nombre de f1

function ye=f1(x,y)
ye=x*y+1;

Esto tambin se crea en una diferente pestaa a la de arriba:

clear x,y;
x0=0;
y0=21;
xf=10;
n=20;
h=(xf-x0)/n;
i=1;
x(i)=x0;
y(i)=y0;
while i<=n
k1=h*f1(x0,y0);
k2=h*f1(x0+h/2,y0+k1/2);
k3=h*f1(x0+h/2,y0+k2/2);
k4=h*f1(x0+h,y0+k3);
km=(k1+2*k2+2*k3+k4)/6;
y1=y0+km;
x1=x0+h;
i=i+1;
x(i)=x1;
y(i)=y1;
x0=x1;
y0=y1;
disp([y1])
end
plot(x,y)
grid on
%disp([x1])
%disp([y1])
title('Mtodo de Runge Kutta de cuarto orden')

S-ar putea să vă placă și