# Why its showing error using vertcat?

1 view (last 30 days)
Sabbir Hossain on 22 Nov 2020
Commented: Sabbir Hossain on 22 Nov 2020
x = [0 1 2 3 4 5]
y = [2.2 7.7 13.6 27.2 40.9 61.1]
sumx = 0;
sumy = 0;
sumx2 = 0;
sumx3 = 0;
sumx4 = 0;
sumxy = 0;
sumx2y = 0;
n = length(x);
for i=1:n
sumx = sumx + x(i);
sumy = sumy + y(i);
sumx2 = sumx2 + ((x(i))^2);
sumx3 = sumx3 + ((x(i))^3);
sumx4 = sumx4 + ((x(i))^4);
sumxy = sumxy + (x(i)*y(i));
sumx2y= sumx2y + (((x(i))^2)*y);
end
D= det([n sumx sumx2 ; sumx sumx2 sumx3 ; sumx2 sumx3 sumx4]);
a0=det([sumy sumxy sumx2y ; sumx sumx2 sumx3 ; sumx2 sumx3 sumx4])/D;
a1=det([sumy sumxy sumx2y ; n sumx sumx2 ; sumx2 sumx3 sumx4])/D;
a2=det([sumy sumxy sumx2y ; n sumx sumx2 ; sumx sumx2 sumx3])/D;
ym= a0 +(a1*x) + (a2*x.^2)

Stephen Cobeldick on 22 Nov 2020
Edited: Stephen Cobeldick on 22 Nov 2020
I suspect that this line
sumx2y= sumx2y + (((x(i))^2)*y);
requires indexing into y, e.g.:
sumx2y = sumx2y + (x(i)^2)*y(i);

#### 1 Comment

Sabbir Hossain on 22 Nov 2020
Thanks a lot! :D