~rhcarvalho/+junk/racket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#lang racket

;; This script plots a Lagrange interpolation in a rectangular region.

(require version/utils)
(when (version<? (version) "5.2")
  (error "This script requires Racket 5.2 or newer"))

(require plot) ; PLoT from Racket 5.2

;; v1 through v4 are the values at the corners of the rectangle.
(define-values (v1 v2 v3 v4) (values 3 1 4 9))
;; L1 and L2 are the dimensions of the rectangle.
(define-values (L1 L2) (values 2 2))

;; Returns for each (x,y) the interpolated value.
(define (z x y)
  (let ([x/L1 (/ x L1)]
        [y/L2 (/ y L2)])
    (+ (* (+ 1/2 x/L1) (+ 1/2 y/L2) v1)
       (* (- 1/2 x/L1) (+ 1/2 y/L2) v2)
       (* (+ 1/2 x/L1) (- 1/2 y/L2) v4)
       (* (- 1/2 x/L1) (- 1/2 y/L2) v3))))

(plot3d (contour-intervals3d z -1 1 -1 1)
        #:title "x, y interpolation using Lagrange polynomials"
        #:x-label "x" #:y-label "y" #:z-label "z")