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

Análisis Numérico.

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="yellow", size=.5)+
  theme_bw()

ggplotly(graf_1a)
der_exacta <- function(x){2*exp(2*x)+2*sin(2*x)}
der_h1.25 <- fderiv(f, x, n=1, h=1.25, method = "central")
der_h1 <- fderiv(f, x, n=1, h=1, method = "central")
der_h.75 <- fderiv(f, x, n=1, h=0.75, method = "central")
der_h0 <- fderiv(f, x, n=1, h=0, method = "central")

graf_der_1a <- ggplot()+
  geom_line(aes(x, der_exacta(x)), color= "red", size=1.2) +
  geom_line(aes(x, der_h1.25), color= "blue", size=1) +
  geom_line(aes(x, der_h1), color= "pink") +
  geom_line(aes(x, der_h.75), color= "green") +
  geom_line(aes(x, der_h0), color= "purple") +

  theme_bw()

ggplotly(graf_der_1a)

Se puede observar que la curva purpura se aproxima más a la función de \(f(x)=e^{2x}-cos 2x\) que es donde h equivale a un tamaño de paso de 0

error_h1.25 <-abs (der_exacta(x)-der_h1.25)
ggplot()+
  geom_line(aes(x, error_h1.25), color="brown")+
  theme_bw()

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

ggplot()+
  geom_line(aes(x, error_h0), color="brown", 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, by=.005)
y<- f(x)

graf_1b<- ggplot()+
  geom_line(aes(x, y), color="red", size=.5)+
  theme_bw()

ggplotly(graf_1b)
der_exacta <- function(x){1/(x+2)-2*(x+1)}
der_h1.5 <- fderiv(f, x, n=1, h=1.5, method = "central")
der_h1 <- fderiv(f, x, n=1, h=1, method = "central")
der_h.5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h0 <- fderiv(f, x, n=1, h=0, method = "central")

graf_der_1b <- ggplot()+
  geom_line(aes(x, der_exacta(x)), color= "red", size=1.25) +
  geom_line(aes(x, der_h1.5), color= "gold", size=1) +
  geom_line(aes(x, der_h1), color= "pink") +
  geom_line(aes(x, der_h.5), color= "green") +
  geom_line(aes(x, der_h0), color= "purple") +

  theme_bw()

ggplotly(graf_der_1b)

La línea amarilla con una reducción en un tamaño de paso de 1.5, sin embargo, es solamente una línea paralela a las de otros tamaños por lo que a paser de las diferentes reducciones la aproximación será bastante buena

error_h1.5 <-abs (der_exacta(x)-der_h1.5)
ggplot()+
  geom_line(aes(x, error_h1.5), color="brown")+
  theme_bw()

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

ggplot()+
  geom_line(aes(x, error_h0), color="brown", size=1.2)+
  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="grey", size=.5)+
  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.5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h_0 <- fderiv(f, x, n=1, h=0, method = "central")

graf_der_1c <- ggplot()+
  geom_line(aes(x, der_exacta(x)), color= "red", size=1.2) +
  geom_line(aes(x, der_h_1), color= "gold") +
  geom_line(aes(x, der_h_0.5), color= "pink") +
  geom_line(aes(x, der_h_0), color= "brown") +

  theme_bw()

ggplotly(graf_der_1c)

Si reducimos el tamaño de paso a 0 vemos que la aproximación es practicamente total para la función \(f(x)=x\, sen\,x+x^2cos\,x\)

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="brown", 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="blue", size=.5)+
  theme_bw()

ggplotly(graf_1d)
der_exacta <- function(x){-3*sin(6*x)-exp(2*x)*2}
der_h_1 <- fderiv(f, x, n=1, h=1, method = "central")
der_h_0.5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h_0 <- fderiv(f, x, n=1, h=0, method = "central")

graf_der_1d <- ggplot()+
  geom_line(aes(x, der_exacta(x)), color= "red", size=1.2) +
  geom_line(aes(x, der_h_1), color= "blue") +
  geom_line(aes(x, der_h_0.5), color= "pink") +
  geom_line(aes(x, der_h_0), color= "green") +

  theme_bw()

ggplotly(graf_der_1d)

Se observa tanto en la gráfica superior como en la de error que la aproximación de las rectas no es tan buena para la función \(f(x)=(cos\,3x)^2-e^{2x}\) a excepción de cuando h vale 0

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="brown", size=1.2)+
  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=.5, to=1, length.out=100)
y<- f(x)

graf_2a <-ggplot()+
  geom_line(aes(x, y), color="red")+
  geom_area(aes(x, y), fill="blue", alpha=.5)+
  theme_bw()

ggplotly(graf_2a)

Función integral

Método: Kronrod

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

Método: Clenshaw

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

Método: Simpson

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

Método: Newton-Cotes

2 nodos

cotes(f, .5, 1)
## [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=.5, length.out=100)
y<- f(x)

graf_2b <-ggplot()+
  geom_line(aes(x, y), color="red")+
  geom_area(aes(x, y), fill="blue", alpha=.5)+
  theme_bw()

ggplotly(graf_2b)

Función integral

Método: Kronrod

pracma::integral(f, 0, .5, method= "Kronrod")
## [1] -0.2670628

Método: Clenshaw

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

Método: Simpson

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

Método: Newton-Cotes

2 nodos

cotes(f, 0, .5)
## [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="green")+
  geom_area(aes(x, y), fill="brown", alpha=.5)+
  theme_bw()

ggplotly(graf_2c)

Función integral

Método: Kronrod

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

Método: Clenshaw

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

Método: Simpson

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

Método: Newton-Cotes

2 nodos

cotes(f, 1, 1.5)
## [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="red")+
  geom_area(aes(x, y), fill="blue", alpha=.5)+
  theme_bw()

ggplotly(graf_2d)
## Warning: Removed 1 rows containing missing values (position_stack).

Función integral

Método: Kronrod

pracma::integral(f, 0, 1, method= "Kronrod")
## [1] -0.06471696

Método: Clenshaw

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

Método: Simpson

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

Método: Newton-Cotes

2 nodos

cotes(f, 0, 1)
## [1] NaN

\[\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="gold")+
  geom_area(aes(x, y), fill="black", alpha=.5)+
  theme_bw()

ggplotly(graf_2e)

Función integral

Método: Kronrod

pracma::integral(f, 1, 1.6, method= "Kronrod")
## [1] -1.459993

Método: Clenshaw

pracma::integral(f, 1, 1.6, method= "Clenshaw")
## [1] -1.459993

Método: Simpson

pracma::integral(f, 1, 1.6, method= "Simpson")
## [1] -1.459993

Método: Newton-Cotes

2 nodos

cotes(f, 1, 1.6)
## [1] -1.459993

\[\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="green")+
  geom_area(aes(x, y), fill="purple", alpha=.3)+
  theme_bw()

ggplotly(graf_2f)

Función integral

Método: Kronrod

pracma::integral(f, 0, pi/4, method= "Kronrod")
## [1] 2.588629

Método: Clenshaw

pracma::integral(f, 0, pi/4, method= "Clenshaw")
## [1] 2.588629

Método: Simpson

pracma::integral(f, 0, pi/4, method= "Simpson")
## [1] 2.588629

Método: Newton-Cotes

2 nodos

cotes(f, 0, pi/4)
## [1] 2.588629