SOLUCIÓN NUMÉRICA DE - · PDF fileCurva solución pendiente Campo de...

Post on 06-Mar-2018

220 views 5 download

Transcript of SOLUCIÓN NUMÉRICA DE - · PDF fileCurva solución pendiente Campo de...

SOLUCIÓN NUMÉRICA DE

ECUACIONES DIFERENCIALES ORDINARIAS DE PRIMER ORDEN

SOLUCION NUMERICA

Una solución de esta ecuación inicial con CI es una función

0 0: ( , )x x Rϕ ε ε− + →

tal que

0 0'( ) ( , ( )), para ( , )x f x x x x xϕ ϕ ε ε= ∈ − +

PROBLEMA: Hallar una aproximación numérica de la solución de la EDO con CI

Curva solución

pendiente

Campo de pendientes f(x,y)

0 1 2 ,na x x x x b= < < < < =L

Conocemos 0( ) ( )a xϕ ϕ=

Queremos obtener una aproximación de ( ) para b b aϕ >

Consideramos una partición del intervalo

donde lo llamamos tamaño de paso. Observemos que

[ , ]a b

1i it th

n

+ −=

0 , 1, 2,...kt kh t k n= + =

Curva solución

pendiente=

Consideramos la condición inicial

0 0 0 0( , ) ( , ( )) ( , ( ))x y x y x a y a= =

0 0 0 2( ) ( ) '( )( ) ( )y x y x y x x x R x= + − +

El desarrollo de Taylor 1er orden alrededor de

0x

0 0 0( ) ( ) '( )( )y x y x y x x x+ −�

obtenemos la aproximación lineal de ( )y x

cerca de 0x

tomando 1 0x x x h= = +

obtenemos 1 0 0 0 0 0 0( ) ( ) ( ) ( ) '( ) ( ) ( , )y x y x y x h y x y x h y x f x y h= = + + = +�

1 1 0 0 0

0 0 0

( ) ( ) ( , )

= ( , )

y y x y x f x y h

y f x y h

= +

+

En resumen:

En general, tenemos

1 1( ) ( ) ( , )

= ( , )

k k k k k

k k k

y y x y x f x y h

y f x y h

+ += +

+

Ejemplo Método de Euler

1 0 0 0( , ) 1 (3.5)(0.1) 1.35y y f t y h= + = + =

2 2 1 1 1( ) ( , ) 1.35 (0.1,1.35)(0.1)

1.35 (3.229837)(0.1) 1.672984

y t y y f t y h f≅ = + = +

= + ≅

SI QUEREMOS APROXIMAR y(1) CONTINUAMOS CON ESTE PROCESO

Ejemplo Consideremos la ecuación diferencial ordinaria

Recordemos que no es difícil encontrar la solución analítica, mediante el método de separación de variables, de la ecuación diferencial. Ejercicio: Hallar la solución analítica de la ecuación diferencial anterior

Veamos nuestro primer ejemplo para resolover ecuaciones diferenciales ordinarias de primer orden mediante Matlab:

function r = yexact(t,y0,K,s)

r = y0*exp(K*t) + s*(1 - exp(K*t));

Se ha definido una función que depende de cuatro argumentos: el temporal, la condición inicial y dos constantes.

Supongamos que sus valores están dados por y0=100, K=1, and s=20. Escribamos el siguiente código en

nuestra pantalla del editor de Matlab:

El siguiente comando crea un vector

t = 0:0.01:5;

plot(t,yexact(t,100,1,20))

El siguiente comando crea la gráfica de la solución

Figura 1: Solución exacta

Paso 1. definir f (t, y)

Paso 2. Entrada: Valores iniciales t0 and y0

Paso 3. entrada tamaño de paso h # de pasos n

Paso 4. salida t0 and y0

Paso 5. para j de 1 a n do

Paso 6. k1 = f (t, y)

y = y + h ∗ k1

T = t + h

Paso 7. salidas t y y

Paso 8. end

Estructura computacional del Método de Euler

Programa en Matlab

