SOLUCIÓN NUMÉRICA DE ECUACIONES DIFERENCIALES ORDINARIAS DE PRIMER ORDEN
SOLUCION NUMERICA Una solucón de esta ecuacón ncal con CI es una funcón ϕ : ( x ε, x + ε ) R tal que 0 0 ϕ '( x) = f ( x, ϕ( x)), para x ( x ε, x + ε ) 0 0 PROBLEMA: Hallar una aproxmacón numérca de la solucón de la EDO con CI
Campo de pendentes f(x,y) Curva solucón pendente Conocemos ϕ a = ϕ x0 ( ) ( ) Queremos obtener una aproxmacón de ϕ ( b) para b> a Consderamos una partcón del ntervalo [ ab, ] a= x0 < x < x < L< xn = b, t t donde h= + lo llamamos tamaño de paso. Observemos que n t, k = kh+ t0 k =,,... n
Consderamos la condcón ncal ( x, y ) = ( x, y( x )) = ( a, y( a)) 0 0 0 0 Curva solucón El desarrollo de Taylor er orden alrededor de x 0 y( x) = y( x ) + y '( x )( x x ) + R ( x) 0 0 0 obtenemos la aproxmacón lneal de y( x) pendente= cerca de x0 y( x) y( x ) + y '( x )( x x ) 0 0 0 tomando x= x = x0+ h obtenemos y( x) = y( x ) = y( x + h) y( x ) + y '( x ) h= y( x ) + f ( x, y ) h 0 0 0 0 0 0
En resumen: y = y( x ) y( x ) + f ( x, y ) h 0 0 0 = y + f ( x, y ) h 0 0 0 En general, tenemos y = y( x ) y( x ) + f ( x, y ) h k+ k+ k k k = y + f ( x, y ) h k k k
Ejemplo Método de Euler y = y + f ( t, y ) h= + (3.5)(0.) =.35 0 0 0 yt ( ) y = y + f ( t, y ) h=.35 + f (0.,.35)(0.) =.35 + (3.9837)(0.).67984 SI QUEREMOS APROXIMAR y() CONTINUAMOS CON ESTE PROCESO
Ejemplo Consderemos la ecuacón dferencal ordnara Recordemos que no es dfícl encontrar la solucón analítca, medante el método de separacón de varables, de la ecuacón dferencal. Ejercco: Hallar la solucón analítca de la ecuacón dferencal anteror Veamos nuestro prmer ejemplo para resolover ecuacones dferencales ordnaras de prmer orden medante Matlab: functon r = yexact(t,y0,k,s) r = y0*exp(k*t) + s*( - exp(k*t));
Se ha defndo una funcón que depende de cuatro argumentos: el temporal, la condcón ncal y dos constantes. Supongamos que sus valores están dados por y0=00, K=, and s=0. Escrbamos el sguente códgo en nuestra pantalla del edtor de Matlab: El sguente comando crea un vector t = 0:0.0:5; El sguente comando crea la gráfca de la solucón plot(t,yexact(t,00,,0)) Fgura : Solucón exacta
Estructura computaconal del Método de Euler Paso. defnr f (t, y) Paso. Entrada: Valores ncales t0 and y0 Paso 3. entrada tamaño de paso h # de pasos n Paso 4. salda t0 and y0 Paso 5. para j de a n do Paso 6. k = f (t, y) y = y + h k T = t + h Paso 7. saldas t y y Paso 8. end
Programa en Matlab clc; clf; clear all; T=5; tnc=0.0; tfnal=5.0; ync=50.0; n=00; f=@(t,y) -*(y-t); h=(tfnal-tnc)/n; t=zeros(,n+); y=zeros(,n+); t()=tnc; y()=ync; for =:n t(+)=t()+h; y(+)=y()+h*f(t(),y()); end plot(t,y,'b') ttle('solucones de dy/dt=-*(y-t), y(0)=t medante Euler') axs([0 5 0 55]) xlabel('tempo'); ylabel('temperatura') hold on plot(t,5,'-r')
Gráfca obtenda con Matlab
Dferentes números de paso n=5,0
Programas Método de Euler en Matlab TIPO FUNCIÓN functon [t,y]=euler_clasco(f,tnc,ync,tfnal,n) h=(tfnal-tnc)/n; t=zeros(,n+); y=zeros(,n+); t()=tnc; y()=ync; for =:n t(+)=t()+h; y(+)=y()+h*f(t(),y()); end TIPO SCRIPT clc; clf; clear all; T=5; tnc=0.0; tfnal=5.0; ync=50.0; n=00; f=@(t,y) -*(y-t); [t,y]=euler_clasco(f,tnc,ync,tfnal,n); plot(t,y,'b') ttle('solucones de dy/dt=-*(y-t), y(0)=t medante Euler') axs([0 5 0 55]) xlabel('tempo'); ylabel('temperatura') hold on plot(t,5,'-r')
%%%PROGRAMA DEL METODO DE EULER MODIFICADO CON UN ARCHIVO TIPO SCRIPT%%% %%%ECUACION DIFERENCIAL ORDINARIA dy/dt=k(y-c)%%%% k = ; c = 0; y0= 00; npuntos = 50; %%Numero de pasos%% h = 0.; y = zeros(npuntos,); %Incalzamos el vector 'y' de poscones con ceros% t = zeros(npuntos,); %Incalzamos el vector 't' de tempos con ceros% y() = y0; %Poscon ncal% t() = 0.0; %Tmepo ncal% for j = : npuntos %- % loop para el tamaño de paso% y(j+) = y(j) + (h/) * ((k*(y(j)-c)+ y(j) +h*k*(y(j)-c))); t(j+) = t (j) + h; end
%%Hemos termnado la smulacon numerca%% %%Para el ERROR%% z = exp(t + log(80))+ 0; %Solucon analtca de la ecuacon %%Empezemos la parte de la vsualzacon%% plot (t,y,'-bo',t,z,'r') grd on xlabel('tempo') ylabel('y(t)') legend('solucon numerca','solucon analtca') ttle('solucon numerca medante Euler mejorado Vs solucon analtca')
Euler mejorado (Heun, predctor-corrector) 0 0 Predctor: y% = y + f ( x, y ) h Corrector: y = y + 0 0 0 0 f ( x, y ) + f ( x, y% ) h f ( x, y% ) f ( x0, y0) y% f x y f x y y + % { (, ) (, )} 0 0 x = x + h x0 0 x = x + h x0 0 Predctor: y% = y + f ( x, y ) h Corrector: + y = y + + f ( x, y ) + f ( x, y% + + ) h
Ejemplo Euler-mejorado
Comparacón Euler vs. Euler-mejorado
Estructura computaconal del Método de Euler Mejorado Paso. defnr f (t, y) Paso. Entrada: Valores ncales t0 and y0 Paso 3. entrada tamaño de paso h # de pasos n Paso 4. salda t0 and y0 Paso 5. para j de a n do Paso 6. k = f (t, y) k = f (t + h, y + h k) y = y + (h/) (k + k) t = t + h Paso 7. saldas t y y Paso 8. end
Programa Euler-mejorado en Matlab functon [t,y]=euler_mejorado(f,tnc,ync,tfnal,n) h=(tfnal-tnc)/n; t=zeros(,n+); y=zeros(,n+); t()=tnc; y()=ync; for =:n t(+)=t()+h; P=f(t(),y()); P=f(t()+h,y()+h*P); y(+)=y()+(h/)*(p+p); end tnc=0.0; tfnal=.0; ync=50.0; n=000; f=@(t,y) -*(y-5); [t3,y3]=euler_mejorado(f,tnc,y nc,tfnal,n); plot(t3,y3,'b') ttle('solucones de dy/dt=-*(y- 5), y(0)=t medante Eulermejorado') axs([0 0 55]) xlabel('tempo'); ylabel('temperatura') axs([0 0 55])
Método de Punto Medo (o Polígono mejorado) Se usa el método de Euler para realzar una predccón del valor de la solucón usando la pendente en un punto ntermedo del ntervalo h y+ / = y+ f ( x, y ) y + = y+ f ( x+ /, y+ / ) h
Geometría Punto Medo Predccón h y y f x y = 0+ ( 0, 0) 0+ y = y + f ( x, y ) h 0 0+ 0+ f ( x, y ) 0+ 0+ f ( x0, y0) ( x0, y0) y 0+ y x 0 x x h = 0+ 0+ x = x0+ h
Punto Medo Predccón h y = y+ fxy (, ) + y = y + f ( x, y ) h + + +
Qué hemos hecho hasta el momento? Queremos aproxmar numercamente solucones de EDO s de prmer orden dy = dx f ( x, y) Flosofía: Valor nuevo = valor vejo + pendente * (tamaño_paso) y = y + ϕ * h y + + valor nuevo, y valor vejo ϕ pendente, h tamaño de paso
Runge Kuta Buscamos un método de la forma Valor nuevo = valor vejo + pendente * (tamaño_paso) y = y + ϕ * h y + + valor nuevo, y valor vejo ϕ pendente, h tamaño de paso De manera que la pendente sea una pendente ponderada sobre dstntos puntos del ntervalo [x(),x(+)].
Runge Kuta de Cuarto Orden (Acto de fe) Valor nuevo = valor vejo + pendente * (tamaño_paso) y k+ y = y + ϕ * h k+ valor nuevo, y valor vejo ϕ pendente, h tamaño de paso k k
y = y + ϕ * h, ϕ = + k k k k 6 + + + ( ) k k n n n3 n4 k, k, k,4 k,3 y + y t t = + + h + t t h
Ejemplo Runge Kuta Cuarto Orden f ( t, y) = t+ 4y Tamaño de paso: yt ( ) = yt ( + h) = y(0+ 0.) 0 = y(0.) y.506
Otro ejemplo y () =? Tamaño de paso Es necesaro realzar dos juegos de cálculos Calculamos las cuatro pendentes Reptendo el proceso
ϕ Obtencón de Runge Kuta de Segundo Orden y y ϕh = + + ϕ= ak + ak + K+ a k M M es una pendente ponderada de salda a constantes k = f ( t, y ) k = f ( t + ph, y + q kh) k = f ( t + ph, y + q kh+ q kh) 3 M k = f ( t + p h, y + q kh+... + q k h) M M M, M, M N
Aproxmacón de la solucón por sere de Taylor de segundo orden alrededor de t= t h yt h yt y t h y t Pero podemos calcular y '( t ) y y ''( t ) 3 ( + ) = ( ) + '( ) + ''( ) + Oh ( ) y '( t ) = f ( t, yt ( )) f ( t, yt ( )) f ( t, yt ( )) y ''( t ) = + y '( t ) t y f ( t, yt ( )) f ( t, yt ( )) = + t y f ( t, yt ( ))
La aproxmacón obtenda está dada por yt ( ) yt ( h) + = + h yt ( ) + y '( t ) h+ y ''( t ) f ( t, yt ( )) f ( t, yt ( )) h = yt ( ) + f ( t, yt ( )) h+ + f ( t, yt ( )) t y De esta forma, (, ( )) (, ( )) ( )= ( ) (, ( )) f t yt f t yt yt yt f t yt h f ( t, yt ( )) h + + + + t y
Aproxmacón por Sere de Taylor a prmer orden de una funcón de dos varables f ( t, y ) f ( t, y ) f ( t+ H, y + K) f ( t, y ) + H + K t y Para aproxmar k tomamos ( ) ( ) H = ph y K = q kh De esta forma, obtenemos k = f ( t + ph, y + q kh) f ( t, y ) f ( t, y ) = f ( t, y ) + ph + qkh t y [ ] [ ]
De esta forma ϕ = ak + ak f ( t, y ) f ( t, y ) = af ( t, y ) + a f ( t, y ) + ph + qkh t y [ ] [ ] f ( t, y ) f ( t, y ) = [ a+ a] f ( t, y ) + h ap + aq k t y f ( t, y ) f ( t, y ) = [ a+ a] f ( t, y ) + h ap + aq f ( t, y ) t y
En consecuenca, y yt ( ) = y + ϕh= yt ( ) + ϕh + + f ( t, y ) f ( t, y ) = yt ( ) + [ a+ a] f ( t, y ) + h ap + aq f ( t, y ) h t y f ( t, y ) f ( t, y ) = yt ( ) + [ a+ a] f ( t, y ) h+ ap + aq f ( t, y ) h t y En resumen, f ( t, y ) f ( t, y ) yt ( + ) = yt ( ) + a+ a f ( t, y ) h+ ap + aq f ( t, y ) h t y [ ]
Hemos obtendo dos aproxmacones para yt ( + ), a saber: (, ( )) (, ( )) ( )= ( ) (, ( )) f t yt f t yt yt yt f t yt h f ( t, yt ( )) h + + + + x y f ( t, y ) f ( t, y ) yt ( + ) = yt ( ) + a+ a f ( t, y ) h+ ap + aq f ( t, y ) h t y Igualando coefcentes [ ] a+ a =, ap = y aq =
Es decr, a = a, p = y q = a a Por lo que tenemos una nfndad de Métodos de Runge Kuta de orden, uno por cada valor que le demos al parámetro ndependente a
Casos partculares a = a = p = q = donde y y ϕh = + + ϕ= ak + ak = + k = f ( t, y ) [ k k ] k = f ( t + ph, y + q kh) = f ( t + h, y + kh) = f ( t + h, y + f ( t, y ) h) Por lo tanto y y f ( t, y ) + f ( t + h, y + kh) h + = + Euler modfcado (Heun o predctor-corrector)!!!!!!
Casos partculares a = 0, a = p = q = donde y y ϕh = + + ϕ= ak + ak = k k = f ( t + ph, y + q kh) = f ( t+ h, y+ f ( t, y ) h) y+ = y + f ( t+ h, y + khh ) = y + f ( t, y+ khh ) + Por lo tanto
y+ = y + f ( t+ h, y + khh ) = y + f ( t, y + khh ) y% = y + kh= y + f ( t, y ) h + y+ = y+ f ( t+ h, y + khh ) = y + f ( t, y% ) h + + + Obtenemos el Método de Punto Medo
Geometría del método y = y + kh= y + f ( t, y ) h y y = y + f ( t, y% ) h % + + + + f ( t, y ) y % y + t = t + h t + t + f ( t, y% ) t + + y + h
Predccón h y y f x y = 0+ ( 0, 0) 0+ y = y + f ( x, y ) h 0 0+ 0+ f ( x, y ) 0+ 0+ f ( x0, y0) ( x0, y0) y 0+ y x 0 x x h = 0+ 0+ x = x0+ h
Sstemas de ecuacones dferencales ax ''( t) + bx '( t) + cxt ( ) = F( t) Cond. nc: xt ( ) = x, x '( t ) = x% 0 0 0 0 con el cambo de varables yt ( ) = x '( t) tenemos que y '( t) = x ''( t). En consecuenca x ' = y b c y ' = F( t) y x a a a
x ' = y b c y ' = F( t) y x a a a x ' = f ( x, yt, ) = y b c y ' = f( x, yt, ) = F( t) y x a a a x x ' xt ( ) x Y = Y ' =, Y ( t ) = = = Y y y ' 0 0 0 0 yt ( 0) x% 0 Tomamos F( Y, t) f ( x, yt, ) = f( x, yt, )
En resumen, obtenemos una ecuacón de la forma Y ' = F( Y, t), Y ( t ) = Y Muy semejante a una veja conocda: 0 0 y ' = f ( yt, ), yt ( ) = y 0 0 La cual aproxmamos numércamente medante dstntos algortmos : P.ej. Euler y = y + + hf ( y, t )
Extendendo a sstemas de ecuacones Y = Y + + hf( Y, t ) x ( ) + x f x, y, t = + h y y f x, y, t ( ) + En resumen: x = x + hf x, y, t ( ) + y = y + hf x, y, t ( ) +
Euler para sstemas f ( x, y) = x 4 y, f ( x, y) = x+ y (,, ) ( 4 ) (,, ) ( ) x = x + hf x y t = x + h x y + y = y + hf x y t = x + h x + y + f( x0, y0) f( x0, y0) f( x, y ) f( x, y )
Punto Medo para Sstemas de Ecuacones Predccón h y = y+ f ( y, t ) + = + + + + y y f ( y, t ) h Correccón h Y = Y + F( Y, t ) + = + + + + x + x h f( x, y, t ) y = + y f( x, y, t ) + h x = x + f ( x, y, t ) + h y = y + f ( x, y, t ) + Y Y f ( Y, t ) h f( x, y, t ) x x + + + h = + y y + f( x, y, t ) + + h x = x + f ( x, y, t ) + + + h y = y + f ( x, y, t ) + + +
Dnámca de poblacones: depredador-presa Y R = F Y (0) = h= 0., n= 80
tnc=0.0, tfnal=8; xnc=.0,ync=.0; n=80; f=@(t,x,y) *x-.*x.*y; f=@(t,x,y) -y+0.9*x.*y; [t,x,y]=rk4_plano_mm(f,f,tnc,xnc,ync,tfnal,n); [t,x,y]=euler_clasco_plano(f,f,tnc,xnc,ync,tfnal,n); fgure() plot(t,x,'r',t,y,'b ), legend('t vs x','t vs y') ttle('t vs x,t vs y') fgure() plot3(t,x,y,'g'), ttle('t vs x vs y') fgure(3) plot(x,y,'g'),ttle('x vx y') fgure(4) plot(t,x,'r',t,y,'b'),legend('t vs x','t vs y') ttle('t vs x,t vs y') fgure(5) plot3(t,x,y,'k ), ttle('t vs x vs y') fgure(6) plot(x,y,'k'), ttle('x vx y') fgure(7) plot(x,y,'g',x,y,'k'), ttle('x vx y') Rutna en Matlab para comparar los métodos de Euler smple y Runge- Kutta de orden 4
Tarea Dar los esquemas teratvos para sstemas de dos ecuacones dferencales de prmer orden para los métodos de Euler Mejorado Runge Kutta de orden 4 Euler haca atrás