'Understanding the question in a Racket programming assignment
The question is :
A univariate polynomial of order n is given by the following equation.
Pn (x) = anxn + . . . + a2x2 + a1x + a0
Here, ai are the coefficients of the polynomial, and x is its unique variable. You might implement a procedure poly-3 for computing the polynomial of order 3 of x as follows.
(define (poly-3 x a0 a1 a2 a3)
(+ a0 (* a1 x) (* a2 x x) (* a3 x x x)))
In poly-3, the coefficients and the variable are bundled together as arguments; and you would have to specify the coefficients each time you want to compute the same polynomial with different values of x.
Instead, implement the procedure make-poly-3 that generates a procedure that computes the polynomial for an arbitrary x.
(define (make-poly-3 a0 a1 a2 a3)
...)
(define my-poly-3
(make-poly-3 1 2 3 4))
(my-poly-3 2)
Next, write a function sum-poly-3-range which will sum up the results for calling my-poly-3 for the values in a range:
(define (sum-poly-3-range from to)
...)
(sum-poly-3-range 1 50)
I am not understanding what I need to do (I am not asking for the programming solution, just steps).
My confusions:
- Can't understand the workflow or say the steps I need to follow.
- How to pass coefficients for the polynomial? Should I generate randomly or should I use the constant values of a0, a1,a2,a3?
- When looping through the range should I use that value as x?
Solution 1:[1]
make-poly-3 is a procedure which takes four arguments, and which will return another procedure. The values of the four arguments it takes will be the values of the coefficients to the polynomial.
The procedure it returns will take a single argument, which will be the value of x at which the polynomial is to be evaluated.
So, for instance
(define linear (make-poly-3 0 1 0 0))
> (linear 2)
2
> (define squared (make-poly-3 0 0 1 0))
> (squared 2)
4
The sum-poly-3-range function uses whatever value my-poly-3 has (it 'uses my-poly-3 free' to use a bit of jargon), and evaluates it for every integer in a range which you give it, and works out the sum of the results.
So, as a simple example:
> (define my-poly-3 (make-poly-3 1 0 0 0))
> (sum-poly-3-range 1 50)
50
This is because (make-poly-3 1 0 0 0) returns a polynomial function which evaluates to 1 for all arguments (the constant term is the only non-zero term).
And
> (define my-poly-3 (make-poly-3 0 1 0 0))
> (sum-poly-3-range 1 50)
1275
because this polynomial just squares its argument.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ignis volens |