clc; clf; clear all; T=25; tinic=0.0; tfinal=5.0; yinic=50.0; n=100; f=@(t,y) -2*(y-T); h=(tfinal-tinic)/n; t=zeros(1,n+1); y=zeros(1,n+1); t(1)=tinic; y(1)=yinic; for i=1:n t(i+1)=t(i)+h; y(i+1)=y(i)+h*f(t(i),y(i)); end

plot(t,y,'b') title('Soluciones de dy/dt=-2*(y-T), y(0)=T mediante Euler') axis([0 5 20 55]) xlabel('Tiempo'); ylabel('Temperatura') hold on plot(t,25,'-r')

Gráfica obtenida con Matlab

Diferentes números de paso n=5,10

function [t,y]=Euler_clasico(f,tinic,yinic,tfinal,n) h=(tfinal-tinic)/n; t=zeros(1,n+1); y=zeros(1,n+1); t(1)=tinic; y(1)=yinic; for i=1:n t(i+1)=t(i)+h; y(i+1)=y(i)+h*f(t(i),y(i)); end

clc; clf; clear all; T=25; tinic=0.0; tfinal=5.0; yinic=50.0; n=100; f=@(t,y) -2*(y-T); [t1,y1]=Euler_clasico(f,tinic,yinic,tfinal,n); plot(t1,y1,'b') title('Soluciones de dy/dt=-2*(y-T), y(0)=T mediante Euler') axis([0 5 20 55]) xlabel('Tiempo'); ylabel('Temperatura') hold on plot(t1,25,'-r')

Programas Método de Euler en Matlab

TIPO FUNCIÓN

TIPO SCRIPT

%%%PROGRAMA DEL METODO DE EULER MODIFICADO CON UN ARCHIVO TIPO SCRIPT%%% %%%ECUACION DIFERENCIAL ORDINARIA dy/dt=k(y-c)%%%% k = 1; c = 20; y0= 100; npuntos = 50; %%Numero de pasos%% h = 0.1; y = zeros(npuntos,1); %Inicializamos el vector 'y' de posiciones con ceros% t = zeros(npuntos,1); %Inicializamos el vector 't' de tiempos con ceros% y(1) = y0; %Posicion inicial% t(1) = 0.0; %Timepo inicial% for j = 1 : npuntos %-1 % loop para el tamaño de paso% y(j+1) = y(j) + (h/2) * ((k*(y(j)-c)+ y(j) +h*k*(y(j)-c))); t(j+1) = t (j) + h; end

%%Hemos terminado la simulacion numerica%% %%Para el ERROR%% z = exp(t + log(80))+ 20; %Solucion analitica de la ecuacion %%Empezemos la parte de la visualizacion%% plot (t,y,'-bo',t,z,'r') grid on xlabel('tiempo') ylabel('y(t)') legend('solucion numerica','solucion analitica') title('Solucion numerica mediante Euler mejorado Vs … solucion analitica')

Euler mejorado (Heun, predictor-corrector)

0x 1 0x x h= +

1y%

1 1( , )f x y%

0 0( , )f x y

{ }0 0 1 1

1( , ) ( , )

2f x y f x y+ %

1y

1

1 11

: ( , )

( , ) ( , ):

2

i i i i

i i i ii i

y y f x y h

f x y f x yy y h

+

+ ++

= +

+= +

%

%

Predictor

Corrector

1 0 0 0: ( , )y y f x y h= +%Predictor 0 0 1 11 0

( , ) ( , ):

2

f x y f x yy y h

+= +

%Corrector

0x 1 0x x h= +

Ejemplo Euler-mejorado

Comparación Euler vs. Euler-mejorado

Estructura computacional del Método de Euler Mejorado

Paso 1. definir f (t, y)

Paso 2. Entrada: Valores iniciales t0 and y0

Paso 3. entrada tamaño de paso h # de pasos n

Paso 4. salida t0 and y0

Paso 5. para j de 1 a n do

Paso 6. k1 = f (t, y)

