...
|
...
|
@@ -22,7 +22,7 @@ points(x_vec_2,mock_vec, col="red", type="p")
|
22
|
22
|
conrel =function(b) b
|
23
|
23
|
quarel =function(x,a,m,b) a*x**2 + m*x+b
|
24
|
24
|
|
25
|
|
-reso = 10 #the resolution
|
|
25
|
+reso = 50 #the resolution
|
26
|
26
|
a_vec = seq(-4,4,length.out = reso)
|
27
|
27
|
m_vec = seq(-4,8,length.out = reso)
|
28
|
28
|
b_vec = seq(-2,2,length.out = reso)
|
...
|
...
|
@@ -46,13 +46,25 @@ for (i in (1:length(b_vec))){
|
46
|
46
|
}
|
47
|
47
|
}
|
48
|
48
|
}
|
49
|
|
-
|
|
49
|
+#find min
|
50
|
50
|
minimum_qua=min(qua_arr)
|
51
|
51
|
best_m_qua = m_vec[which(qua_arr == minimum_qua,arr.ind=TRUE)[2]]
|
52
|
52
|
best_b_qua = b_vec[which(qua_arr == minimum_qua,arr.ind=TRUE)[1]]
|
53
|
53
|
best_a_qua = a_vec[which(qua_arr == minimum_qua,arr.ind=TRUE)[3]]
|
|
54
|
+points(x_vec, quarel(x_vec,best_a_qua,best_m_qua,best_b_qua), type="l", col="red", xlab='x',ylab='f(x)=y')
|
|
55
|
+
|
54
|
56
|
minimum_lin=min(lin_arr)
|
55
|
57
|
best_m_lin = m_vec[which(lin_arr == minimum_lin,arr.ind=TRUE)[2]]
|
56
|
58
|
best_b_lin = b_vec[which(lin_arr == minimum_lin,arr.ind=TRUE)[1]]
|
|
59
|
+points(x_vec, linrel(x_vec,best_m_lin,best_b_lin), type="l", col="blue", xlab='x',ylab='f(x)=y')
|
57
|
60
|
minimum_con=min(lin_arr)
|
58
|
61
|
best_b_con = b_vec[which(con_arr == minimum_con,arr.ind=TRUE)[1]]
|
|
62
|
+points(x_vec, rep(conrel(best_b_lin),41), type="l", col="yellow", xlab='x',ylab='f(x)=y')
|
|
63
|
+
|
|
64
|
+#Probelm3
|
|
65
|
+norm = function(a) 1/sum(a) * a
|
|
66
|
+
|
|
67
|
+posterior_qua = exp(-0.5 *(norm(qua_arr) - minimum_qua))
|
|
68
|
+posterior_lin = exp(-0.5 *(norm(lin_arr) - minimum_lin))
|
|
69
|
+posterior_con = exp(-0.5 *(norm(con_arr) - minimum_con))
|
|
70
|
+
|