Skip to content

Instantly share code, notes, and snippets.

@vedantk
Created January 26, 2011 04:39
Show Gist options
  • Save vedantk/796235 to your computer and use it in GitHub Desktop.
Save vedantk/796235 to your computer and use it in GitHub Desktop.
Taylor Polynomials: generate them in Racket
(require plot)
(define (factorial n)
(if (< n 2) 1 (* n (factorial (- n 1)))))
(define (taylor-poly func n c)
(define (calc-coeff dfn level)
(/ (dfn c) (factorial level)))
(define (find-coeff fn level coeffs)
(if (= level (+ n 1))
coeffs
(find-coeff (derivative fn) (+ level 1)
(append coeffs (list (calc-coeff fn level))))))
(find-coeff func 0 '()))
(taylor-poly sin 4 0)
; '(0 1.0 0.0 -0.5551115123125783 0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment