Tarea 7. Problemas con valores iniciales (EDOs).

Análisis Numérico.

Abril 2022

Aproxima la solución de los siguientes problemas con valores iniciales con los diferentes métodos provistos por las funciones del package pracma. Comenta la comparación entre las aproximaciones obtenidas y la solución exacta (en caso que se indique).

  1. \(y'=1+(t-y)^2\), \(2\leq t \leq 3\), \(y(2)=1\). Solución exacta \(y(t)=t+1/(1-t)\)
dy <- function(t,y){1+(t-y)^2}
solucion_real <- function(t){t+1/(1-t)}

t <- seq(from=2, to=3, length.out=10)

graf_der <- ggplot()+
  geom_line(aes(t, dy(t, solucion_real(t))), color="aquamarine", size=2)+
  labs(x="t", y="y(t)", title = "Grafica 1")+
  theme_light()

ggplotly(graf_der)

Euler

sol_eq_euler <- euler_heun(dy, 2, 3, 1, n=100, improved=TRUE)
sol_eq_rg <- ode45(dy, 2, 3, 1, hmax=0.01)

sol_graf <- ggplot()+
  geom_line(aes(t, solucion_real(t)), color="blue", size=1)+
  geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="red", size=1)+
  geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="gold", size=1)
labs(x="t", y="y(t)", title = "Solucion") +
  theme_light()
## NULL
ggplotly(sol_graf)
  1. \(y'=cos(2t)+sen(3t)\), \(0\leq t\leq 1\), \(y(0)=1\). Solución exacta \[\begin{equation} y(t)=\frac{1}{2}sen(2t)-\frac{1}{3}cos(3t)+\frac{4}{3}. \end{equation}\]
dy <- function(t,y){cos(2*t)+sin(3*t)}
t <- seq(from=0, to=1, length.out=10)
graf_der <- ggplot()+
  geom_line(aes(t, dy(t)), color="gold", size=2)+
  labs(x="t", y="y(t)",title = "Grafica 2")+
  theme_light()
  
ggplotly(graf_der)
sol_real <- function(t){0.5*sin(2*t)-(1/3)*cos(3*t)+4/3}
sol_eq_euler <- euler_heun(dy, 0, 1, 1, n=100, improved=TRUE)
sol_eq_rg <- ode45(dy, 0, 1, 1, hmax=0.01)
sol_graf <- ggplot()+
  geom_line(aes(t, solucion_real(t)), color="blue", size=1)+
  geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="red", size=1)+
  geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="gold", size=1)+
  labs(x="t", y="y(t)",title = "Solucion 2")+
  theme_light()
  
ggplotly(sol_graf)

