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]\)
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="#235ADE", 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="orange")+
geom_line(aes(x,der_h_0.05), color="pink")+
geom_line(aes(x,der_h_0), color="green")+
theme_bw()
ggplotly(graf_der_1a)error_h1 <- abs(der_exacta(x)-der_h_1)
ggplot()+
geom_line(aes(x, error_h1), color="forestgreen")+
theme_bw()error_h0 <- abs(der_exacta(x)-der_h_0)
ggplot()+
geom_line(aes(x, error_h0), color="firebrick", size=1.2)+
theme_bw()- \(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="#235ADE")+
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="black")+
geom_line(aes(x,der_h_1.5), color="red")+
geom_line(aes(x,der_h_1), color="orange")+
geom_line(aes(x,der_h_0.05), color="pink")+
geom_line(aes(x,der_h_0), color="green")+
theme_bw()
ggplotly(graf_der_1b)error_h1 <- abs(der_exacta(x)-der_h_1.5)
ggplot()+
geom_line(aes(x, error_h1), color="forestgreen")+
theme_bw()error_h0 <- abs(der_exacta(x)-der_h_0)
ggplot()+
geom_line(aes(x, error_h0), color="firebrick", size=1.2)+
theme_bw()- \(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="#235ADE", 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="black")+
geom_line(aes(x,der_h_1), color="orange")+
geom_line(aes(x,der_h_0.05), color="pink")+
geom_line(aes(x,der_h_0), color="green")+
theme_bw()
ggplotly(graf_der_c)error_h1 <- abs(der_exacta(x)-der_h_1)
ggplot()+
geom_line(aes(x, error_h1), color="forestgreen")+
theme_bw()error_h0 <- abs(der_exacta(x)-der_h_0)
ggplot()+
geom_line(aes(x, error_h0), color="firebrick", size=1.2)+
theme_bw()- \(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="#235ADE", size=1)+
theme_bw()
ggplotly(graf_1d)der_exacta <- function (x){-3*sin(6*x)-2*exp(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_1d <- ggplot()+
geom_line(aes(x,der_exacta(x)), color="black")+
geom_line(aes(x,der_h_1), color="orange")+
geom_line(aes(x,der_h_0.05), color="pink")+
geom_line(aes(x,der_h_0), color="green")+
theme_bw()
ggplotly(graf_der_1d)error_h1 <- abs(der_exacta(x)-der_h_1)
ggplot()+
geom_line(aes(x, error_h1), color="forestgreen")+
theme_bw()error_h0 <- abs(der_exacta(x)-der_h_0)
ggplot()+
geom_line(aes(x, error_h0), color="firebrick", 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=0.5, to=1, length.out=100)
y <- f(x)
graf_2a <- ggplot()+
geom_line(aes(x,y), color="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.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, 0.5,1, method="Simpson")## [1] 0.19375
Función Newton-Cotes
Nodos=8
cotes(f, 0.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=0.5, length.out=100)
y <- f(x)
graf_2b <- ggplot()+
geom_line(aes(x,y), color="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.5)+
theme_bw()
ggplotly(graf_2b)Función integral
Método: Kronrod
pracma::integral(f, 0,0.5, method="Kronrod")## [1] -0.2670628
Método: Clenshaw
pracma::integral(f, 0,0.5, method="Clenshaw")## [1] -0.2670628
Método: Simpson
pracma::integral(f, 0,0.5, method="Simpson")## [1] -0.2670628
Función Newton-Cotes
Nodos=8
cotes(f, 0, 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="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.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
Función Newton-Cotes
Nodos=8
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="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.5)+
theme_bw()
ggplotly(graf_2d)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
Función Newton-Cotes
Nodos=8
cotes(f, 0, 1)## [1] NaN
##Este método no ha funcionado para calcular esta integral debido a la forma en la que ha sido programado.
\[\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="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.5)+
theme_bw()
ggplotly(graf_2e)Función integral
Método: Kronrod
pracma::integral(f, 1,1.6, method="Kronrod")## [1] -0.7339692
Método: Clenshaw
pracma::integral(f, 1,1.6, method="Clenshaw")## [1] -0.7339692
Método: Simpson
pracma::integral(f, 1,1.6, method="Simpson")## [1] -0.7339692
Función Newton-Cotes
Nodos=8
cotes(f, 1, 1.6)## [1] -0.7339692
\[\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="#235ADE", size=1)+
geom_area(aes(x,y), fill="red", alpha=0.5)+
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
Función Newton-Cotes
Nodos=8
cotes(f, 0, pi/4)## [1] 2.588629