[R] R을 활용한 미분과 적분 계산

반응형

 

이번 포스팅은 제가 학교에서 진행했던 수업자료의 일부로 R을 활용하여 미분과 적분을 하는 방법에 대해서 소개해드리겠습니다.

1. Mathematical functions

  • Anything to the power zero is 1 :
  • x0=1
  • One raised to any power is still 1 :
  • 1x=1
  • Infinity plus 1 is infinity :
  • +1=
  • One over infinity (the reciprocal of infinity, 1) is zero :
  • 1=0
  • A fraction raised to the power infinity is zero :
  • 0.99=0
  • Negative power are reciprocals :
  • xb=1xb
  • Functional powers are roots :
  • x1/3=3x
  • The base of natural logarithms e is 2.71828, so :
  • e=

1. 1. Logarithmic functions, Exponential functions

y=aln(bx)

y=aebx

  • 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π.
  • Recall that the full circle is 2π radians, so 1 radian = 360/2π = 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)=c0+c1x+c2x2+...+cnxn

  • y=2x2+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=2x2+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.04x30.6x2+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.04x40.6x3+2x2+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=ddx(f)=dfdx=dydx

  • 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 :
  • ddx(c)=0
  • Power :
  • ddx(xn)=nxn1
  • Log :
  • ddx(logx)=1x
  • Exponent :
  • ddx(ex)=ex
  • Linear combination :ddx(f1+f2)=df1dx+df2dxex) y=1+2x+3x2+4ex+5log(x)
  • dydx=2+6x+4ex+5x
  • ddx(c1f1+c2f2)=c1df1dx+c2df2dx
  • ddx(cf)=cdfdx

2. 2. Multiplication rule for differentiation

ddx(f×g)=dfdx×g+dgdx×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=xex, dfdx=ex+xex

2. 3. Chain rule for differentiation

f(x)=h(g(x))

dfdx=dhdg×dgdx

  • For example, the following rules can be obtained by applying chain rule to a logarithmic function :
  • ddxlogf(x)=f(x)f(x)

2. 4. Partial differentiation

f(x,y)=x2+xy+y2

fx(x,y)=fx=2x+y

fy(x,y)=fy=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)=5x2+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×(2×x)+10=10x+10
  • D(f, "x")                         # 함수 f를 X로 미분

Some complex examples

  • f(x)=ex+10x3+2x
    ## exp(x) + 10 * (3 * x^2) + 2
    • f(x)=ex+30x2+2
  • f <- expression(exp(x) + 10*x^3 + 2*x)
    D(f, "x")
  • f(x)=logx3+logx
    ## 3 * x^2/x^3 + 1/x
    • f(x)=3x+1x
  • f <- expression(log(x^3) + log(x))
    D(f, "x")
  • f(x)=log(x3+x+1)+ex2
    ## (3 * x^2 + 1)/(x^3 + x + 1) + exp(x^2) * (2 * x)
    • f(x)=3x2+1x3+x+1+2xex2
  • f <- expression(log(x^3 + x + 1) + exp(x^2))
    D(f, "x")
  • f(x)=eex+1+logx
    ## exp(exp(x + 2)) * exp(x + 2) + 1/x
    • f(x)=eex+2ex+2+1x
  • f <- expression(exp(exp(x+2)) + log(x))
    D(f, "x")
  • f(x,y)=eey+1+logx
    ## 1/x
    • fx(x,y)=1x
  • 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 install Deriv package.
  • library(Deriv)
  • In Deriv() function, it is similar to D() 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)×cos(x)
    ## cos(x)^2 - sin(x)^2
    • f(x)=cos2(x)sin2(x)
  • Deriv(sin(x)*cos(x), "x")
  • f(x)=x3+x2+1
    ## 3 * x^2 - 2/x^3
    • f(x)=3x22x3
  • 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 run integrate() 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

  1. f(x)=x31
  2. f(x)=log(x22)
  3. f(x)=e2x3
  4. f(x)=x4+3x32x2+4

반응형

'ETC' 카테고리의 다른 글

[R] 기계학습 과제...  (0) 2017.10.09
[R] R을 활용한 테일러 전개  (0) 2017.07.04
TAGS.

Comments