\[\begin{equation} y'=\frac{y^2+y}{t}, \quad 1\leq t \leq 3, \quad y(1)=-2 \end{equation}\]

dy<- function(t, y){y^2+y}
t<- seq(from=1, to=3, length.out=10)
graf_der<- ggplot()+
  geom_line(aes(t, dy(t, solucion_real(t))), color="gold")+
  labs(x="t", y="y(t)",title ="Grafica 3" )+
  theme_light()
  
  ggplotly(graf_der)

\[\begin{equation} y'=\rac{y}{t}-(y/t)^2, \quad 1\leq t \leq 4, \quad y(1)=1 \end{equation}\]

Solución exacta.

dy <- function(t,y){y/t-(y/t)^2}
sol_real <- function(t){t/(1+log(t))}
t <- seq(from=1, to=4, length.out=10)
graf_der <- ggplot()+
  geom_line(aes(t, dy(t, solucion_real(t))), color="gold", size=1)+
  labs(x="t", y="y(t)",title ="Grafica 4" )+
  theme_light()
  
ggplotly(graf_der)

\[\begin{equation} y(t)=\frac{t}{1+log\,t} \end{equation}\]

dy <- function(t,y){y/t-(y/t)^2}
sol_real <- function(t){t/(1+log(t))}
t <- seq(from=1, to=4, length.out=10)
graf_der <- ggplot()+
  geom_line(aes(t, dy(t, solucion_real(t))), color="gold", size=1)+
  labs(x="t", y="y(t)",title ="solucion 4" )+
  theme_light()
graf_der
## Warning: Removed 1 row(s) containing missing values (geom_path).

\[\begin{equation} y'=(t+2t^3)y^3-ty, \quad 0\leq t \leq 2, \quad y(0)=\frac{1}{3} \end{equation}\]

Solución exacta.

\[\begin{equation} y(t)=\frac{1}{\sqrt{3+2t^2+6e^{t^2}}} \end{equation}\]

dy <- function(t,y){(t+2*t^3)*y^3-t*y}
sol_real <- function(t){1/sqrt(3+2*t^2+6*exp(t^2))}
t <- seq(from=0, to=2, length.out=100)
graf_der <- ggplot()+
  geom_line(aes(t, dy(t, solucion_real(t))), color="gold", size=2)+
  labs(x="t", y="y(t)",title ="Grafica 5" )+
  theme_light()
graf_der

sol_eq_euler <- euler_heun(dy, 0, 2, 1/3, n=100, improved= TRUE)
sol_eq_rg <- ode45(dy, 0, 2, 1/3,hmax=0.01)
sol_graf <- ggplot()+
  geom_line(aes(t, sol_real(t)), size=1)+
  geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="blue", size=1)+
  geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="red")+
  labs(x="t", y="y(t)",title = "Solucion 5")+
  theme_light()
ggplotly(sol_graf)

\[\begin{equation} y'=\frac{2-2ty}{t^2+1}, \quad 0\leq t \leq 3, \quad y(0)=1 \end{equation}\]

Solución exacta.

\[\begin{equation} y(t)=\frac{2t+1}{t^2+1} \end{equation}\]

dy <- function(t,y){(2-2*t*y)/(t^2+1)}
sol_real <- function(t){1/sqrt(3+2*t^2+6*exp(t^2))}
t <- seq(from=0, to=3, length.out=100)
graf_der <- ggplot()+
  geom_line(aes(t, dy(t, sol_real(t))), color="gold", size=1)+
  labs(x="X",y="y", title = "Grafica 6")+
  theme_light()
ggplotly(graf_der)

\[\begin{equation} \begin{aligned} y'_1&=y_2, &\qquad y_1(0)=1;\\ y'_2&=-y_1-2e^t+1, &\qquad y_2(0)=0;\\ y'_3&=-y_1-e^t+1, &\qquad y_3(0)=1; \end{aligned} \end{equation}\]

para \(0\leq t \leq 2\). Donde las soluciones exactas son:

\[\begin{equation} \begin{aligned} y_1(t)&=cos\,t+ sen\,t-e^t+1\\ y_2(t)&=-sen\, t+cos\, t-e^t\\ y_3(t)&=-sen\,t+cos\,t \end{aligned} \end{equation}\]

dy<-function(t, y){as.matrix(c(y[2],-y[1]-2*exp(t)+1, -y[1]-exp(t)+1))}
y0<-as.matrix(c(1, 0, 1))
sol_rg<-ode45(dy, 0, 2, y0, hmax=.01)
sol_sis<- ggplot()+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,1]), color="blue", size=2)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="red",size=2)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,3]), color="gold",size=2)+
  labs(x="X",y="y", title = "Grafica 7")+
  theme_light()
ggplotly(sol_sis)

sol real

y1 <- function(t){cos(t)+sin(t)-exp(t)+1}
y2 <- function(t){-sin(t)+cos(t)-exp(t)}
y3 <- function(t){-sin(t)+cos(t)}
sol_graf <- ggplot()+
  geom_line(aes(t, y1(t)), color="blue", size=2)+
  geom_line(aes(t, y2(t)), color="red", size=2)+
  geom_line(aes(t, y3(t)), color="gold", size=2)+
  labs(x="X",y="y", title = "Solucion 7")+
  theme_light()
ggplotly(sol_graf)

\[\begin{equation} \begin{aligned} y'_1&=3y_1+2y_2-(2t^2+1)e^{2t}, &\qquad y_1(0)=1;\\ y'_2&=4y_1+y_2+(t^2+2t-4)e^{2t}, &\qquad y_2(0)=1; \end{aligned} \end{equation}\]

para \(0\leq t \leq 1\). Donde las soluciones exactas son:

\[\begin{equation} \begin{aligned} y_1(t)&=\frac{1}{3}e^{5t}-\frac{1}{3}e^{-t}+e^{2t}\\ y_2(t)&=\frac{1}{3}e^{5t}+\frac{2}{3}e^{-t}+t^2e^{2t} \end{aligned} \end{equation}\]

