Course CMPSCI 287 Assignment 3

Due 11:59 PM, 7OCT 99 as hwk3.scm in your cs287 directory

Recall that the polynomial function p for which
    p(x) =  a0 +  a1 x +  a2 x2 ... an xn
can be represented by the Scheme list
    (a0 a1 a2 ... an)

(1) Write a function lam_poly for which the application (lam_poly p) produces a lambda-expression that is the definition of the polynomial-function as a Scheme function

Below is a possible example - you may produce a solution that is more or less optimised than it, as long as it is correct.

    (example
        '(lam_poly '(3 4 5))
        '(lambda (x) (+ 3 (* 4 (expt x 1)) (* 5 (expt x 2))

(2) The built-in Scheme function eval provides a way of compiling a Scheme expression. The call:
  (eval expr (nearest-repl/environment))
will evaluate an expression in the "current environment". For example: (eval '(+ 2 3) (nearest-repl/environment)) evaluates to 5.

Write a function fun_poly for which the application (fun_poly p) produces a Scheme procedure object

    (example
        '((fun_poly '(3 4 5)) 2)
         (+ 3 (* 4 (expt 2 1)) (* 5 (expt 2 2)))
    )
[Note that from Naomi Nag's point of view, this is a level-1 problem.]
(3) The Newton-Raphson method for the iterative solution of non-linear equations makes use of the equation
    f(x+t) = f(x) + t f'(x) + e  ------------------- (1)
where f' denotes the derivative of f and e is an error term quadratic in t [for analytic functions]. Setting
    f(x+t) = 0
we obtain
    t = -f(x)/f'(x)  - e/f'(x)  -------------------- (2)
We can use (2) as a basis for improving a approximate solution xi of f(x) = 0 to a better solution xi+1 by
    xi+1 =  xi - f(x)/f'(x)
Write a Scheme function solve_poly for which the application (solve_poly p x0 eps) will evaluate to an approximation to a root of the polynomial equation p = 0 provided that x0 is sufficiently close to a root of p. The parameter eps is used to determine the accuracy of the root - you should regard a solution as adequate when
    abs(xi+1 -  xi ) < eps
Do not forget that you should use floating point contagion to ensure that your program doesn't try to produce a rational answer (which if correct, would be produced slowly, and wouldll be meaningless to the average user).

The simplest example should converge in one iteration:

 (solve_poly '(1 1) 0 0.000001)
to give the root -1.0
    (solve_poly '(1 0 -1) 0.5 0.000001)
Should give the root 1.0 (approximately) while
    (solve_poly '(1 0 -1) -0.5 0.000001)
Should give the root -1.0. Finally, to get serious, try to find the roots of the polynomial defined by the Scheme list
    '(2 -1 2 1)