Fit and Plot a Polynomial Surface

11 次查看(过去 30 天)
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