Error using Polyfit: The first two inputs must have the same number of elements. The x can have multiple inputs as columns?
46 views (last 30 days)
Show older comments
x = t_result(:,2:8);
y = t_result(:,11);
p1 = polyfit(x,y,1);
f1 = polyval(p1, x);
p2 = polyfit(x,y,2);
f2 = polyval(p2, x);
res1 = polyval(p1,x) - y;
res2 = polyval(p2, x) - y;
subplot(2,2,1), plot(x,y,'ko'), hold on, plot(x,f1,'k--'), grid on, title('1st Order Fit')
subplot(2,2,2), plot(x,y,'ko'), hold on, plot(x,f2,'k--'), grid on, title('2nd Order Fit')
0 Comments
Answers (2)
Walter Roberson
on 17 Sep 2020
The x can have multiple inputs as columns?
doc polyfit
"If x is not a vector, then polyfit converts it into a column vector x(:)."
"If y is not a vector, then polyfit converts it into a column vector y(:)."
You cannot use polyfit() to fit a multi-dimensional x against a y value.
I speculate that you might be wanting:
p1 = [x, ones(size(x,1),1)] \ y;
This should give you a size(x,2)+1 vector of results, such that
p1(1)*x(:,1) + p1(2)*x(:,2) + p1(3)*x(:,3) + p1(4)*x(:,4) + p1(5)*x(:,5) + p1(6)*x(:,6) + p1(7)*x(:,7) + p1(8)*1 best approximates y
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing 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!