Saturday, February 27, 2016

Doing Basic Calculus Using R!

Doing Basic Calculus Using R!
Differentiation Rules/Rules for Calculating Derivatives
  • Constant: f'(c) = 0, here c as a constant
  • Scalar Multiple: f'[cf(x)] = cf'(x)
  • Sum and Difference: [f(x) ± g(x)]' = f' (x) ± g' (x)
  • Product: [f(x) * g(x)]' = f'(x) * g(x) + f(x) * g'(x)
  • Quotient: [f(x) / g(x)]' = [g(x) * f'(x) - f(x) * g'(x)] / g(x)2
  • Power: f'(xn) = n * x(n-1)
  • Chain Rule: [f(g(x)]' = f'(g(x)) * g'(x)
  • Exponential: f'(ex) = ex                  Arbitrary base: f'(bx) = bx * lnb
  • Logarithmic: f'(ln|x|) = 1/x                  Arbitrary base: f'b(logx) = 1/(x lnb)
Calculating Derivative and Integration Using R!
  • R can symbolically find the derivative of any function by using the function D() with function expression(). R knows how to use the chain rule as well.
    • First derivative:     D(expression(x^2), "x") ===> 2 * x
    • Higher derivative: D(D(expression(x^2),"x"), "x") ===> 2
    • Partial derivative: D(expression((y-x)/y),"x") ===> -(1/y) and D(expression((y-x)/y),"y") ===> 1/y - (y - x)/y^2, which is equal to x/y^2
    • with the eval() function, you can get the value using particular values of its parameters: x =10; eval(D(expression(x^2), "x"))  ===> 20
    • D(expression(pnorm(x)),"x") ===> dnorm(x)
    • D(expression(dnorm(x)),"x") ===> -(x * dnorm(x))
  • R  can numerically perform one dimentsional integration using function integrate()
    • integrate(dnorm,-Inf,Inf) ===> 1 with absolute error < 9.4e-05
    • integrate(dnorm,-2.58,2.58) ===> 0.99012 with absolute error < 1.9e-08
    • integrate(function(x) {x^3 + x}, 0, 1) ===> 0.75 with absolute error < 8.3e-15
  • Other differentiation related R packages
    • Deriv is for symbolic differentiation.
    • Ryacas allows R users to access the yacas computer algebra system that does an excellent job of differentiation.
  • Use R to Compute Numerical Integrals
Online Calculators: 
Taylor's Series is a series expansion of a function near a point. A real function f(x) which is close to a point a can be estimated as:
  • f(x) =(f(n)(a)/n! * (x - a)n
  • If a = 0, the expansion is known as a Maclaurin series.
  • Mathematical Annotation to write math symbols and expressions in R graphics (cheat sheet). 

No comments: