Re: User error? using polyfitn



On Aug 1, 2:59 am, "Doug " <dlun...@xxxxxxx> wrote:
I'm trying to fit a 3rd degree polynomial to a 4 dimension vector but am receiving an error. I'm pretty sure this is User error but am hoping someone can confirm.
Thanks!

-----------------------------
Test case

X = [30283 20974 40539 91059
54815 82935 68492 86031
27741 52782 20357 73967
100939 133556 108475 132732
79309 72594 89180 108548
51074 119284 33319 78563
126552 225774 136969 159157
116231 190673 124323 135083
85886 139067 262800 143185
79350 172461 241657 116668
138276 157770 179669 165956
157927 115074 198781 182370
147525 158100 411094 199085
169776 175124 170413 163409
125307 177306 95407 159007
111207 178466 206460 225036
154182 182159 187999 194402
168701 157936 98865 197024
125522 199295 186229 150596
150830 197525 156740 183813
238765 195124 287008 274041
216665 189816 203677 236783
256638 242376 317508 284584
213928 277714 274816 261946
89433 205460 120011 140368
222816 270634 382162 253755
189917 220903 383327 228505
252445 303877 349604 247337
268267 89690 289973 310174
243958 80479 261901 264269
273625 269086 310874 330201
497259 532734 502572 416877];

y = [ 49000
65000
78000
95000
116900
118000
119000
127000
137500
140000
158800
160000
160000
173500
175000
176500
185000
194500
197000
198500
222000
225000
238500
275000
285000
290000
300000
300000
307000
340000
360000
539000];

p = polyfitn(X, y, 3);

??? Error using ==> run
Error using ==> mldivide
Matrix dimensions must agree.



hello,
I know the reason now. In your above test case,
size(X) =[32 1].
The parameter 3 is give to the polyfitn and has been parsed to
modelterms(to check it in the polyfitn file) , so results

modelterms=[
3 0 0 0
2 1 0 0
2 0 1 0
2 0 0 1
2 0 0 0
1 2 0 0
1 1 1 0
1 1 0 1
1 1 0 0
1 0 2 0
1 0 1 1
1 0 1 0
1 0 0 2
1 0 0 1
1 0 0 0
0 3 0 0
0 2 1 0
0 2 0 1
0 2 0 0
0 1 2 0
0 1 1 1
0 1 1 0
0 1 0 2
0 1 0 1
0 1 0 0
0 0 3 0
0 0 2 1
0 0 2 0
0 0 1 2
0 0 1 1
0 0 1 0
0 0 0 3
0 0 0 2
0 0 0 1
0 0 0 0]

This matrix shows the possible combinations of 4 variables to present
a 3rd degree polynomial. The size of this matrix is [ 35 4], so
there are 35 possible coefficients for your wanted 3rd degree
polynomial, more than the number 32 of your independent variables, so
the problems became underdetermined. That is why the error came out.

So you should expand the fist dimension of the matrx X and y, or to
fit to a lower order polynomial.

Below I do some change to your test case:

%%%%%%%

clc
clear

dpl=3;
dm=40; %% dm must >=35 for dpl=3.
X=floor(rand(dm,4)*1000000)
y=floor(rand(dm,1)*1000000)

p = polyfitn(X, y, dpl)

% sp = polyn2sympoly(p)


%%%%%

Good day!


.