k2 = f (t + h, y + h ∗ k1)

y = y + (h/2) ∗ (k1 + k2)

t = t + h

Paso 7. salidas t y y

Paso 8. end

Programa Euler-mejorado en Matlab

function [t,y]=Euler_mejorado(f,tinic,yinic,tfinal,n) h=(tfinal-tinic)/n; t=zeros(1,n+1); y=zeros(1,n+1); t(1)=tinic; y(1)=yinic; for i=1:n t(i+1)=t(i)+h; P1=f(t(i),y(i)); P2=f(t(i)+h,y(i)+h*P1); y(i+1)=y(i)+(h/2)*(P1+P2); end

tinic=0.0; tfinal=1.0; yinic=50.0; n=1000; f=@(t,y) -2*(y-25); [t3,y3]=Euler_mejorado(f,tinic,yinic,tfinal,n); plot(t3,y3,'b') title('Soluciones de dy/dt=-2*(y-25), y(0)=T mediante Euler-mejorado') axis([0 1 20 55]) xlabel('Tiempo'); ylabel('Temperatura') axis([0 1 20 55])

22

• Se usa el método de Euler para

realizar una predicción del valor de

la solución usando la pendiente en

un punto intermedio del intervalo

1/2 ( , )2

i i i i

hy y f x y+ = +

Método de Punto Medio (o Polígono mejorado)

