Error using Polyfit: The first two inputs must have the same number of elements. The x can have multiple inputs as columns?

47 ビュー (過去 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')

回答 (2 件)

KSSV
KSSV 2020 年 9 月 17 日
編集済み: KSSV 2020 年 9 月 17 日
Your x is a m*6 array and y is a m*1..It is not allowed. That is why you got error.
Run a loop and use polyfit for each column of x.
If you are looking to fit a line with multiple inputs.....Have a look on regression.

Walter Roberson
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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by