-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExperimentingWithPolyInterp.rkt
54 lines (42 loc) · 1.99 KB
/
ExperimentingWithPolyInterp.rkt
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#lang racket
(require plot
"Matrix_List_Helpers.rkt"
"Linear_Systems.rkt"
"Polynomial_Ops.rkt"
"Polynomial_Interpolation.rkt")
;;--This file experiments with Polynomial Interpolation through an example
;; Look how the polynomial fitted through the points changes depending on which
;; points are used in the approximation
(define POINTS (list '(5 1.226) '(10 1.498) '(15 1.822) '(20 2.138) '(30 2.662)
'(40 2.840) '(50 2.807) '(60 2.542) '(70 2.210) '(80 1.877)
'(90 1.539) '(100 1.201)))
(define 6-POINTS (map (λ (i) (list-ref POINTS i)) (list 1 3 5 7 9 11)))
(define 6PT-x (map (λ (pt) (first pt)) 6-POINTS))
(define 6PT-y (map (λ (pt) (second pt)) 6-POINTS))
(define Others (filter (λ (pt) (not (member pt 6-POINTS))) POINTS))
(define Others-x (map (λ (pt) (first pt)) Others))
(define Others-y (map (λ (pt) (second pt)) Others))
(define All-x (map (λ (x) (first x)) POINTS))
(define All-y (map (λ (x) (second x)) POINTS))
(define P6 (calc-poly 6-POINTS))
(define P6-func (λ (x) (eval-poly P6 x)))
(define P12 (calc-poly POINTS))
(define P12-func (λ (x) (eval-poly P12 x)))
(define Others-approx (map P6-func Others-x))
(define error (axpy Others-y -1 Others-approx))
(plot-new-window? #true)
(plot (list (axes)
(function P6-func 0 100)
(points (map vector Others-x Others-approx)
#:size 2)
(points (map vector All-x All-y))
(function P12-func 0 100
#:color "red")))
;;The Plot:
;;- The polynomial fitted to the 6 points
;;- The calculated values for the other 6 points
;;- The polynomial fitted to 12 points (red)
;; Look at the accuracy of of the points we calculated vs. the real values
;; Also, look how the polynomial changes shape from 6-pts to 12-pts
;; Note that it become "curvier", that is, has more peaks as the number of
;; points considered in the approximation increases.