MATLAB: Problem with code. Error using vertcat

matricesvertcat

Hello, I am having difficulty with a code. Every time I run it, it tells me that I have an error using vertcat. I'm not quite sure how to fix this. Any help is greatly appreciated. The code is provided below:
function a = QuadFit( x,y )
colx=size(x);
coly=size(y);
m=colx;
n=coly;
if m~=n
error('Size of x and y must be equal');
end
%x values summation
sumx=0;
for i=1:m
sumx=sumx+x(i);
end
%x^2 values summation
sumx_2=0;
for i=1:m
sumx_2=sumx_2+(x(i)^2);
end
%x^3 values summation
sumx_3=0;
for i=1:m
sumx_3=sumx_3+(x(i)^3);
end
%x^4 values summation
sumx_4=0;
for i=1:m;
sumx_4=sumx_4+(x(i)^4);
end
%y values summation
sumy=0;
for i=1:m
sumy=sumy+y(i);
end
%x*y values summation
sumxy=0;
for i=1:m
sumxy=sumxy+(x(i)*y(i));
end
%(x^2)*y values summation
sumx_2y=0;
for i=1:m
sumx_2y=sumx_2y+((x(i)^2)*y(i));
end
a=[sumx_2 sumx_3 sumx_4 sumx_2y;sumx sumx_2 sumx_3 sumxy;n sumx sumx_2 sumy];
a=rref(a);
a1=a(1,4);
a2=a(2,4);
a3=a(3,4);
end
The error is occurring is this line:
a=[sumx_2 sumx_3 sumx_4 sumx_2y;sumx sumx_2 sumx_3 sumxy;n sumx sumx_2 sumy];

Best Answer

  • If you want the column size only of ‘x’ and ‘y’, you have to be specific with the size function.
    Change the ‘colx’ and ‘coly’ assignments to:
    colx=size(x,2);
    coly=size(y,2);
    and it works!