Least Squares Regression, Help me find the error in my code!
2 次查看(过去 30 天)
显示 更早的评论
Suppose the curve you're fitting can follow the form
y = an*x^n+ an-1*x^(n-1)+...+ a1x + a0
x and y are two column vectors of the same size and n is the degree of the polynomial. A is an array of the coefficients, in the order from an to a0.
Solve for A, and the e, the array of the residuals.
Here's what I have: function [A,e] = MyPolyRegressor(x, y, n)
for i= 1:n
x(i)=x.^(n-1);
end;
A=x/y;
e=A*x-y;
Whenever I run it, it says, "In an assignment A(I) = B, the number of elements in B and I must be the same." Please help!
回答(2 个)
Matt J
2012-10-26
编辑:Matt J
2012-10-26
In
x(i)=x.^(n-1);
the left hand side is a scalar location and the right hand side is not a scalar. So there's no room for it. I don't really understand why you're trying to overwrite x with powers of itself like x.^(n-1).
You should consider the VANDER command. I'm assuming you're not allowed to use POLYFIT.
Azzi Abdelmalek
2012-10-26
There is a problem in your x array size, it should be a matrix (nxm) where m is the length of y and m is the lenngth of unknown variables
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!