I'm trying to build a Taylor series in lisp, as the following image.
1 + x^1/1! + x^2/2! + x^3/3!.....etc
the power and factorial functions are already implemented in order to use them in taylor function.
currently i wrote the following initial code to solve the equation.
(defun taylor(x n)
(if (= n 0) 1
(+ (/ (power x n) (factorial n)) (taylor(x (- n 1))))))
using this code will cause the following error
error: unbound function - X
im new in lisp, so any help will be appreciated :D
You have an extra parenthesis in front of taylor
; i.e. you need to call it as (taylor x n)
and not (taylor (x n))
.
In general, errors like these are much easier to spot if you indent code appropriately, e.g. the following
(defun factorial (n)
(if (= n 1)
1
(* n (factorial (1- n)))))
(defun power (x n)
(if (= n 1)
x
(* x (power x (1- n)))))
(defun taylor (x n)
(if (= n 0)
1
(+ (/ (power x n)
(factorial n))
(taylor x (- n 1)))))