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 次查看(过去 30 天)
显示 更早的评论
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');
0 个评论
回答(1 个)
Arthur
2013-9-12
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!