-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finalcodecubicquadraticlinear_update.m
225 lines (169 loc) · 5.76 KB
/
Finalcodecubicquadraticlinear_update.m
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
%Data initialization%
data_camera = csvread('CAMERA_1.csv');
camera_time = data_camera(:,1);
camera_data_w = data_camera(:,2);
hold on
data_sensor = csvread('SENSOR_1.csv');
sensor_time = data_sensor(:,1);
sensor_data_w = data_sensor(:,2);
%Data processing&
p=2 % data length quadratic %
l=3 %data length cubic%
q=1 %data length Linear%
predict=[]% Array initialization of Cubic Data%
predicterror=[]% Array initialization of predicted Error for Cubic Data%
predictlength=[]% Array initialization of predicted length for Cubic Data%
actuallength=[]% Array initialization of the actual length%
predictq=[] % Array initialization of Quadratic Data%
predicterrorq=[]% Array initialization of predicted Error for Quadratic data Data%
predictlengthq=[]% Array initialization of predicted length of Quadratic Data%
predictl=[] % Array initialization of Linear Data%
predicterrorl=[]% Array initialization of predicted Error for Linear data Data%
predictlengthl=[]% Array initialization of predicted length of Linear Data%
%noprediction=[]%Array initialization of no prediction%
d=[]; % Array initialization of difference between cubic and quadratic data%
n=4; % Data position%
i=1;
t=[]; % Array resizing for plot operation for camera data value%
k=[]; % Array resizing for plot operation for camera time value%
u=[];% Array resizing for plot operation for camera data value%
v=[];% Array resizing for plot operation for camera time value%
while n<311
%camera Data Quadratic%
x = camera_time(n-p:n,:);
y = camera_data_w(n-p:n,:);
% Camera Data cubic%
xc = camera_time(n-l:n,:);
yc = camera_data_w(n-l:n,:);
%
%camera Data Linear %
xl = camera_time(n-q:n,:);
yl = camera_data_w(n-q:n,:);
u=camera_time(l:n+1,:);
v=camera_data_w(l:n+1,:);
%Cubic curve fit
parms = [xc.^3 xc.^2 xc ones(size(xc))]\yc;
xv = linspace(min(xc), max(xc))';
y_fit = [xv.^3 xv.^2 xv ones(size(xv))]*parms;
% %quadratic curve fit%
parms1 = [x.^2 x ones(size(x))]\y;
y_fit1 = [xv.^2 xv ones(size(xv))]*parms1;
%Linear curve fit%
parms2 = [xl ones(size(xl))]\yl;
y_fit2 = [xv ones(size(xv))]*parms2;
%Cubic parameters%
cof1=parms(1);
cof2=parms(2);
cof3=parms(3);
cof4=parms(4);
%Quadratic parameter%
coefq1=parms1(1);
coefq2=parms1(2);
coefq3=parms1(3);
%Linear Parameters%
coefl1=parms2(1);
coefl2=parms2(2);
% Predicted Cubic data%
predict(i)=cof1*(camera_time(n+1))^3+ cof2*(camera_time(n+1))^2+ cof3*camera_time(n+1)+ cof4;
%Predicted quadratic data%
predictq(i)=coefq1*(camera_time(n+1))^2+ coefq2*(camera_time(n+1))+ coefq3;
%
%Predicted Linear data%
predictl(i)=coefl1*(camera_time(n+1))+coefl2;
% No prediction Value%
noprediction(i)=abs(camera_data_w(i+1)-camera_data_w(i));
%Difference between cubic and quadratic data%
d(i)=abs(predict(i)-predictq(i));
%Predicted error for cubic data%
predicterror(i)=abs(camera_data_w(n+1)-predict(i));
%predicted error for Quadratic data%
predicterrorq(i)=abs(camera_data_w(n+1)-predictq(i));
%predicted error for Linear data%
predicterrorl(i)=abs(camera_data_w(n+1)-predictl(i));
%Predicted length for cubic data%
predictlength(i)=abs(predict(i)-camera_data_w(n));
%Predicted length for quadratic data%
predictlengthq(i)=abs(predictq(i)-camera_data_w(n));
%Predicted length for quadratic data%
predictlengthl(i)=abs(predictl(i)-camera_data_w(n));
%Actual length of data%
actuallength(i)=abs(camera_data_w(n+1)-camera_data_w(n));
t(i)=camera_time(n+1);
k(i)=camera_data_w(n+1);
n=n+1;
i=i+1;
%Figure 1 plotting%
%figure('Name', 'Curve fitting with predicted next value for cubic and quadratic function')
plot(x, y, 'bp')
hold on
grid on
plot(xv, y_fit, '-r')
plot(xv, y_fit1,'-b')
plot(xv,y_fit2,'-m')
plot(u,v,'-g')
plot(u,v,'-g','linewidth',1) %plot data with big width %
plot( t(i-1),predict(i-1),'-x')
plot( t(i-1),predictq(i-1),'-o')
plot( t(i-1),predictl(i-1),'-s')
plot(t(i-1),k(i-1),'-d')
xlabel('Camera Time')
ylabel('camera data')
end
% Data analysis cubic data error%
maxerror=max(predicterror);
minerror=min(predicterror);
averageerorr=mean(predicterror);
standardev=std(predicterror);
%Data analysis quadratic data error%
maxerrorq=max(predicterrorq);
minerrorq=min(predicterrorq);
averageerorrq=mean(predicterrorq);
standardevq=std(predicterrorq);
%Data analysis quadratic data error%
maxerrorl=max(predicterrorl);
minerrorl=min(predicterrorl);
averageerorrl=mean(predicterrorl);
standardevl=std(predicterrorl);
% % The error in actual length
maxerror_actual=max(actuallength);
minerror_actual=min(actuallength);
averageerorr_actual=mean(actuallength);
standardev_actual=std(actuallength);
% %Figure 1 plotting%
%
% figure('Name', 'Curve fitting with predicted next value for cubic and quadratic function')
% plot(x, y, 'bp')
% hold on
% grid on
% plot(xv, y_fit, '-r')
% plot(xv, y_fit1,'-b')
% plot(xv,y_fit2,'-m')
% plot(t,k,'-g')
% plot( t(i-1),predict(i-1),'-x')
% plot( t(i-1),predictq(i-1),'-o')
% plot( t(i-1),predictl(i-1),'-s')
% plot(t(i-1),k(i-1),'-d')
% xlabel('Camera Time')
% ylabel('camera data')
% hold off
%Figure 2 plotting%
figure('Name', 'Predicted length comaparision with Actual length')
plot(t,predictlength,'-r')
hold on
plot(t,predictlengthq,'-b')
plot(t,predictlengthl,'-m')
plot(t,actuallength,'-g')
xlabel('Camera Time')
ylabel('Length of samples from previous point')
hold off
%Figure 3 plotting%
figure('Name', 'Predicted Error comparision')
plot(t,predicterror,'-r')
hold on
plot(t,predicterrorq,'-b')
plot(t,predicterrorl,'-g')
plot(t,actuallength,'-k')
% plot(t,noprediction,'-c')
%plot(t,d,'-m')
xlabel('Camera Time')
ylabel('Predicted Error')