dy <- function(t,y){as.matrix(3*y[1]+2*y[2]-2*(2*t^2+1)*exp(2*t), 4*y[1]+y[2]+(t^2+2*t-4)*exp(2*t))}
y0 <- as.matrix(c(1, 1))
sol_rg <- ode45(dy, 0, 1, y0, hmax=0.01)
sol_sis <- ggplot()+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,1]), color="blue", size=2)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="red", size=2)+
  labs(x="X", y ="Y", title = "Grafica 8")+
  theme_light()
ggplotly(sol_sis)
y1 <- function(t){(1/3)*exp(5*t)-(1/3)*exp(-t)+exp(2*t)}
y2 <- function(t){(1/3)*exp(5*t)+(2/3)*exp(-t)+t^2*exp(2*t)}
sol_graf <- ggplot()+
  geom_line(aes(t, y1(t)), color="blue", size=2)+
  geom_line(aes(t, y2(t)), color="red", size=2)+
  labs(x="X", y="Y", title = "Solucion 8")+
  theme_light()
ggplotly(sol_graf)

\[\begin{equation} \begin{aligned} y'_1&=y_2-y_3+t, &\qquad y_1(0)&=1;\\ y'_2&=3t^2, &\qquad y_2(0)&=1;\\ y'_3&=y_2+e^{-t}, &\qquad y_3(0)&=-1; \end{aligned} \end{equation}\] para \(0\leq t \leq 1\). Donde las soluciones exactas son: \[\begin{equation} \begin{aligned} y_1(t)&=-0.05t^5+0.25t^4+t+2-e^{-t}\\ y_2(t)&=t^3+1\\ y_3(t)&=0.25t^4+t-e^{-t} \end{aligned} \end{equation}\]

dy <- function(t,y){as.matrix(y[2]-y[3]+t, 3*t^2, y[2]+exp(-t))}
y0 <- as.matrix(c(1, 1, -1))
sol_rg <- ode45(dy, 0, 1, y0, hmax=0.01)
sol_sis <- ggplot()+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,1]), color="blue", size=1)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="red", size=1)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,3]), color="gold", size=1)+
  labs(x="X", y="Y", title = "Grafica 9")+
  theme_light()
ggplotly(sol_sis)
y1 <- function(t){-0.05*t^5+0.25*t^4+t+2-exp(-t)}
y2 <- function(t){t^3+1}
y3 <- function(t){0.25*t^4+t-exp(-t)}
sol_graf <- ggplot()+
  geom_line(aes(t, y1(t)), color="blue", size=1)+
  geom_line(aes(t, y2(t)), color="red", size=1)+
  geom_line(aes(t, y3(t)), color="gold", size=1)+
   labs(x="X", y="Y", title = "Solucion 9")+
  theme_light()
ggplotly(sol_graf)

\[\begin{equation} \begin{aligned} y'_1&=3y_1+2y_2-y_3-1-3t-2\, sen\,t, &\qquad y_1(0)&=5;\\ y'_2&=y_1-2y_2+3y_3+6-t+2\,sen\,t+cos\, t, &\qquad y_2(0)&=-9;\\ y'_3&=2y_1+4y_3+8-2t, &\qquad y_3(0)&=-5; \end{aligned} \end{equation}\]

para \(0\leq t \leq 2\). Donde las soluciones exactas son:

\[\begin{equation} \begin{aligned} y_1(t)&=2e^{3t}+3e^{-2t}+t\\ y_2(t)&=-8e^{-2t}+e^{4t}-2e^{3t}+\,sen\,t\\ y_3(t)&=2e^{4t}-4e^{3t}-e^{-2t}-2 \end{aligned} \end{equation}\]

dy<-function(t,y){as.matrix(c(3*y[1]+2*y[2]-y[3]-1-3*t-2*sin(t),                    y[1]-2*y[2]+3*y[3]+6-t+2*sin(t)+cos(t),2*y[1]+4*y[3]+-2*t))}
y0<-as.matrix(c(5, -9, -5))
sol_rg<-ode45(dy, 0, 2, y0, hmax=.1)
sol_sis<- ggplot()+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,1]), color="pink", size=1)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="blue", size=1)+
  geom_line(aes(sol_rg$t, sol_rg$y[ ,3]), color="purple", size=1)+
  labs(x="X", y="Y", title = "Grafica  10")+
  theme_light()

