Error using Polyfit: The first two inputs must have the same number of elements. The x can have multiple inputs as columns?
41 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Walter Roberson
2020-9-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!