-
Notifications
You must be signed in to change notification settings - Fork 1
/
lstq.py
146 lines (100 loc) · 2.36 KB
/
lstq.py
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
# %% [markdown]
# # Least Squares Solution:
# %% [markdown]
# <<<<< **Author: Arshad Shaik, UID:118438832; ENPM-673 Perception for Robotics; part of Project-1**>>>>
#
# The following program uses pc1.csv and pc2.csv, fits a surface to the data using the least square method. Subsequently, the results (the surface) are plotted to demonstrate the fit.
# %%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg
#%matplotlib inline
# %%
df = pd.read_csv('pc2.csv') # change the dataset as needed
print(df)
# %%
XX = df['x'].values
# %%
XXX = np.array([XX]).T
# %%
np.shape(XXX)
# %%
YY = df['y'].values
YYY = np.array([YY]).T
ZZ = df['z'].values
ZZZ = np.array([ZZ]).T
# %%
data = np.hstack((XXX,YYY,ZZZ))
# %%
X,Y = np.meshgrid(np.arange(-12.0, 12.0, 0.5), np.arange(-12.0, 12.0, 0.5))
# %%
print(np.shape(X))
# %%
print(np.shape(Y))
# %%
sum_x = 0.0
for i in range(0,len(XXX)):
sum_x += XXX[i]
print(sum_x)
# %%
sum_y = 0.0
for i in range(0,len(YYY)):
sum_y += YYY[i]
print(sum_y)
# %%
sum_z = 0.0
for i in range(0,len(ZZZ)):
sum_z += ZZZ[i]
print(sum_z)
# %%
sum_x2 = 0.0
for i in range(0,len(XXX)):
sum_x2 += XXX[i]*XXX[i]
print(sum_x2)
# %%
sum_y2 = 0.0
for i in range(0,len(YYY)):
sum_y2 += YYY[i]*YYY[i]
print(sum_y2)
# %%
sum_z2 = 0.0
for i in range(0,len(ZZZ)):
sum_z2 += ZZZ[i]*ZZZ[i]
print(sum_z2)
# %%
sum_xy = 0.0
for i in range(0,len(XXX)):
sum_xy += XXX[i]*YYY[i]
print(sum_xy)
# %%
sum_xz = 0.0
for i in range(0,len(XXX)):
sum_xz += XXX[i]*ZZZ[i]
print(sum_xz)
# %%
sum_yz = 0.0
for i in range(0,len(YYY)):
sum_yz += YYY[i]*ZZZ[i]
print(sum_yz)
# %%
AA = np.array([[sum_x2[0], sum_xy[0], sum_x[0]], [sum_xy[0], sum_y2[0], sum_y[0]], [sum_x[0], sum_y[0], len(XXX)]], dtype=np.float64)
BB = np.array([sum_xz, sum_yz, sum_z])
Q = np.linalg.inv(AA).dot(BB)
print(Q)
# %%
# evaluate it on grid
Z = Q[0]*X + Q[1]*Y + Q[2]
# %%
# plot points and fitted surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.5)
ax.scatter(data[:,0], data[:,1], data[:,2], c='r', s=50)
plt.xlabel('X')
plt.ylabel('Y')
ax.set_zlabel('Z')
ax.axis('auto')
ax.axis('tight')
plt.show()
# %%