-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (89 loc) · 2.23 KB
/
main.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"math"
"image/color"
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/vg"
)
func main() {
x0, h, n := 0., 0.2, 8
var points []Point
for x, i := x0, 0; i < n; i++ {
points = append(points, Point{x, f(x), df(x)})
x += h
}
polLag := Lagranzh(points...)
polNew1 := NewtonFirst(h, points...)
polNew2 := NewtonSecond(h, points...)
pols := BuildSplines(h, points...)
fmt.Println(points)
fmt.Println(polLag)
fmt.Println(polNew1)
fmt.Println(polNew2)
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = "Interpolation"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
fPlotter := plotter.NewFunction(f)
fPlotter.Color = color.RGBA{B: 255, A: 255}
lagPlotter := plotter.NewFunction(makePolunomialFunctinon(polLag))
lagPlotter.Color = color.RGBA{R: 255, A: 100}
new1Plotter := plotter.NewFunction(makePolunomialFunctinon(polNew1))
new1Plotter.Color = color.RGBA{G: 255, A: 100}
new2Plotter := plotter.NewFunction(makePolunomialFunctinon(polNew2))
new2Plotter.Color = color.RGBA{G: 255, R: 255, A: 100}
splinePlotter := plotter.NewFunction(makeSplineFunction(pols, points))
splinePlotter.Color = color.RGBA{G: 255, B: 255, A: 100}
p.Add(fPlotter)
p.Add(lagPlotter)
p.Add(new1Plotter)
p.Add(new2Plotter)
p.Add(splinePlotter)
p.X.Min = x0
p.X.Max = x0 + float64(n-1)*h
p.Y.Min = f(x0)
p.Y.Max = f(x0 + float64(n-1)*h)
if err := p.Save(14*vg.Inch, 14*vg.Inch, "interpolation8.png"); err != nil {
panic(err.Error())
}
}
func f(x float64) float64 {
return math.Pow(x, 7) + 5*x - 6
}
func df(x float64) float64 {
return 7*math.Pow(x, 6) + 5
}
func makePolunomialFunctinon(p Polynomial) func(float64) float64 {
return func(x float64) float64 {
var res float64
for i, v := range p.a {
res += math.Pow(x, float64(i)) * v
}
return res
}
}
func makeSplineFunction(polynomials []Polynomial, points []Point) func(float64) float64 {
return func(x float64) float64 {
var res float64
if x < points[0].x || x > points[len(points)-1].x {
res = 0
} else {
j := 0
for i, v := range points {
if v.x > x {
j = i - 1
break
}
}
for i, v := range polynomials[j].a {
res += math.Pow(x, float64(i)) * v
}
}
return res
}
}