Fit and Plot a Polynomial Surface

8 ビュー (過去 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 Center および File ExchangeGet 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