Define a procedure cubic that can be used together with the newtons-method procedure in expressions of the form
(newtons-method (cubic a b c) 1)to approximate zeros of the cubic x3 + ax2 + bx + c.
Section 1.3.4 introduces the concept of procedures as the returned values of other procedures. Let's use this concept, and the procedures
square
and cube
that are defined in the book, to produce cubic
:
(define (cubic a b c) (lambda (x) (+ (cube x) (* a (square x)) (* b x) c)))Now let's put this into action. There are various tools on the web that allow you to visualize and solve cubic polynomials. I picked one at random, plugged in a few numbers and came up with the cubic function: x3 - 5x2 + 3x + 1, which has roots at x = -0.23607, x = 1 and x = 4.23607. Let's see what we get:
> (newtons-method (cubic -5 3 1) -1) -0.23606797747438676 > (newtons-method (cubic -5 3 1) 1.1) 1.000000000077753 > (newtons-method (cubic -5 3 1) 4) 4.236067977501216As we can see, we get pretty accurate approximations.
No comments:
Post a Comment