ggplotly(sol_sis)

Solucion real

y1 <- function(t){2*exp(3*t)+3*exp(-2*t)+t}
y2 <- function(t){-8*exp(-2*t)+exp(4*t)-2*exp(3*t)+sin(t)}
y3 <- function(t){2*exp(4*t)-4*exp(3*t)-exp(-2*t)-2}
sol_graf <- ggplot()+
  geom_line(aes(t, y1(t)), color="blue", size=1)+
  geom_line(aes(t, y2(t)), color="red", size=1)+
  geom_line(aes(t, y3(t)), color="gold", size=1)+
   labs(x="X", y="Y", title = "Solucion 10")+
  theme_light()
ggplotly(sol_graf)
  1. \(y''-2y'+y=t\,e^t-t\), \(0\leq t\leq 1\), \(y(0)=y'(0)=0\).

Solución exacta:

\[\begin{equation} y(t)=\frac{1}{6}t^3e^t-te^t+2e^t-t-2. \end{equation}\]

dy <- function(t, u){as.matrix(c(u[2], t*exp(t)-t-u[1]+2*u[2]))}
y0 <- as.matrix(c(0,0))
sol_aprox <- ode45(dy, 0, 1, y0, hmax=0.01)
y_exacta <- function(t){(1/6)*(t^3)*exp(t)-t*exp(t)+2*exp(t)-t-2}
sol_rg <- ode45(dy, 0, 1, y0, hmax=0.01)
sol_sis <- ggplot()+
  geom_line(aes(sol_aprox$t, y_exacta(sol_aprox$t)), color="red", size=1)+
  geom_line(aes(sol_aprox$t, sol_aprox$y[ ,1]), color="blue", size=1)+
  labs(x="t", y="y(t)", title = "Grafica 11")+
  theme_light()
ggplotly(sol_sis)
  1. \(y'''+2y''-y'-2y=e^t\), \(0\leq t\leq 3\), \(y(0)=1\), \(y'(0)=2\), \(y''(0)=0\).

Solución exacta:

\[\begin{equation} y(t)=\frac{43}{36}e^t+\frac{1}{4}e^{-t}-\frac{4}{9}e^{-2t}+\frac{1}{6}te^t. \end{equation}\]

dy <- function(t,u){as.matrix(c(u[2],u[3], exp(t)+2*u[1]+u[2]-2*u[3]))}
y0 <- as.matrix(c(1,2,0))
sol_aprox <- ode45(dy,0,3, y0, hmax = 0.1)
y_exacta <- function(t){(43/36)*exp(t)+1/4*exp(-t)-4/9*exp(-2*t)+1/6*t*exp(t)}
graf_aprox <- ggplot()+
  geom_line(aes(sol_aprox$t, y_exacta(sol_aprox$t)),color="blue", size=1)+
  geom_line(aes(sol_aprox$t, sol_aprox$y[,1]),color="red", size=1)+
  labs(x="t", y="y(t)", title = "Grafica 12")+
  theme_light()
  
ggplotly(graf_aprox)
  1. \(t^3y'''+t^2y''-2ty'+2y=8t^3-2\), \(1\leq t\leq 2\), \(y(1)=2\), \(y'(1)=8\), \(y''(1)=6\). Solución exacta: \(y(t)=2t-t^{-1}+t^2+t^3-1\) .
dy <- function(t, u){as.matrix(c(u[2], u[3], 8*t^3-2-2*u[1]+2*t*u[2]-(t^2)*u[3]))}
y0 <- as.matrix(c(2,8,6))
sol_aprox <- ode45(dy, 1, 2, y0, hmax=0.1)
y_exacta <- function(t){2*t-t^(-1)+t^2+t^3-1}
sol_rg <- ode45(dy, 1, 2, y0, hmax=0.1)
sol_sis <- ggplot()+
  geom_line(aes(sol_aprox$t, y_exacta(sol_aprox$t)), color="green", size=1)+
  geom_line(aes(sol_aprox$t, sol_aprox$y[ ,1]), color="gold", size=1)+
  labs(x="t", y="y(t)", title = "Grafica 13")+
  theme_light()
  
ggplotly(sol_sis)