indepvar and depvar are of inconsistent sizes with polyfitn

2 Ansichten (letzte 30 Tage)
when i use polyfitn with matlab say indepvar and depvar are of inconsistent sizes and i don't now where i am wrong can yo help me?
this is the data:
x1=[2.2 2.2 2.2 2.44 2.49 2.48 2.45 2.2 2.39 2.38];
x2=[29.4 30.9 32.9 26.9 28 28.8 30.1 29.4 28.8 30];
y=[7.5 7.02 6.94 7.91 7.96 7.91 7.78 7.42 7.86 7.47];
p=polyfitn([x1,x2],y,1)
Unrecognized function or variable 'polyfitn'.
Error using polyfitn (line 172)
indepvar and depvar are of inconsistent sizes.

Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 4 Sep. 2019
Yes, you send in an array [x1,x2] of size [1 x 20] and an array y of size [ 1 x 10] into polyfitn. The polyfitn version I have (a matlab-central submission by John d'Errico from 2006) gives me a more sensible result after transposing your inputs:
p=polyfitn([x1;x2]',y',1)
p =
ModelTerms: [3x2 double]
Coefficients: [1.5589 -0.11979 7.4607]
ParameterVar: [0.1693 0.0010284 2.9687]
ParameterStd: [0.41146 0.032068 1.723]
R2: 0.91523
RMSE: 0.10284
VarNames: {}
This was my curse when starting to use matlab - I solved it by the very sofisticated technique:
If it doesn't work transpose, transpose again until all combinations have been tried.
You might do the same but a more intelligent aproach is to check the documentation and input variables.
HTH
  3 Kommentare
LOGAN
LOGAN am 14 Dez. 2024
ok how does that help him
Steven Lord
Steven Lord am 15 Dez. 2024
Compare the sizes:
x1=[2.2 2.2 2.2 2.44 2.49 2.48 2.45 2.2 2.39 2.38];
x2=[29.4 30.9 32.9 26.9 28 28.8 30.1 29.4 28.8 30];
y=[7.5 7.02 6.94 7.91 7.96 7.91 7.78 7.42 7.86 7.47];
If the poster was using this File Exchange submission, what does its help text state about the requirements it places on its inputs?
% indepvar - (n x p) array of independent variables as columns
% n is the number of data points
% p is the dimension of the independent variable space
%
% IF n == 1, then I will assume there is only a
% single independent variable.
%
% depvar - (n x 1 or 1 x n) vector - dependent variable
% length(depvar) must be n.
If you passed [x1, x2] into polyfitn as the indepvar input argument, what are n and p?
[n, p] = size([x1, x2])
n = 1
p = 20
Is y (the depvar input argument) either an n x 1 or 1 x n vector? If we look at its size, it's clear that it is not.
sz = size(y)
sz = 1×2
1 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If we transposed the x and y data:
[n, p] = size([x1.', x2.'])
n = 10
p = 2
sz = size(y.')
sz = 1×2
10 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
We see that y.' is an n x 1 vector. So polyfitn can operate on it and the combination [x1.', x2.'].

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by