[R] R을 활용한 미분과 적분 계산
반응형
이번 포스팅은 제가 학교에서 진행했던 수업자료의 일부로 R을 활용하여 미분과 적분을 하는 방법에 대해서 소개해드리겠습니다.
1. Mathematical functions
- Anything to the power zero is 1 :
- \[x^{0} = 1\]
- One raised to any power is still 1 :
- \[1^{x} = 1\]
- Infinity plus 1 is infinity :
- \[\infty + 1 = \infty\]
- One over infinity (the reciprocal of infinity, \(\infty^{-1}\)) is zero :
- \[\frac{1}{\infty} = 0\]
- A fraction raised to the power infinity is zero :
- \[0.99^{\infty} = 0\]
- Negative power are reciprocals :
- \[x^{-b} = \frac{1}{x^{b}}\]
- Functional powers are roots :
- \[x^{1/3} = \sqrt[3]{x}\]
- The base of natural logarithms \(e\) is 2.71828, so :
- \[e^{\infty} = \infty\]
1. 1. Logarithmic functions, Exponential functions
\[y = a \ln (bx)\]
\[y = a e^{bx}\]
- For example, \(a=1\) and \(b=1\)
-
x = seq(from = 0, to = 10, by = 0.1) y = log(x) z = exp(x) par(mfrow = c(1, 2)) plot(x, y, type = "l", main = expression(log(x)), lwd = 2) grid() plot(x, z, type = "l", main = expression(e^x), lwd = 2) grid()
1. 2. Trigonometric function
- Here are the sine, cosine and tangent functions of \(x\) over the range \(0\) to \(2\pi\).
- Recall that the full circle is \(2 \pi\) radians, so \(1\) radian = \(360/2\pi\) = \(57.29578\) degrees.
- \(\sin\) function, \(\cos\) function, \(\tan\) function
-
x = seq(from = 0, to = 2*pi, length.out = 100) y = sin(x) z = cos(x) w = tan(x) par(mfrow = c(2, 2)) plot(x, y, type = "l", main = expression(sin(x)), lwd = 2) abline(v = 0, h = 0, lty = 3) plot(x, z, type = "l", main = expression(cos(x)), lwd = 2) abline(v = 0, h = 0, lty = 3) plot(x, w, type = "l", ylim = c(-3, 3), main = expression(tan(x)), lwd = 2) abline(v = 0, h = 0, lty = 3)
1. 3. Polynomial functions
\[f(x) = c_{0} + c_{1}x + c_{2}x^{2} + ... + c_{n}x^{n}\]
- \(y = 2x^{2} + 5x + 2\)
-
x = seq(from = -10, to = 10, by = 0.1) y = 2*x^{2} + 5*x + 2 par(mfrow = c(1, 1)) plot(x, y, type = "l", main = expression(2*x^{2} + 5*x + 2), lwd = 2) grid()
- \(y = -2x^{2} + 5x + 2\)
-
x = seq(from = -10, to = 10, by = 0.1) y = -2*x^{2} + 5*x + 2 plot(x, y, type = "l", main = expression(-2*x^{2} + 5*x + 2), lwd = 2) grid()
- \(y = 0.04x^{3} - 0.6x^{2} + 4x + 2\)
-
x = seq(from = 0, to = 10, by = 0.1) y = 0.04*x^{3} - 0.6*x^{2} + 4*x + 2 plot(x, y, type = "l", main = expression(0.04*x^{3} - 0.6*x^{2} + 4*x + 2), lwd = 2) grid()
- \(y = 0.04x^{4} - 0.6x^{3} + 2x^{2} + 4x + 2\)
-
x = seq(from = 0, to = 10, by = 0.1) y = 0.04*x^{4} - 0.6*x^{3} + 2*x^{2} + 4*x + 2 plot(x, y, type = "l", main = expression(0.04*x^{4} - 0.6*x^{3} + 2*x^{2} + 4*x + 2), lwd = 2) grid()
1. 4. Basic mathematical operator in R
+
: addition## [1] 2
-
1 + 1
-
: subtraction## [1] 1
-
10 - 9
*
: multiplication## [1] 0
-
0 * 1000
/
: division## [1] 2
-
10 / 5
log()
: logarithm## [1] 0
log10(10)
## [1] 1
log2(2)
## [1] 1
log(10)
## [1] 2.302585
-
log(1)
exp()
: exponent## [1] 2.718282
log(exp(1))
## [1] 1
-
exp(1)
^
: power## [1] 100
10^2+1
## [1] 101
10^{2+1}
## [1] 1000
-
10^2
sqrt()
: square root## [1] 1
sqrt(2)
## [1] 1.414214
-
sqrt(1)
sin()
,cos()
,tan()
: trigonometrical function## [1] 0
cos(0)
## [1] 1
tan(45)
## [1] 1.619775
-
sin(0)
2. Differentiation
\[f^{'}= \frac{d}{dx} (f) = \frac{df}{dx} = \frac{dy}{dx}\]
- A differentiation is a type of transformation that takes a new function from original function.
- A new function created by a differential, called derivative function, represents the slope of the original function.
2. 1. Basic differential formula
- Constant :
- \[\frac{d}{dx}(c)=0\]
- Power :
- \[\frac{d}{dx}(x^{n}) = n x^{n-1}\]
- Log :
- \[\frac{d}{dx}(\log x)=\frac{1}{x}\]
- Exponent :
- \[\frac{d}{dx}(e^{x}) = e^{x}\]
- Linear combination :\[\frac{d}{dx}(f_1 + f_2) = \frac{df_1}{dx} + \frac{df_2}{dx}\]ex) \(y = 1 + 2x + 3x^{2} + 4e^{x} + 5\log(x)\)
- \(\frac{dy}{dx} = 2 + 6x + 4e^{x} + \frac{5}{x}\)
- \[\frac{d}{dx}(c_1 f_1 + c_2 f_2) = c_1 \frac{df_1}{dx} + c_2 \frac{df_2}{dx}\]
- \[\frac{d}{dx}(cf) = c \frac{df}{dx}\]
2. 2. Multiplication rule for differentiation
\[\frac{d}{dx}(f \times g) = \frac{df}{dx} \times g + \frac{dg}{dx} \times f\]
- When the function is multiplied by two functions, derivative of the function is gained to use the derivative of each individual function.
- ex) \(f = x e^{x}\), \(\frac{df}{dx} = e^{x} + xe^{x}\)
2. 3. Chain rule for differentiation
\[f(x) = h(g(x))\]
\[\frac{df}{dx} = \frac{dh}{dg} \times \frac{dg}{dx}\]
- For example, the following rules can be obtained by applying chain rule to a logarithmic function :
- \[\frac{d}{dx} \log{f(x)} = \frac{f^{'}(x)}{f(x)}\]
2. 4. Partial differentiation
\[f(x, y) = x^{2} + xy + y^{2}\]
\[f_{x}(x, y) = \frac{\partial f}{\partial x} = 2x + y\]
\[f_{y}(x, y) = \frac{\partial f}{\partial y} = x + 2y\]
3. Calculate derivatives using R function
- In this chapter, we will learn two convenient ways of various methods by using
R function
.
3. 1. D
function
- We calculate the derivative function by using
D()
function. - STEP1) Define the function to be differentiate by using
expression()
function.- \(f(x) = 5x^{2} + 10x + 5\)
f <- expression(5*x^2 + 10*x + 5)
- STEP2) Use
D()
function## 5 * (2 * x) + 10
- As a result, We obtain \(f^{'}(x) = 5 \times (2 \times x) + 10 = 10x + 10\)
-
D(f, "x") # 함수 f를 X로 미분
Some complex examples
- \(f(x) = e^{x} + 10x^{3} + 2x\)
## exp(x) + 10 * (3 * x^2) + 2
- \(f^{'}(x) = e^{x} + 30x^{2} + 2\)
-
f <- expression(exp(x) + 10*x^3 + 2*x) D(f, "x")
- \(f(x) = \log{x^{3}} + \log{x}\)
## 3 * x^2/x^3 + 1/x
- \(f^{'}(x) = \frac{3}{x} + \frac{1}{x}\)
-
f <- expression(log(x^3) + log(x)) D(f, "x")
- \(f(x) = \log(x^{3} + x + 1) + e^{x^{2}}\)
## (3 * x^2 + 1)/(x^3 + x + 1) + exp(x^2) * (2 * x)
- \(f^{'}(x) = \frac{3x^{2} + 1}{x^{3} + x + 1} + 2xe^{x^{2}}\)
-
f <- expression(log(x^3 + x + 1) + exp(x^2)) D(f, "x")
- \(f(x) = e^{e^{x+1}} + \log{x}\)
## exp(exp(x + 2)) * exp(x + 2) + 1/x
- \(f^{'}(x) = e^{e^{x+2}} e^{x+2} + \frac{1}{x}\)
-
f <- expression(exp(exp(x+2)) + log(x)) D(f, "x")
- \(f(x, y) = e^{e^{y+1}} + \log{x}\)
## 1/x
- \(f_{x}^{'}(x, y) = \frac{1}{x}\)
-
f <- expression(exp(exp(y+2)) + log(x)) D(f, "x")
3. 2. Deriv
packages
- This method is immediate and quick but to run
Deriv()
function, you must installDeriv
package. -
library(Deriv)
- In
Deriv()
function, it is similar toD()
function but does not need to specify a objective function.
Some examples
- \(f(x) = \sin(x) + \cos(x)\)
## cos(x) - sin(x)
- \(f^{'}(x) = \cos(x) - \sin(x)\)
-
Deriv(sin(x) + cos(x), "x")
- \(f(x) = \sin(x) \times \cos(x)\)
## cos(x)^2 - sin(x)^2
- \(f^{'}(x) = \cos^{2}(x) - \sin^{2}(x)\)
-
Deriv(sin(x)*cos(x), "x")
- \(f(x) = x^{3} + x^{-2} + 1\)
## 3 * x^2 - 2/x^3
- \(f^{'}(x) = 3x^{2} - \frac{2}{x^{3}}\)
-
Deriv(x^(3) + x^(-2) + 1, "x")
4. Calculate integration using R function
- The integral of the function is one of the most important computation and is to calculate the extent and volumne of the function.
4. 1. integrate()
function
- We define the formula(objective function) by using
function() {expression}
function to runintegrate()
function.## function(x) {x^3 - 7*x^2 + 13*x + 12} ## <environment: 0x000000001606b770>
-
f <- function(x) {x^3 - 7*x^2 + 13*x + 12} f
- Then, use
integrate()
function. But it must be established lower bound and upper bound.## 50.25 with absolute error < 5.6e-13
-
integrate(f, lower = 1, upper = 4)
5. Exercise
- \(f(x) = x^{3} - 1\)
- \(f(x) = \log(x^{2} - 2)\)
- \(f(x) = e^{2x^{3}}\)
- \(f(x) = x^{4} + 3x^{3} - 2x^{2} + 4\)
반응형
'ETC' 카테고리의 다른 글
[R] 기계학습 과제... (0) | 2017.10.09 |
---|---|
[R] R을 활용한 테일러 전개 (0) | 2017.07.04 |
TAGS.