Professional Documents
Culture Documents
Matemticas de Especialidad
Ingeniera Elctrica
Integracin de
Ecuaciones Diferenciales Ordinarias
ODE
Jos Luis de la Fuente OConnor
jldelafuente@etsii.upm.es
joseluis.delafuente@upm.es
Clase_integracin ODE_2014.pdf
1/62
ndice
Introduccin
Integracin numrica de EDOs (ODEs)
Mtodo
de Euler
Mejoras en el mtodo de Euler
Mtodo de Taylor
Mtodos de Runge-Kutta
Mtodos implcitos y problemas stiff
2/62
Introduccin
4
dy
D y 0 D f .t; y/;
dt
3/62
y/
4/62
< y D f .t; y/
y.a/ D y
: t 2 a; b:
5/62
y = 5*y*(1-y)
1,2
1,2
0,8
0,8
0,6
0,6
y
y
0,4
0,4
0,2
0,2
-0,2
-0,2
-0,4
-0,4
0
0,2
0,4
0,6
0,8
1,2
t
1,4
1,6
1,8
2,2
0,2
0,4
0,6
0,8
1,2
1,4
1,6
1,8
2,2
7/62
8/62
9/62
10/62
1
D yi C f .ti ; yi / C f .ti C1; yiC1/ h:
2
11/62
Mtodo de Euler
4
Desde el punto de arranque hay que moverse una pequea distancia, llegndose
a un nuevo punto .t1; y1/ en el que se revala la pendiente, luego hay que
moverse con esa nueva pendiente otro poco, etc.
12/62
< y D ty C t
y.0/ D y
: t 2 0; 1;
1,8
1,6
1,4
1,2
0,8
0,6
0,4
0,2
0
0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
t
13/62
1,8
1,6
1,4
1,2
0,8
0,6
0,4
0,2
0
0
0,1
0,2
0,3
0,4
0,5
0,6
0,7
0,8
0,9
t
14/62
i,
HUURU
YDORUH[DFWR
YDORUDSUR[LPDGR
yi
ti
4
ti+1
for i=1:length(t)-1
k1 = t(i)*yx(i)+t(i)^3;
yx(i+1) = yx(i)+h*k1;
end
for i=1:length(t1)-1
k11 = t1(i)*yx1(i)+t1(i)^3;
yx1(i+1) = yx1(i)+h1*k11;
end
y=3*exp(t.^2/2)-t.^2-2;
% Solucin exacta
% Grficos
plot(t,yx,b--,t,y,r-,t1,yx1,k--);
legend(Aproximacin h=0,1,Exacto y,aproximacin h=0,01);
title(Paso de Euler, h=0,1 y h=0,01);
xlabel(t);
ylabel(y*(t), y(t)); axis([0 1 0.95 2]);
% Tabla
for i=1:length(t)
disp(sprintf(t=%5.3f,
end
y(t)=%6.4f,
y*(t)=%6.4f,t(i),y(i),yx(i)));
16/62
y*(t)=1.0000
y*(t)=1.0000
y*(t)=1.0101
y*(t)=1.0311
y*(t)=1.0647
y*(t)=1.1137
y*(t)=1.1819
y*(t)=1.2744
y*(t)=1.3979
y*(t)=1.5610
y*(t)=1.7744
1.8
1.7
1.6
y*(t), y(t)
>> Euler_2_S
t=0.000, y(t)=1.0000,
t=0.100, y(t)=1.0050,
t=0.200, y(t)=1.0206,
t=0.300, y(t)=1.0481,
t=0.400, y(t)=1.0899,
t=0.500, y(t)=1.1494,
t=0.600, y(t)=1.2317,
t=0.700, y(t)=1.3429,
t=0.800, y(t)=1.4914,
t=0.900, y(t)=1.6879,
t=1.000, y(t)=1.9462,
>>
Aproximacin h=0,1
Exacto y
aproximacin h=0,01
1.5
1.4
1.3
1.2
1.1
1
0
0.1
0.2
0.3
0.4
0.5
t
0.6
0.7
0.8
0.9
17/62
h
D yi C f .ti ; yi / C f .tiC1; yi C1/ :
2
j
j 1
hasta que se cumpla una tolerancia de yi C1 yi C1 .
Ejemplo
4
19/62
% Derivadas
% Euler
% Heun sin iteracin
20/62
El resultado es este.
>> [t,yx,y,y_h,y_hi]=Heun_4(@dpdt_Heun_c,[0 4],2,1);
>> [t,yx,y,y_h,y_hi]
ans =
0
2.0000
2.0000
2.0000
2.0000
1.0000
6.1946
5.0000
6.7011
6.3609
2.0000
14.8439
11.4022
16.3198
15.3022
3.0000
33.6772
25.5132
37.1992
34.7433
4.0000
75.3390
56.8493
83.3378
77.7351
Solucin de y'=4e(0.8t)-0.5y
90
Exacto
Euler
Heun
Heun con it.
80
70
60
50
y
40
30
20
10
0.5
1.5
2
t
2.5
3.5
21/62
yi C1 D yi C hf ti C1=2; yi C1=2 :
4
22/62
Mtodo de Taylor
4
Si f es derivable,
y 0 D f .t; y/
y 00 D f 0 D f t C fy y 0 D f t C fy f
y 000 D f 00 D f t t C 2f ty f C fyy f 2 C f t fy C fy2 f
:::
23/62
24/62
Mtodos de Runge-Kutta
4
Son una familia de mtodos desarrollados a partir del trabajo de los alemanes
Carl David Tolm Runge (1856-1927) y Martin Wilhelm Kutta (1867-1944).
25/62
1;n
1 kn 1 h :
26/62
Son
yi C1 D yi C a1k1 Ca2k2 h;
donde
k1 D f .ti ; yi / fi
k2 D f .ti Cp1h; yi Cq11k1h/:
yiC1 D yi C hy .ti / C
h2 00
y .ti /
2
D yi C hfi C
h2
2
f ti C fyi fi :
27/62
Comparando las dos expresiones se llega a estas tres ecuaciones con cuatro
incgnitas:
1
1
y a2q11 D :
a1 C a2 D 1;
a2p1 D
2
2
28/62
1
2
yi C1
k1
k2
k3
y1 C h6 k1 C 4k2 C k3
f .ti ; yi /
f ti C 12 h; yi C 21 hk1
f .ti C h; yi k1h C 2k2h/ :
D f .ti C h; yi C k3h/ :
30/62
odo
de integracin de EDOs ms utilizado.
4
Interpretacin geomtrica
k2
k1
yi
k3
yi+1
k4
k6
D f ti C h; yi
3
hk1
7
C 27 hk2 C 12
hk3
7
12
hk4
7
C 87 hk5 :
Para aumentar en una unidad el orden del Runge-Kutta clsico de orden cuatro
hacen falta dos evaluaciones de funcin adicionales.
Ejemplo de recapitulacin
4
Vamos a integrar la ODE y 0 D y 1t 2t , empleando Euler, Heun,
Runge-Kutta y la rutina de Matlab ode45 muy habitual para estos
problemas, que usa Runge-Kutta con una precisin alta.
33/62
% intervalos de ejes
plot(T4,Y4,-k);
function yprima=ec04(t,y)
% solucin exacta desconocida
if t==0, yprima=1; else yprima=y*(-2*t+1/t);
end
end
function [T,Y]=heun(f,tspan,y0,h)
% Mtodo de Heun
n=(tspan(2)-tspan(1))/h+1;
T=tspan(1)*zeros(n,1); Y=y0*zeros(n,length(y0));
y=y0; t=tspan(1);
for i=2:n
t1=t+h;
yp=f(t,y);
% predictor con Euler explcito
y1=y+h*yp;
y1n=y+h*(yp+f(t1,y1))/2; % corrector con la regla trapezoidal
T(i,:)=t1; Y(i,:)=y1n;
% tiempo valor de la funcin
t=t1; y=y1;
% se actualizan t e y
end, end
function [T,Y]=rk4(f,tspan,y0,h)
% Runge-Kutta orden 4
n=fix((tspan(2)-tspan(1))/h+1);
T=tspan(1)*zeros(n,1); Y=y0*zeros(n,length(y0));
y=y0; t=tspan(1);
for i=2:n
t1=t+h;
k1=h*f(t,y);
% clculo de los coeficientes k
k2=h*f(t+h/2,y+k1/2);
k3=h*f(t+h/2,y+k2/2);
k4=h*f(t+h,y+k3);
y1=y+(k1+2*k2+2*k3+k4)/6;
T(i,:)=t1; Y(i,:)=y1;
% tiempo valor de la funcin
t=t1; y=y1;
% se actualizan t e y
end, end
end
34/62
Este es el resultado:
Solucin de y'=y(1/t-2t)
Euler
Heun
rk4
ode45
0.5
0.4
0.3
0.2
0.1
0.2
0.4
0.6
0.8
1
tiempo
1.2
1.4
1.6
1.8
35/62
Mtodo Runge-Kutta-Fehlberg
4
3
9
k3 D hf ti C 38 h; y1 C 32
k1 C 32
k2
12
k4 D hf ti C 32
h; y1 C 1932
k
2197 1
k5 D hf ti C h; y1 C 439
k
216 1
k6 D hf ti C 12 h; y1
8
k
27 1
7200
k
2197 2
7296
C 2197
k3
8k2 C 3680
k
513 3
845
k
4104 4
3544
k
2565 3
C 1859
k
4104 4
C 2k2
yi C1 D yi C
25
k
216 1
2197
C 1408
k
C
k
3
2565
4104 4
zi C1 D yi C
16
k
135 1
6656
28561
C 12825
k3 C 56430
k4
1
k
5 5
11
k
40 5
9
k
50 5
2
C 55
k6 :
37/62
1
128
2197
1
2
k1 4275
k3 75240
k4 C 50
k5 C 55
k6.
ei C1 D jzi C1 yi C1j D h 360
T oljyi j
h D 0;8
ei
15
hi
1
h; y1
5
k3 D hf ti C
3
h; y1
10
k4 D hf ti C
4
h; y1
5
k5 D hf ti C
8
h; y1
9
1
k
5 1
3
k
40 1
44
k
45 1
19372
k
6561 1
k6 D hf ti C h; y1 C 9017
k
3168 1
zi C1 D yi C
35
k
384 1
9
k
40 2
56
k
15 2
32
k
9 3
25360
k
2187 2
355
k
33 2
500
125
C 1113
k3 C 192
k4
64448
k
6561 3
212
k
729 4
49
C 46732
k C 176
k4
5247 3
2187
11
k
C
k
6784 5
84 6
5103
k
18656 5
k7 D hf .ti C h; ziC1/
yi C1 D yi C
5179
k
57600 1
7571
C 16695
k3 C 393
k
640 4
92097
k
339200 5
187
C 2100
k6 C 14 k7 :
39/62
17253
k
339200 5
22
k
525 6
1
k .
40 7
Si se cumplen las tolerancias, ziC1 ser el nuevo punto y k1 ser k7, por lo que
no se desperdician clculos.
40/62
Los mtodos que hemos considerado hasta ahora son explcitos: una frmula
determina explcitamente una nueva aproximacin yi C1 a partir de datos de h,
t i y yi .
Hay un tipo de problemas, denominados stiff rgidos, para los que los mtodos
explcitos evolucionan mal hacia la solucin.
Los problemas stiff son aquellos que convergen relativamente rpido hacia una
solucin estable, pero que tienen componentes transitorios importantes con un
decaimiento mucho ms rpido.
Suelen modelizar procesos fsicos con varios componentes con escalas de tiempo
muy dispares. El intervalo de tiempo con el que se les estudia puede ser bueno
para uno de ellos pero no para todos.
41/62
y0 D y.0/;
C t 2:
42/62
1.4
0.9
1.2
0.8
1
0.7
0.6
0.8
0.5
0.6
0.4
0.3
0.4
0.2
0.2
0.1
0.1
0.2
0.3
0.4
0.5
t
0.6
0.7
0.8
0.9
Figure 2.2.1: Solution of = ; ( ; 2)+2 (solid) illustrating inner (dashed) and outer
(dash-dot) solutions.
y
0.1
0.2
0.3
0.4
0.5
t
0.6
0.7
0.8
0.9
(solid)
In order to solve sti problems we'll need a numerical method with a weaker stability
43/62
La informacin que se considera en cada paso tiene en cuenta los dos lados del
intervalo ti ; ti C h.
yi C h t 2 C 2t
:
D
1 C h
46/62
1.2
0.8
0.6
y
0.4
0.2
-0.2
-0.4
0.2
0.4
0.6
0.8
1.2
1.4
Tiempo
47/62
Mtodos multipaso
4
Los mtodos que hemos analizado hasta ahora son monopaso, o de paso
sencillo. El valor de yiC1 se calcula a partir de la informacin de un nico punto.
48/62
Los mtodos multipaso necesitan la ayuda de otros para dar los primeros pasos
y no se adaptan bien a discontinuidades en la solucin: discontinuidades en las
fuerzas aplicadas, impactos, enlaces que aparecen o desaparecen, etc.
m
i
j D1
j yiC1
Ch
m
i
j f tiC1 j ; yiC1
j D0
50/62
h
D yi C 3yi0
2
yi0 1
;
51/62
52/62
Error
2 yi C1 D yi C
h
2
3 yi C1 D yi C
h
12
23fi
16fi
C 5fi
4 yi C1 D yi C
h
24
55fi
59fi
C 37fi
5 yi C1 D yi C
h
720
3fi
fi
1901fi
5 3 00
h f ./
12
2744fi
3 4 000
h f ./
8
251 5 .4/
h f ./
720
9fi
C 2616fi
1274fi
C 251fi
95 6 .5/
h f ./
288
Error
2 yi C1 D yi C
h
2
3 yi C1 D yi C
h
12
5fi C1 C 8fi
4 yi C1 D yi C
h
24
9fi C1 C 19fi
5 yi C1 D yi C
h
720
3fi C1
fi
1 3 00
h f ./
12
fi
5fi
251fiC1 C 646fi
1 4 000
h f ./
24
C fi
264fi
19 5 .4/
h f ./
720
C 106fi
19fi
27 6 .5/
h f ./
1440
53/62
Mtodos predictor-corrector
4
54/62
h
D yi C
55fi
24
Df
59fi
C 37fi
ti C1; yiC1
9fi
ti C1; yiC1
h
9fiC1 C 19fi
24
5fi
1 C fi
2 :
El mtodo de Milne-Simpson:
1. Utiliza como predictor
yiC1 D yi
3C
4h
2fi
3
fi
1 C 2fi
2 :
2. Como corrector
yi C1 D yi
1C
h
fiC1 C 4fi C fi 1 :
3
9yi
yi
2 C 3h fiC1 C 2fi
2fi
:
56/62
function [t,y]=Adams_Bas_Moul(inter,ic,n)
% Mtodo predictor-corrector de orden 2 de Adams-Bashforth-Moulton
h=(inter(2)-inter(1))/n; y(1)=ic; t(1)=inter(1); s=2;
for i=1:s-1
% Trapezoide de un paso para
t(i+1)=t(i)+h;
%
inicializacin
y(i+1)=trapstep(t(i),y(i),h);
function z=ab2step(t,i,y,f,h) % Paso Adams-Bashforth de orden 2
f(i)=ydot(t(i),y(i));
z=y(i)+h*(3*f(i)-f(i-1))/2;
end
end
for i=s:n
% Mtodo multipaso
f(i)=ydot(t(i),y(i));
function z=am1step(t,i,y,f,h) % Paso Adams-Moulton de orden 2
t(i+1)=t(i)+h;
z=y(i)+h*(f(i+1)+f(i))/2;
y(i+1)=ab2step(t(i),i,y,f,h);
end
f(i+1)=ydot(t(i+1),y(i+1));
y(i+1)=am1step(t(i),i,y,f,h);
function z=ydot(t,y)
% Funcin que se integra
end
z=4*(t-t^3)*y^2;
[T4,Y4]=ode45(@ydot,inter,ic); close all, plot(t,y,T4,Y4) end
xlabel(Tiempo); ylabel(y)
end
legend(Adams-Bash.-Moul., ode45)
function y=trapstep(t,x,h)
% Un paso con trapezoide
z1=ydot(t,x); g=x+h*z1; z2=ydot(t+h,g);
y=x+h*(z1+z2)/2;
end
57/62
0,9
0,8
0,7
0,6
0,5
0,4
0,3
0,2
0,1
0,
0,
0,5
1,
1,5
2,
2,5
Tiempo
58/62
59/62
Al aplicar los mtodos vistos hasta ahora hay que tener en cuenta que la
variable dependiente y, la funcin f y los coeficientes ki , del mtodo de
Runge-Kutta por ejemplo, son vectores.
y 0 D f .t; y/
y iC1 D y i C hf .ti ; y i /:
60/62
1/
y.t0/ D y0
y 0.t0/ D y00
y 00.t0/ D y000
.n
y .n 1/.t0/ D y0
4
1/
1/
se tendr que
y10
y20
y30
D
D
D
:::
yn0 1 D
yn0 D
y2
y3
y4
9
>
>
>
>
>
>
>
>
=
y 0 D f .t; y/;
>
>
>
>
>
yn
>
>
>
f .x; y1; y2; : : : ; yn/ ;
62/62