Tarea 6. Diferenciación e integración numérica.

Análisis Numérico. Dana Paola Mendez Juarez

Abril del 2022

Ejercicio 1.

Para cada una de las siguientes funciones realiza la respectiva gráfica en el intervalo dado. Compara las gráficas de las derivadas aproximadas (con dos tamaños de paso \(h\neq 0\) y \(h=0\)) y la derivada exacta en tal intervalo.

  1. \(f(x)=e^{2x}-cos 2x\), \(x\in [0,2]\)
f <- function(x){exp(2*x)-cos(2*x)}
x <- seq(from=0, to=2, length.out=100)
y <- f(x)

graf_1a <- ggplot()+
  geom_line(aes(x,y), color="brown", size=1)+
  theme_bw()
ggplotly(graf_1a)
der_exacta <- function (x){2*exp(2*x)+2*sin(2*x)}
der_h_1<- fderiv(f, x, n=1, h=1, method="central")
der_h_0.05<- fderiv(f, x, n=1, h=0.5, method="central")
der_h_0<- fderiv(f, x, n=1, h=0, method="central")

graf_der_1a <- ggplot()+
  geom_line(aes(x,der_exacta(x)), color="black")+
  geom_line(aes(x,der_h_1), color="purple")+
  geom_line(aes(x,der_h_0.05), color="pink")+
  geom_line(aes(x,der_h_0), color="yellow")+
  theme_bw()
ggplotly(graf_der_1a)
error_h1 <- abs(der_exacta(x)-der_h_1)
ggplot()+
  geom_line(aes(x, error_h1), color="brown")+
  theme_bw()

error_h0 <- abs(der_exacta(x)-der_h_0)

ggplot()+
  geom_line(aes(x, error_h0), color="red", size=1.2)+
  theme_bw()

  1. \(f(x)=log(x+2)-(x+1)^2\), \(x\in [0,5]\)
f <- function(x){log(x+2)-(x+1)^2}
x <- seq(from=0, to=5, length.out=100)
y <- f(x)

graf_1b <- ggplot()+
  geom_line(aes(x,y), color="firebrick")+
  theme_bw()
ggplotly(graf_1b)
der_exacta <- function(x){1/(x+2)-2*(x+1)}
der_h_1.5<- fderiv(f, x, n=1, h=1.5, method="central")
der_h_1<- fderiv(f, x, n=1, h=1, method="central")
der_h_0.05<- fderiv(f, x, n=1, h=0.5, method="central")
der_h_0<- fderiv(f, x, n=1, h=0, method="central")

graf_der_1b <- ggplot()+
  geom_line(aes(x,der_exacta(x)), color="green")+
  geom_line(aes(x,der_h_1.5), color="orange")+
  geom_line(aes(x,der_h_1), color="blue")+
  geom_line(aes(x,der_h_0.05), color="pink")+
  geom_line(aes(x,der_h_0), color="yellow")+
  theme_bw()
ggplotly(graf_der_1b)
error_h1 <- abs(der_exacta(x)-der_h_1.5)

ggplot()+
  geom_line(aes(x, error_h1), color="purple")+
  theme_bw()

  1. \(f(x)=x\, sen\,x+x^2cos\,x\), \(x\in [0,\pi]\)
f <- function(x){x*sin(x)+x^2*cos(x)}
x <- seq(from=0, to=pi, length.out=100)
y <- f(x)

graf_1c <- ggplot()+
  geom_line(aes(x,y), color="forestgreen", size=1)+
  theme_bw()
ggplotly(graf_1c)
der_exacta <- function (x){3*x*cos(x)+(1-x^2)*sin(x)}
der_h_1<- fderiv(f, x, n=1, h=1, method="central")
der_h_0.05<- fderiv(f, x, n=1, h=0.5, method="central")
der_h_0<- fderiv(f, x, n=1, h=0, method="central")

graf_der_c <- ggplot()+
  geom_line(aes(x,der_exacta(x)), color="brown")+
  geom_line(aes(x,der_h_1), color="red")+
  geom_line(aes(x,der_h_0.05), color="purple")+
  geom_line(aes(x,der_h_0), color="blue")+
  theme_bw()
ggplotly(graf_der_c)
error_h1 <- abs(der_exacta(x)-der_h_1)

ggplot()+
  geom_line(aes(x, error_h1), color="#235ADE")+
  theme_bw()

error_h0 <- abs(der_exacta(x)-der_h_0)

