-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
194 lines (165 loc) · 3.89 KB
/
main.go
File metadata and controls
194 lines (165 loc) · 3.89 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
//"image/color"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg"
//"github.com/gonum/stat/distuv"
)
type xy struct {
x float64
y float64
}
func main() {
var xys3 []xy
// var xys4 []xy
// var xys5 []xy
f3, err := os.Open("./data3.txt")
if err != nil {
fmt.Errorf("unable to open file %v", err)
}
// f4, err := os.Open("./data4.txt")
// if err != nil {
// fmt.Errorf("unable to open one of file%v", err)
// }
// f5, err := os.Open("./data5.txt")
// if err != nil {
// fmt.Errorf("unable to open 5th file%v", err)
// }
defer f3.Close()
scanner := bufio.NewScanner(f3)
for scanner.Scan() {
xyline := scanner.Text()
xys3 = append(xys3, returnxy(xyline))
}
// defer f4.Close()
// scanner = bufio.NewScanner(f4)
// for scanner.Scan() {
// xyline := scanner.Text()
// xys4 = append(xys4, returnxy(xyline))
// }
// defer f5.Close()
// scanner = bufio.NewScanner(f5)
// for scanner.Scan() {
// xyline := scanner.Text()
// xys5 = append(xys5, returnxy(xyline))
// }
//fmt.Println(xys)
plotgraph(xys3 /*, xys4, xys5*/)
if err = scanner.Err(); err != nil {
fmt.Errorf("uable to scan %v", err)
}
}
func plotgraph(xys3 /*, xys4, xys5 */ []xy) {
p, err := plot.New()
if err != nil {
fmt.Errorf("unable to plot from given xys%v", err)
}
p.Title.Text = "scattered graph"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
err = plotutil.AddScatters(p, rxys(xys3))
if err != nil {
fmt.Errorf("unable to scatter%v", err)
}
m1, c1 := getiline(rxys(xys3))
// m2, c2 := getiline(rxys(xys4))
// m3, c3 := getiline(rxys(xys5))
err = plotutil.AddLinePoints(p, putiline(xys3, m1, c1)) // "FourthFile", putiline(xys4, m2, c2),
// "fifthFile", putiline(xys5, m3, c3),
if err != nil {
fmt.Errorf("unable to make line%v", err)
}
if err = p.Save(4*vg.Inch, 4*vg.Inch, "out.png"); err != nil {
fmt.Errorf("unable to save out.png bcz %v", err)
}
}
func getiline(xys plotter.XYs) (float64, float64) {
if len(xys) < 1 {
fmt.Errorf("your xys are empty ")
}
var x []float64
// var x [] plotter.XYs.X
for k := 0; k < len(xys); k++ {
x = append(x, xys[k].X)
}
// for v := range xys {
// }
minc := 6.0
minm := 3.0
// y=m*x+c
mincost := 999.999
for m := 0.3; m < 1; m += 0.01 {
for c := 0.0; c < 6; c += 0.1 {
for _ = range x {
cost := computeCost(m, c, x, xys)
if cost < mincost {
mincost = cost
minc = c
minm = m
}
}
}
}
fmt.Println("mincost= ", mincost, "minc= ", minc, "minm= ", minm)
return minm, minc
}
func computeCost(m float64, c float64, i []float64, xys plotter.XYs) float64 {
if len(i) == 1 || len(xys) == 1 {
return math.Sqrt(math.Pow((i[0]-xys[0].X), 2) + math.Pow((m*i[0]+c)-xys[0].Y, 2))
}
var totalCost float64
var slicedX []float64
var nxys plotter.XYs
for _ = range i {
slicedX = i[1:]
nxys = xys[1:]
totalCost = lineCost(i[0], m*i[0]+c, xys[0].X, xys[0].Y) + computeCost(m, c, slicedX, nxys)
}
fmt.Println(totalCost, "totalcost")
return totalCost
}
func lineCost(xp float64, yp float64, xysx float64, xysy float64) float64 {
return math.Sqrt(math.Pow((xp-xysx), 2) + math.Pow(yp-xysy, 2))
}
func putiline(xys []xy, m float64, c float64) plotter.XYs {
pts := make(plotter.XYs, len(xys))
i := 0
for v := range pts {
pts[v].X = xys[i].x
pts[v].Y = xys[i].x*m + c
i++
}
return pts
}
func rxys(xys []xy) plotter.XYs {
pts := make(plotter.XYs, len(xys))
i := 0
for v := range pts {
pts[v].X = xys[i].x
pts[v].Y = xys[i].y
i++
}
return pts
}
func returnxy(xyline string) xy {
var xy xy
var err error
xys := strings.Split(xyline, ",")
xy.x, err = strconv.ParseFloat(xys[0], 64)
if err != nil {
fmt.Errorf("cannot convert this string to float %v", err)
}
xy.y, err = strconv.ParseFloat(xys[1], 64)
if err != nil {
fmt.Errorf("unable to convert this y to %v", err)
}
return xy
}