Fit and Plot a Polynomial Surface

조회 수: 9 (최근 30일)
Marius Gratzl
Marius Gratzl 2018년 9월 21일
댓글: dpb 2018년 9월 24일
Allright, the last time i used matlab was two years ago and i almost forgot everything. What i´m trying to do is to plot a x y z matrix and fit it so i get an mathematical formular. So basically what´s done here : https://de.mathworks.com/help/curvefit/polynomial.html#bt9ykh7 "Fit and Plot a Polynomial Surface"
I tried it like this, as i said i haven´t done this in a while so don´t execute me
y1=[1 2 3 4];
x1=[100 200 300 400]';
[x,y]=meshgrid(x,y1);
z=[1.0 1.6 2.0 2.460; 1.0 1.5...
1.8 2.2; 1.0 1.5 1.931 2.2;...
1.1 1.6 2.1 2.4];
fitsurface=fit([x,y],z, 'poly21');
plot(fitsurface, [x,y],z);
And that´s what i get :
Error using fit>iFit (line 135)
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in Matlab_Versuch (line 7)
fitsurface=fit([x,y],z, 'poly21');
I basically know what´s wrong but i can´t fix it.
Thanks

채택된 답변

dpb
dpb 2018년 9월 21일
편집: dpb 2018년 9월 21일
Check spelling... :)
x1=[100 200 300 400]';
[x,y]=meshgrid(x,y1);
You defined x1 but used x as argument instead...
fitsurface=fit([x,y],z, 'poly21')
fit wants vectors, not arrays. Use the Matlab idiom (:) to return all elements of matrix/array as column vector:
x=x(:); y=y(:); z=z(:);
fitsurface=fit([x,y],z, 'poly21')
Somehow the builtin plot doesn't recognize it's a surface and only plots on 2D axis; I had to use scatter3 to show...
Also, it appears the real curvature is in Y but you fit the quadratic in X; looks like should either reverse role of X,Y or use 'poly12' instead of 'poly21'
  댓글 수: 2
dpb
dpb 2018년 9월 24일
[Maruius Gratzi Answer moved to comment -- dpb]
That was incredibly helpful, thank you so much.
dpb
dpb 2018년 9월 24일
If it solves the problem, go ahead and Accept the answer to indicate so others don't take time to try to add...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by