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.
- \(f(x)=e^{2x}-cos 2x\), \(x\in [0,2]\)
<- function(x){exp(2*x)-cos(2*x)}
f<- seq(from=0, to=2, length.out=100)
x<- f(x)
y
<- ggplot()+
graf_1ageom_line(aes(x, y), color="yellow", size=.5)+
theme_bw()
ggplotly(graf_1a)
<- function(x){2*exp(2*x)+2*sin(2*x)}
der_exacta .25 <- fderiv(f, x, n=1, h=1.25, method = "central")
der_h1<- fderiv(f, x, n=1, h=1, method = "central")
der_h1 .75 <- fderiv(f, x, n=1, h=0.75, method = "central")
der_h<- fderiv(f, x, n=1, h=0, method = "central")
der_h0
<- ggplot()+
graf_der_1a 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
.25 <-abs (der_exacta(x)-der_h1.25)
error_h1ggplot()+
geom_line(aes(x, error_h1.25), color="brown")+
theme_bw()
<-abs (der_exacta(x)-der_h0)
error_h0
ggplot()+
geom_line(aes(x, error_h0), color="brown", size=1.2)+
theme_bw()
- \(f(x)=log(x+2)-(x+1)^2\), \(x\in [0,5]\)
<- function(x){log(x+2)-(x+1)^2}
f<- seq(from=0, to=5, by=.005)
x<- f(x)
y
<- ggplot()+
graf_1bgeom_line(aes(x, y), color="red", size=.5)+
theme_bw()
ggplotly(graf_1b)
<- function(x){1/(x+2)-2*(x+1)}
der_exacta .5 <- fderiv(f, x, n=1, h=1.5, method = "central")
der_h1<- fderiv(f, x, n=1, h=1, method = "central")
der_h1 .5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h<- fderiv(f, x, n=1, h=0, method = "central")
der_h0
<- ggplot()+
graf_der_1b 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
.5 <-abs (der_exacta(x)-der_h1.5)
error_h1ggplot()+
geom_line(aes(x, error_h1.5), color="brown")+
theme_bw()
<-abs (der_exacta(x)-der_h0)
error_h0
ggplot()+
geom_line(aes(x, error_h0), color="brown", size=1.2)+
theme_bw()
- \(f(x)=x\, sen\,x+x^2cos\,x\), \(x\in [0,\pi]\)
<- function(x){x*sin(x)+x^2*cos(x)}
f<- seq(from=0, to=pi, length.out=100)
x<- f(x)
y
<- ggplot()+
graf_1cgeom_line(aes(x, y), color="grey", size=.5)+
theme_bw()
ggplotly(graf_1c)
<- function(x){3*x*cos(x)+(1-x^2)*sin(x)}
der_exacta <- fderiv(f, x, n=1, h=1, method = "central")
der_h_1 .5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h_0<- fderiv(f, x, n=1, h=0, method = "central")
der_h_0
<- ggplot()+
graf_der_1c 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\)
<-abs (der_exacta(x)-der_h_1)
error_h1 ggplot()+
geom_line(aes(x, error_h1), color="brown")+
theme_bw()
<-abs (der_exacta(x)-der_h_0)
error_h0 ggplot()+
geom_line(aes(x, error_h0), color="brown", size=1.2)+
theme_bw()
- \(f(x)=(cos\,3x)^2-e^{2x}\), \(x\in [0,\pi/2]\)
<- function(x){cos(3*x)^2-exp(2*x)}
f<- seq(from=0, to=pi/2, length.out=100)
x<- f(x)
y
<- ggplot()+
graf_1dgeom_line(aes(x, y), color="blue", size=.5)+
theme_bw()
ggplotly(graf_1d)
<- function(x){-3*sin(6*x)-exp(2*x)*2}
der_exacta <- fderiv(f, x, n=1, h=1, method = "central")
der_h_1 .5 <- fderiv(f, x, n=1, h=0.5, method = "central")
der_h_0<- fderiv(f, x, n=1, h=0, method = "central")
der_h_0
<- ggplot()+
graf_der_1d 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
<-abs (der_exacta(x)-der_h_1)
error_h1 ggplot()+
geom_line(aes(x, error_h1), color="brown")+
theme_bw()
<-abs (der_exacta(x)-der_h_0)
error_h0 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}\]
<-function(x){x^4}
f<- seq(from=.5, to=1, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2a 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
::integral(f, 0.5, 1, method= "Kronrod") pracma
## [1] 0.19375
Método: Clenshaw
::integral(f, 0.5, 1, method= "Clenshaw") pracma
## [1] 0.19375
Método: Simpson
::integral(f, .5, 1, method= "Simpson") pracma
## [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}\]
<-function(x){2/(x-4)}
f<- seq(from=0, to=.5, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2b 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
::integral(f, 0, .5, method= "Kronrod") pracma
## [1] -0.2670628
Método: Clenshaw
::integral(f, 0, .5, method= "Clenshaw") pracma
## [1] -0.2670628
Método: Simpson
::integral(f, 0, .5, method= "Simpson") pracma
## [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}\]
<-function(x){x^2*log(x)}
f<- seq(from=1, to=1.5, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2c 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
::integral(f, 1, 1.5, method= "Kronrod") pracma
## [1] 0.1922594
Método: Clenshaw
::integral(f, 1, 1.5, method= "Clenshaw") pracma
## [1] 0.1922594
Método: Simpson
::integral(f, 1, 1.5, method= "Simpson") pracma
## [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}\]
<-function(x){x^2*exp(-x)*log(x)}
f<- seq(from=0, to=1, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2d 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
::integral(f, 0, 1, method= "Kronrod") pracma
## [1] -0.06471696
Método: Clenshaw
::integral(f, 0, 1, method= "Clenshaw") pracma
## [1] -0.06471696
Método: Simpson
::integral(f, 0, 1, method= "Simpson") pracma
## [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}\]
<-function(x){(2*x)/x^2-4}
f<- seq(from=1, to=1.6, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2e 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
::integral(f, 1, 1.6, method= "Kronrod") pracma
## [1] -1.459993
Método: Clenshaw
::integral(f, 1, 1.6, method= "Clenshaw") pracma
## [1] -1.459993
Método: Simpson
::integral(f, 1, 1.6, method= "Simpson") pracma
## [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}\]
<-function(x){exp(3*x)*sin(2*x)}
f<- seq(from=0, to=pi/4, length.out=100)
x<- f(x)
y
<-ggplot()+
graf_2f 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
::integral(f, 0, pi/4, method= "Kronrod") pracma
## [1] 2.588629
Método: Clenshaw
::integral(f, 0, pi/4, method= "Clenshaw") pracma
## [1] 2.588629
Método: Simpson
::integral(f, 0, pi/4, method= "Simpson") pracma
## [1] 2.588629
Método: Newton-Cotes
2 nodos
cotes(f, 0, pi/4)
## [1] 2.588629