Yes, I can see at least two significant errors in my description.  The first is 
in the sequence definitions.  They should be

 list of constants (a0, a1… an) and a list of x-values (x0, x1,….x(n-1)) 
Similarly, the def of p(x) should read

p(x) = a0 + a1(x - x0) + a2(x - x0)(x - x1) + a3(x - x0)(x - x1) (x - x2) + ….+ 
an(x - x0)(x - x1)…(x - x(n-1)), not

p(x) = a0 + a1(x - x0) + a2(x - x0)(x - x1) + a3(x - x0)(x - x1) (x - x2) + ….+ 
an(x - x0)(x - x1)…(x - xn).  Thank you for correcting me! 

Here are some examples:

Data Defs:
x is a real number
lox is (listof x)

;;NewtonPx : (lox lox -> (x -> x)
;; accepts two list of numbers, returns a function equivalent to the 
interpolating polynomial in Netwton form that accepts a number and returns a 
number
(define (NewtonPx lox loa) ….)

Ex #1
(define lox1 ‘(1 2))
(define loa1 (4 5 6))

(NewtonPx lox1 loa1) -> (lambda (x) (+ 4 (* 5(- x 1)) (* 6 (- x 1) (- x 2)))) 
and ( (NewtonPx lox1 loa1) 2) ->  9

Ex. #2
(define lox2 '(4 2 7))
(define loa2 ‘(9 8 6 3))

(NewtonPx lox1 loa1) -> (lambda (x) (+ 9 (* 8 (- x 4)) (* 6 (- x 4) (- x 2)) (* 
3 (- x 4) (- x 2) (- x 7)))) and  ( (NewtonPx lox2 loa2) 2) -> -7

I realize now that the function I’m after doesn’t require anything as fancy as 
macros — judicious use of loops should do.  Still, I’m wondering if there is a 
macro-based solution that I can use to study as an entry point to this subject. 
 

Cheers, 
DY

 










> On Nov 6, 2015, at 6:43 PM, Matthias Felleisen <[email protected]> wrote:
> 
> 
>> On Nov 6, 2015, at 9:07 PM, dyrueta <[email protected]> wrote:
>> 
>> Hi All --
>> 
>> I'm hoping the answer to the question below will serve as an intro to 
>> macros, a subject I've been interested in for awhile but haven't had time to 
>> look into much.  Here goes, fingers crossed:
>> 
>> Given a list of constants (a0, a1… an) and a list of x-values (x0, x1,….xn), 
>> I want to design a function in Racket that produces an interpolating 
>> polynomial in Newton form, or p(x) = a0 + a1(x - x0) + a2(x - x0)(x - x1) + 
>> a3(x - x0)(x - x1) (x - x2) + ….+ an(x - x0)(x - x1)…(x - xn).   My 
>> understanding of functions that produce functions is limited to what’s in 
>> HtDP, and this example (I think) goes beyond that (and hopefully into 
>> macros).   
>> 
>> Any tips or advice would be much appreciated!
> 
> 
> Let’s assume I don’t know what you’re talking about. But we do know together 
> that HtDP requests examples for problems that you’re not quite familiar with. 
> So we work through 3 instances of this problem: 
> 
> #lang racket
> 
> (require plot)
> 
> (define ((make-p-0 a0 a1) x0 x1)
>  (define (p x)
>    (+ a0
>       (* a1 (- x x0))))
>  p)
> 
> [plot (function ((make-p-0 1 2) 3 4)) #:x-min -10 #:x-max +10]
> 
> (define ((make-p-1 a0 a1 a2) x0 x1 x2)
>  (define (p x)
>    (+ a0
>       (* a1 (- x x0))
>       (* a2 (- x x0) (- x x1))))
>  p)
> 
> [plot (function ((make-p-1 1 2 3) 3 4 5)) #:x-min -10 #:x-max +10]
> 
> (define ((make-p-2 a0 a1 a2 a3) x0 x1 x2 x3)
>  (define (p x)
>    (+ a0
>       (* a1 (- x x0))
>       (* a2 (- x x0) (- x x1))
>       (* a3 (- x x0) (- x x1) (- x x2))))
>  p)
> 
> [plot (function ((make-p-2 1 2 3 4) 3 4 5 6)) #:x-min -10 #:x-max +10]
> 
> I threw in the plots for fun. Now I think one of two things is wrong: 
> 
> — your description (making the a-sequence as long as the x-sequence) 
> — or my understanding of your description. 
> 
> Correct me.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to