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

47 vues (au cours des 30 derniers jours)
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')

Réponses (2)

KSSV
KSSV le 17 Sep 2020
Modifié(e) : KSSV le 17 Sep 2020
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 le 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

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by