hyxfyy iiii ),( 2/12/11 +++ +=

Geometría Punto Medio

0x

1 00

2

1

2x x h

+= +

1 0x x h= +

0 0( , )x y

0 0( , )f x y

1 10 0

2 2

( , )f x y+ +

10

2

y+

1 0 0 00

2

( , )2

hy y f x y

+= +

Predicción

1y

1 0 1 10 0

2 2

( , )y y f x y h+ +

= +

Punto Medio

1

2

( , )2

i i ii

hy y f x y+= +Predicción

1 1 1

2 2

( , )i ii i

y y f x y h++ +

= +

Queremos aproximar numericamente soluciones de EDO’s

de primer orden

Filosofía:

),( yxfdx

dy=

1

1

Valor nuevo valor viejo pendiente * (tamaño_paso)

*

valor nuevo, valor viejo

pendiente, tamaño de paso

i i

i i

y y h

y y

h

ϕ

ϕ

+

+

= +

= +

¿Qué hemos hecho hasta el momento?

Runge Kuta

1

1

Valor nuevo valor viejo pendiente * (tamaño_paso)

*

valor nuevo, valor viejo

pendiente, tamaño de paso

i i

i i

y y h

y y

h

ϕ

ϕ

+

+

= +

= +

Buscamos un método de la forma

De manera que la pendiente sea una pendiente ponderada sobre distintos puntos del intervalo [x(i),x(i+1)].

Runge Kuta de Cuarto Orden (Acto de fe)

1

1

Valor nuevo valor viejo pendiente * (tamaño_paso)

*

valor nuevo, valor viejo

pendiente, tamaño de paso

k k

k k

y y h

y y

h

ϕ

ϕ

+

+

= +

= +

,1ik

,2ik

,3ik,4ik

it 1i it t h+ = +1

2it h+

iy

1iy +

( )1 1 2 3 4

1 * , 2 2

6k k n n n ny y h k k k kϕ ϕ+ = + = + + +

Ejemplo Runge Kuta Cuarto Orden

( , ) 1 4f t y t y= − +

1 0

1

( ) ( ) (0 0.2)

(0.2) 2.5016

y t y t h y

y y

= + = +

= ≅ ≅

Tamaño de paso:

Otro ejemplo

Repitiendo el proceso

Tamaño de paso

Calculamos las cuatro

pendientes

(1) ?y =

Es necesario realizar dos “juegos“ de cálculos

Obtención de Runge Kuta de Segundo Orden 1

1 1 2 2

i i

M M

y y h

a k a k a k

ϕ

ϕ+ = +

= + + +K

constantesia

1

2 1 11 1

3 2 21 1 22 2

1 1,1 1 1, 11 11

( , )

( , )

( , )

( , ... )

i i

i i

i i

M i M i M M M N

k f t y

k f t p h y q k h

k f t p h y q k h q k h

k f t p h y q k h q k h− − − − −

=

= + +

= + + +

= + + + +

M

es una pendiente ponderada de salidaϕ

23( ) ( ) '( ) ''( ) ( )

2

Pero podemos calcular '( ) y ''( )

'( ) ( , ( ))

( , ( )) ( , ( ))''( ) '( )

( , ( )) ( , ( )) ( , ( ))

i i i i

i i

i i i

i i i ii i

i i i ii i

hy t h y t y t h y t O h

y t y t

y t f t y t

f t y t f t y ty t y t

t y

f t y t f t y tf t y t

t y

+ = + + +

=

∂ ∂= +

∂ ∂

∂ ∂= + ⋅

∂ ∂

Aproximación de la solución por serie de Taylor de segundo orden alrededor de

it t=

1

2

2

( ) ( )

( ) '( ) ''( )2

( , ( )) ( , ( )) = ( ) ( , ( )) ( , ( ))

2

i i

i i i

i i i ii i i i i

y t y t h

hy t y t h y t

f t y t f t y t hy t f t y t h f t y t

t y

+ = +

≅ + +

∂ ∂+ + + ⋅ ∂ ∂

La aproximación obtenida está dada por

2

1

De esta forma,

( , ( )) ( , ( ))( )= ( ) ( , ( )) ( , ( ))

2

i i i ii i i i i i

f t y t f t y t hy t y t f t y t h f t y t

t y+

∂ ∂+ + + ⋅ ∂ ∂

( ) ( )( , ) ( , )( , ) ( , ) i i i ii i i i

f t y f t yf t H y K f t y H K

t y

∂ ∂+ + ≈ + +

∂ ∂

Aproximación por Serie de Taylor a primer orden de una función de dos variables

2kPara aproximar tomamos 1 11 1 y H p h K q k h= =

De esta forma, obtenemos

[ ] [ ]

2 1 11 1

1 11 1

( , )

( , ) ( , ) ( , )

i i

i i i ii i

k f t p h y q k h

f t y f t yf t y p h q k h

t y

= + +

∂ ∂= + ⋅ + ⋅

∂ ∂

De esta forma

[ ] [ ]

[ ]

[ ]

1 1 2 2

1 2 1 11 1

1 2 2 1 2 11 1

1 2 2 1 2 11

( , ) ( , ) = ( , ) ( , )

( , ) ( , ) = ( , )

( , ) ( , ) = ( , ) ( , )

i i i ii i i i

i i i ii i

i i i ii i i i

a k a k

f t y f t ya f t y a f t y p h q k h

t y

f t y f t ya a f t y h a p a q k

t y

f t y f t ya a f t y h a p a q f t y

t y

ϕ = +

∂ ∂+ + ⋅ + ⋅ ∂ ∂

∂ ∂+ + + ∂ ∂

∂ ∂+ + +

∂ ∂

En consecuencia,

[ ]

[ ]

1 1

1 2 2 1 2 11

2

1 2 2 1 2 11

( ) ( )

( , ) ( , ) = ( ) ( , ) ( , )

( , ) ( , ) = ( ) ( , ) ( , )

i i i i

i i i ii i i i i

i i i ii i i i i

y y t y h y t h

f t y f t yy t a a f t y h a p a q f t y h

t y

f t y f t yy t a a f t y h a p a q f t y h

t y

ϕ ϕ+ +≅ = + = +

∂ ∂+ + + + ∂ ∂

∂ ∂+ + + + ∂ ∂

[ ] 2

1 1 2 2 1 2 11

En resumen,

( , ) ( , )( ) ( ) ( , ) ( , )i i i ii i i i i i

f t y f t yy t y t a a f t y h a p a q f t y h

t y+

∂ ∂= + + + + ∂ ∂

Hemos obtenido dos aproximaciones para , a saber: 1( )iy t +

2

1

( , ( )) ( , ( ))( )= ( ) ( , ( )) ( , ( ))

2

i i i ii i i i i i

f t y t f t y t hy t y t f t y t h f t y t

x y+

∂ ∂+ + + ⋅ ∂ ∂

[ ] 2

1 1 2 2 1 2 11

( , ) ( , )( ) ( ) ( , ) ( , )i i i ii i i i i i

f t y f t yy t y t a a f t y h a p a q f t y h

t y+

∂ ∂= + + + + ∂ ∂

Igualando coeficientes

1 2 2 1 2 11

1 11, y

2 2a a a p a q+ = = =

Es decir,

1 2 1 11

2 2

1 11 , y

2 2a a p q

a a= − = =

Por lo que tenemos una infinidad de Métodos de Runge Kuta de orden 2, uno por cada valor que le demos al parámetro independiente 2a

Casos particulares

1 2 1 11

11

2a a p q= = ⇒ = =

[ ]

1

1 1 2 2 1 2

1

2

i iy y h

a k a k k k

ϕ

ϕ

+ = +

= + = +

1

2 1 11 1

1

( , )

( , )

( , )

= ( , ( , ) )

i i

i i

i i

i i i i

k f t y

k f t p h y q k h

f t h y k h

f t h y f t y h

=

= + +

= + +

+ +

donde

Por lo tanto

11

( , ) ( , )

2

i i i ii i

f t y f t h y k hy y h+

+ + + = +

¡¡¡¡¡¡¡Euler modificado (Heun o predictor-corrector)!!!!!!

Casos particulares

1 2 1 11

10, 1

2a a p q= = ⇒ = = 1

1 1 2 2 2

i iy y h

a k a k k

ϕ

ϕ+ = +

= + =

2 1 11 1( , )

1 1 ( , ( , ) )

2 2

i i

i i i i

k f t p h y q k h

f t h y f t y h

= + +

= + +

donde

Por lo tanto 1 1

1 1

2

1 1( , )

2 2

1 ( , )

2

i i i i

i ii

y y f t h y k h h

y f t y k h h

+

+

= + + +

= + +

1 1

2

1 1( , )

2 2i i i i

iy y k h y f t y h

+

= + = + %

1 1

1 1

2 2

1 1( , )

2 2

= ( , )

i i i i

ii i

y y f t h y k h h

y f t y h

+

+ +

= + + +

+ %

Obtenemos el Método de Punto Medio

1 1

1 1

2

1 1( , )

2 2

1 ( , )

2

i i i i

i ii

y y f t h y k h h

y f t y k h h

+

+

= + + +

= + +

1iy +%1iy +

iy

( , )i if t y

it 1it +

1 1( , )i if t y+ +%

t

y

Geometría del método

h

1

2

1

2i

it t h

+= +

1 1

2

1 1( , )

2 2i i i i

iy y k h y f t y h

+

= + = + %

1 1 1

2 2

( , )i ii i

y y f t y h++ +

= + %

0x

1 00

2

1

2x x h

+= +

1 0x x h= +

0 0( , )x y

0 0( , )f x y

1 10 0

2 2

( , )f x y+ +

10

2

y+

1 0 0 00

2

( , )2

hy y f x y

+= +

Predicción

1y

1 0 1 10 0

2 2

( , )y y f x y h+ +

= +

Sistemas de ecuaciones diferenciales

0 0 0 0

''( ) '( ) ( ) ( )

Cond. inic: ( ) , '( )

con el cambio de variables ( ) '( )

tenemos que

'( ) ''( ).

ax t bx t cx t F t

x t x x t x

y t x t

y t x t

+ + =

= =

=

=

%

'

1' ( )

x y

b cy F t y x

a a a

=

= − −

En consecuencia

'

1' ( )

x y

b cy F t y x

a a a

=

= − −

1

2

' ( , , )

1' ( , , ) ( )

x f x y t y

b cy f x y t F t y x

a a a

= =

= = − −

0 0

0 0

0 0

1

2

( )'' , ( )

( )'

( , , ) Tomamos ( , )

( , , )

x t xx xY Y Y t Y

y t xy y

f x y tF Y t

f x y t

= ⇒ = = = =

=

%

0 0' ( , ), ( )Y F Y t Y t Y= =

En resumen, obtenemos una ecuación de la forma

Muy semejante a una vieja conocida:

0 0' ( , ), ( )y f y t y t y= =

La cual aproximamos numéricamente mediante distintos algoritmos : P.ej. Euler

1 ( , )i i i iy y hf y t+ = +

Extendiendo a sistemas de ecuaciones

1 ( , )i i i iY Y hF Y t+ = +

( )( )

1 1

1 2

, ,

, ,

i i i i i

i i i i ii

x x f x y th

y y f x y t

+

+

= +

( )( )

1 1

1 2

, ,

, ,

i i i i i

i i i i i

x x hf x y t

y y hf x y t

+

+

= +

= +En resumen:

Euler para sistemas 1

2

( , ) 4 ,

( , )

f x y x y

f x y x y

= −

= − +

( ) ( )( ) ( )

1 1

1 2

, , 4

, ,

i i i i i i i i

i i i i i i i i

x x hf x y t x h x y

y y hf x y t x h x y

+

+

= + = + −

= + = + − +

1 0 0( , )f x y 2 0 0( , )f x y

1 1 1( , )f x y 2 1 1( , )f x y

Punto Medio para Sistemas de Ecuaciones

1

2

( , )2

i i ii

hY Y F Y t

+= + 1 1 1

2 2

( , )i ii i

Y Y f Y t h++ +

= +

1

2 1

21

2

1 1

2

1 2

2

( , , )

( , , )2

( , , )2

( , , )2

ii i i i

i i i ii

i i i ii

i i i ii

xx f x y th

y f x y ty

hx x f x y t

hy y f x y t

+

+

+

+

= +

= +

= +

1 1 1

2 21

1 2 1 1

2 2

1 1 1 1

2 2

1 2 1 1

2 2

( , , )

( , , )2

( , , )2

( , , )2

ii i

i i

i i ii i

i i ii i

i i ii i

f x y tx x h

y y f x y t

hx x f x y t

hy y f x y t

+ ++

++ +

++ +

++ +

= +

= +

= +

1

2

( , )2

i i ii

hy y f y t

+= + 1 1 1

2 2

( , )i ii i

y y f y t h++ +

= +Predicción Corrección

Dinámica de poblaciones: depredador-presa

RY

F

=

1(0)

1Y

=

0.1, 80h n= =

tinic=0.0, tfinal=8; xinic=1.0,yinic=1.0; n=80; f1=@(t,x,y) 2*x-1.2*x.*y; f2=@(t,x,y) -y+0.9*x.*y; [t1,x1,y1]=RK4_plano_MM(f1,f2,tinic,xinic,yinic,tfinal,n); [t2,x2,y2]=Euler_clasico_plano(f1,f2,tinic,xinic,yinic,tfinal,n); figure(1) plot(t2,x2,'r',t2,y2,'b‘), legend('t vs x','t vs y') title('t vs x,t vs y') figure(2) plot3(t2,x2,y2,'g'), title('t vs x vs y') figure(3) plot(x2,y2,'g'),title('x vx y') figure(4) plot(t1,x1,'r',t1,y1,'b'),legend('t vs x','t vs y') title('t vs x,t vs y') figure(5) plot3(t1,x1,y1,'k‘), title('t vs x vs y') figure(6) plot(x1,y1,'k'), title('x vx y') figure(7) plot(x1,y1,'g',x2,y2,'k'), title('x vx y')

Rutina en Matlab para comparar los métodos de Euler simple y Runge-Kutta de orden 4

Tarea

Dar los esquemas iterativos para sistemas de dos ecuaciones diferenciales de primer orden para los métodos de • Euler Mejorado •Runge Kutta de orden 4 • Euler hacia atrás