File: test-taylor_series.1.R

package info (click to toggle)
r-cran-lambda.r 1.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 284 kB
  • sloc: sh: 9; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,132 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
rm(list=ls())

compare <- function(a,b, xs) {
  plot(xs, a(xs), type='l')
  lines(xs, b(xs), type='l', col='blue')
  invisible()
}

# f <- taylor(sin, pi)
# xs <- seq(2,4.5,0.02)
# compare(sin,f, xs)
#
# p <- function(x) x^4 + 3 * (x-2)^3 - 2 * x^2 + 1
# p1 <- function(x) 4*x^3 + 9*(x-2)^2 - 4*x
# p2 <- function(x) 12*x^2 + 18*(x-2) - 4
# p3 <- function(x) 24*x + 18
#
# f <- taylor(p, 1)
# xs <- seq(-5,5,0.02)
# compare(p,f, xs)
# 
# f(x) ~ f(a) + f'(a) * (x - a) + f''(a) / 2! * (x - a)^2 + ...
assert('taylor_series_1', {
  fac(1) %as% 1
  fac(n) %when% { n > 0 } %as% { n * fac(n - 1) }
  seal(fac)

  # TODO: Implement this properly for k > 2
  d(f, 1, h=10^-9) %as% function(x) { (f(x + h) - f(x - h)) / (2*h) }
  d(f, 2, h=10^-9) %as% function(x) { (f(x + h) - 2*f(x) + f(x - h)) / h^2 }
   
  taylor(f, a, step=2) %as% taylor(f, a, step, 1, function(x) f(a))
  taylor(f, a, 0, k, g) %as% g
  taylor(f, a, step, k, g) %as% {
    df <- d(f,k)
    g1 <- function(x) { g(x) + df(a) * (x - a)^k / fac(k) }
    taylor(f, a, step-1, k+1, g1)
  }

  f <- taylor(sin, pi)
  v <- f(3.1)
  all.equal(v, sin(3.1), tolerance=0.01)
})