ggplot()+
  geom_line(aes(x, error_h0), color="pink", size=1.2)+
  theme_bw()

  1. \(f(x)=(cos\,3x)^2-e^{2x}\), \(x\in [0,\pi/2]\)
f <- function(x){(cos(3*x))^2-exp(2*x)}
x <- seq(from=0, to=pi/2, length.out=100)
y <- f(x)

graf_1d <- ggplot()+
  geom_line(aes(x,y), color="firebrick", size=1)+
  theme_bw()
ggplotly(graf_1d)
error_h1 <- abs(der_exacta(x)-der_h_1)

ggplot()+
  geom_line(aes(x, error_h1), color="blue")+
  theme_bw()

error_h0 <- abs(der_exacta(x)-der_h_0)

ggplot()+
  geom_line(aes(x, error_h0), color="red", size=1)+
  theme_bw()

Ejercicio 2

Da el valor aproximado (por medio de las funciones integral y cotes, del package pracma) y exacto (en caso de ser posible) de las siguientes integrales (realiza la respectiva gráfica).

\[\begin{equation} \int_{0.5}^1 x^4 dx \end{equation}\]

f <- function(x){x^4}
x <- seq(from=0.5, to=1.5, length.out=100)
y <- f(x)

graf_2a <- ggplot()+
  geom_line(aes(x,y), color="gold", size=1)+
  geom_area(aes(x,y), fill="purple", alpha=0.5)+
  theme_bw()
ggplotly(graf_2a)

Metodo clenshaw

pracma::integral(f, 0.5,1, method="Clenshaw")
## [1] 0.19375

Metodo simpson

pracma::integral(f, 0.5,1, method="Simpson")
## [1] 0.19375

\[\begin{equation} \int_{0}^{0.5} \frac{2}{x-4} dx \end{equation}\]

f <- function(x){2/(x-4)}
x <- seq(from=0, to=1, length.out=100)
y <- f(x)

graf_2b <- ggplot()+
  geom_line(aes(x,y), color="blue", size=1.5)+
  geom_area(aes(x,y), fill="pink", alpha=0.5)+
  theme_bw()
ggplotly(graf_2b)

Metodo clenshaw

pracma::integral(f, 0,0.5, method="Clenshaw")
## [1] -0.2670628

Metodo simpson

pracma::integral(f, 0,0.5, method="Simpson")
## [1] -0.2670628

\[\begin{equation} \int_{1}^{1.5} x^2\, log(x) dx \end{equation}\]

f <- function(x){x^2*log(x)}
x <- seq(from=1, to=1.5, length.out=100)
y <- f(x)

graf_2c <- ggplot()+
  geom_line(aes(x,y), color="purple", size=1)+
  geom_area(aes(x,y), fill="red", alpha=0.5)+
  theme_bw()
ggplotly(graf_2c)

Metodo clenshaw

pracma::integral(f, 1,1.5, method="Clenshaw")
## [1] 0.1922594

Metodo simpson

pracma::integral(f, 1,1.5, method="Simpson")
## [1] 0.1922594

\[\begin{equation} \int_{0}^{1} x^2 e^{-x}\, log(x) dx \end{equation}\]

f <- function(x){x^2*exp(-x)*log(x)}
x <- seq(from=0, to=1, length.out=100)
y <- f(x)

graf_2d <- ggplot()+
  geom_line(aes(x,y), color="yellow", size=1)+
  geom_area(aes(x,y), fill="pink", alpha=0.5)+
  theme_bw()
ggplotly(graf_2d)
## Warning: Removed 1 rows containing missing values (position_stack).

\[\begin{equation} \int_{1}^{1.6} \frac{2x}{x^2-4} dx \end{equation}\]

f <- function(x){2*x/(x^2-4)}
x <- seq(from=1, to=1.6, length.out=100)
y <- f(x)

graf_2e <- ggplot()+
  geom_line(aes(x,y), color="purple", size=1)+
  geom_area(aes(x,y), fill="yellow", alpha=0.5)+
  theme_bw()
ggplotly(graf_2e)

\[\begin{equation} \int_{0}^{\pi/4} e^{3x}sin(2x) dx \end{equation}\]

f <- function(x){exp(3*x)*sin(2*x)}
x <- seq(from=0, to=pi/4, length.out=100)
y <- f(x)

graf_2f <- ggplot()+
  geom_line(aes(x,y), color="red", size=1)+
  geom_area(aes(x,y), fill="pink", alpha=0.5)+
  theme_bw()
ggplotly(graf_2f)