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).
- \(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}
sol_real <- function(t){t+1/(1-t)}
t <- seq(from=2, to=3, length.out=100)
graf_der <- ggplot()+
geom_line(aes(t, dy(t, sol_real(t))), color="blue", size=1)+
theme_bw()
graf_derset.seed(28)
#A=matrix(sample(-10:20, 20, replace = TRUE), nrow=4, ncol=5, byrow = TRUE)
#A
B=matrix(sample(0:100, 20, replace = TRUE), nrow=5, ncol=5, byrow = TRUE)
B## [,1] [,2] [,3] [,4] [,5]
## [1,] 80 31 40 97 77
## [2,] 92 10 96 82 24
## [3,] 63 56 26 39 55
## [4,] 95 28 2 0 15
## [5,] 80 31 40 97 77
#C=matrix(sample(-10:20, 20, replace = TRUE), nrow=4, ncol=5, byrow = TRUE)
#C
#A+CB## [,1] [,2] [,3] [,4] [,5]
## [1,] 80 31 40 97 77
## [2,] 92 10 96 82 24
## [3,] 63 56 26 39 55
## [4,] 95 28 2 0 15
## [5,] 80 31 40 97 77
det(B)## [1] 1.532979e-07
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, sol_real(t)), color="azure3", size=1.4)+
geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="lightsteelblue", size=1)+
geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="lightblue", size=0.6)+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_graf)- \(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=100)
graf_der <- ggplot()+
geom_line(aes(t, dy(t)), color="purple", size=1)+
theme_bw()
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, sol_real(t)), color="azure3", size=1.4)+
geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="yellow", size=1)+
geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="pink", size=0.6)+
theme_bw()
ggplotly(sol_graf)\[\begin{equation} y'=\frac{y^2+y}{t}, \quad 1\leq t \leq 3, \quad y(1)=-2 \end{equation}\]
\[\begin{equation} y'=\frac{y}{t}-(y/t)^2, \quad 1\leq t \leq 4, \quad y(1)=1 \end{equation}\]
Solución exacta.
\[\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=100)
graf_der <- ggplot()+
geom_line(aes(t, dy(t, sol_real(t))), color="purple", size=1)+
theme_bw()
graf_dersol_eq_euler <- euler_heun(dy, 1, 4, 1, n=100, improved=TRUE)
sol_eq_rg <- ode45(dy, 1, 4, 1, hmax=0.01)
sol_graf <- ggplot()+
geom_line(aes(t, sol_real(t)), color="azure3", size=1.4)+
geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="green", size=1)+
geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="brown", size=0.6)+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_graf)\[\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, sol_real(t))), color="purple", size=1)+
theme_bw()
graf_dersol_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)), color="azure3", size=1.4)+
geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="green", size=1)+
geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="purple", size=0.6)+
labs(x="t", y="y(t)")+
theme_bw()
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="blue", size=1)+
theme_bw()
graf_dersol_eq_euler <- euler_heun(dy, 0, 3, 1/3, n=100, improved=TRUE)
sol_eq_rg <- ode45(dy, 0, 3, 1/3, hmax=0.01)
sol_graf <- ggplot()+
geom_line(aes(t, sol_real(t)), color="azure3", size=1.4)+
geom_line(aes(sol_eq_euler$t, sol_eq_euler$y), color="lightblue", size=1)+
geom_line(aes(sol_eq_rg$t, sol_eq_rg$y), color="purple", size=0.6)+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_graf)\[\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}\]
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="red")+
geom_line(aes(t, y2(t)), color="lightblue")+
geom_line(aes(t, y3(t)), color="pink")+
theme_bw()
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="pink")+
geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="purple")+
theme_bw()
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="red")+
geom_line(aes(t, y2(t)), color="pink")+
theme_bw()
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="brown")+
geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="green")+
geom_line(aes(sol_rg$t, sol_rg$y[ ,3]), color="blue")+
theme_bw()
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="lightblue")+
geom_line(aes(t, y2(t)), color="yellow")+
geom_line(aes(t, y3(t)), color="green")+
theme_bw()
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[2]+8-2*t))}
y0 <- as.matrix(c(5, -9, -5))
sol_rg <- ode45(dy, 0, 2, y0, hmax=0.01)
sol_sis <- ggplot()+
geom_line(aes(sol_rg$t, sol_rg$y[ ,1]), color="blue")+
geom_line(aes(sol_rg$t, sol_rg$y[ ,2]), color="brown")+
geom_line(aes(sol_rg$t, sol_rg$y[ ,3]), color="purple")+
theme_bw()
ggplotly(sol_sis)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="pink")+
geom_line(aes(t, y2(t)), color="brown")+
geom_line(aes(t, y3(t)), color="yellow")+
theme_bw()
ggplotly(sol_graf)- \(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="purple", size=1.5)+
geom_line(aes(sol_aprox$t, sol_aprox$y[ ,1]), color="lightblue")+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_sis)- \(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.01)
y_exacta <- function(t){43/36*exp(t)+1/4*exp(-t)-4/9*exp(-2*t)+1/6*exp(t)}
sol_rg <- ode45(dy, 0, 3, y0, hmax=0.01)
sol_sis <- ggplot()+
geom_line(aes(sol_aprox$t, y_exacta(sol_aprox$t)), color="blue", size=1.5)+
geom_line(aes(sol_aprox$t, sol_aprox$y[ ,1]), color="green")+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_sis)- \(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.01)
y_exacta <- function(t){2*t-t^(-1)+t^2+t^3-1}
sol_rg <- ode45(dy, 1, 2, y0, hmax=0.01)
sol_sis <- ggplot()+
geom_line(aes(sol_aprox$t, y_exacta(sol_aprox$t)), color="purple", size=1.5)+
geom_line(aes(sol_aprox$t, sol_aprox$y[ ,1]), color="blue")+
labs(x="t", y="y(t)")+
theme_bw()
ggplotly(sol_sis)