In this programe of line fitting for y=mx+c , i get problem in "cfit". plz help me to get right program for this y=mx+c.

1 view (last 30 days)
x=[0:100]';
y=x;
n=randn(101,1);
yi=y+n;
m=input('inital value slope');
dm=input('incremental value in m');
c=input('inital value contsant');
dc=input('incremental value in c');
niter=input('no. of iterstions');
for a=1:niter;
mx=[m-dm,m,m+dm];
cx=[c-dc,c,c+dc];
for b=1:3;
e=1:3;
my=mx(b);
cy=cx(e);
yfit=my*x;
cfit=my*x*n+cy*(mx);
yerr(b,e)=sum((cfit-yi).^2);
end;
[z indx]=sort(yerr);
m= mx(indx(1));
dm=dm/2;
end;
yfit1=m*x;
plot(x,yfit1,'r');
hold on
plot(x,yi,'x');

Answers (1)

Arthur
Arthur on 12 Sep 2013
The dimensions of your arrays in not consistent. In order to add or multiply two variables, they have to have the same dimensions, or to be scalars. But in your script, my is 1x1, x is 101x1, n is 101x1, cy is 3x1 and mx is 3x1.
I'm not sure what you're trying to achieve, but probably you should fix the dimensions of cy and mx.

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!