Plotting the outcome of a 3D fit

4 views (last 30 days)
Saeid
Saeid on 21 Aug 2023
Commented: Saeid on 21 Aug 2023
I have a set of data in the form [X Y Z] where X, Y and Z are column arrays. I can plot these data using scatter3. However, when I do the fit and then try to plot a surface of the fit with new Xf & Yf values in a range that is a bit beyond the original X & Y, I try this:
XYZFitCoefficinets=fit([X Y],Z,"poly22")
Xf=linspace(0,30,100)'; Yf=linspace(40,140,100)';
Zf=feval(XYZFitCoefficinets,[Xf, Yf])
I get another column array that just shows the calculated points, whereas I need a surface. I know that I need to use the surf function and turn Xf and Yf into mesh grids, but apparently it is not possible to use the feval with meshgridded X & Y. So what can i do?

Accepted Answer

Torsten
Torsten on 21 Aug 2023
Edited: Torsten on 21 Aug 2023
x = 0:0.1:1;
y = -1:0.1:1;
[X,Y] = meshgrid(x,y);
Z = X.^2+Y.^2+0.1*(-1+2*rand(size(X)));
[Xout,Yout,Zout] = prepareSurfaceData(X,Y,Z);
S = fit([Xout,Yout],Zout,"poly22")
Linear model Poly22: S(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 Coefficients (with 95% confidence bounds): p00 = 0.01879 (-0.001415, 0.039) p10 = -0.02596 (-0.1119, 0.05996) p01 = -0.007346 (-0.02993, 0.01523) p20 = 1.018 (0.9357, 1.101) p11 = 0.009983 (-0.02819, 0.04815) p02 = 0.9853 (0.963, 1.008)
xq = 0:0.01:0.5;
yq = -1:0.01:0;
[Xq,Yq] = meshgrid(xq,yq);
S(Xq,Yq)
ans = 101×51
1.0115 1.0112 1.0112 1.0113 1.0117 1.0122 1.0130 1.0140 1.0151 1.0165 1.0181 1.0199 1.0218 1.0240 1.0264 1.0290 1.0318 1.0348 1.0380 1.0414 1.0450 1.0489 1.0529 1.0571 1.0615 1.0662 1.0710 1.0760 1.0813 1.0867 0.9918 0.9915 0.9915 0.9916 0.9920 0.9926 0.9933 0.9943 0.9955 0.9968 0.9984 1.0002 1.0022 1.0044 1.0067 1.0093 1.0121 1.0151 1.0184 1.0218 1.0254 1.0292 1.0332 1.0374 1.0419 1.0465 1.0513 1.0564 1.0616 1.0671 0.9723 0.9721 0.9720 0.9722 0.9725 0.9731 0.9738 0.9748 0.9760 0.9774 0.9789 0.9807 0.9827 0.9849 0.9873 0.9899 0.9927 0.9957 0.9989 1.0023 1.0059 1.0097 1.0138 1.0180 1.0224 1.0270 1.0319 1.0369 1.0422 1.0476 0.9530 0.9528 0.9527 0.9529 0.9532 0.9538 0.9546 0.9555 0.9567 0.9581 0.9597 0.9614 0.9634 0.9656 0.9680 0.9706 0.9734 0.9764 0.9796 0.9830 0.9866 0.9905 0.9945 0.9987 1.0031 1.0078 1.0126 1.0177 1.0229 1.0283 0.9339 0.9337 0.9336 0.9338 0.9341 0.9347 0.9355 0.9364 0.9376 0.9390 0.9406 0.9424 0.9443 0.9465 0.9489 0.9515 0.9543 0.9573 0.9605 0.9640 0.9676 0.9714 0.9754 0.9796 0.9841 0.9887 0.9935 0.9986 1.0038 1.0093 0.9150 0.9148 0.9147 0.9149 0.9153 0.9158 0.9166 0.9176 0.9187 0.9201 0.9217 0.9235 0.9255 0.9277 0.9300 0.9326 0.9355 0.9385 0.9417 0.9451 0.9487 0.9525 0.9565 0.9608 0.9652 0.9698 0.9747 0.9797 0.9850 0.9904 0.8964 0.8961 0.8961 0.8962 0.8966 0.8971 0.8979 0.8989 0.9000 0.9014 0.9030 0.9048 0.9068 0.9090 0.9114 0.9140 0.9168 0.9198 0.9230 0.9264 0.9300 0.9338 0.9379 0.9421 0.9465 0.9512 0.9560 0.9611 0.9663 0.9718 0.8779 0.8776 0.8776 0.8777 0.8781 0.8786 0.8794 0.8804 0.8816 0.8829 0.8845 0.8863 0.8883 0.8905 0.8929 0.8955 0.8983 0.9013 0.9045 0.9079 0.9115 0.9154 0.9194 0.9236 0.9281 0.9327 0.9375 0.9426 0.9478 0.9533 0.8595 0.8593 0.8593 0.8594 0.8598 0.8603 0.8611 0.8621 0.8633 0.8646 0.8662 0.8680 0.8700 0.8722 0.8746 0.8772 0.8800 0.8830 0.8862 0.8896 0.8933 0.8971 0.9011 0.9053 0.9098 0.9144 0.9193 0.9243 0.9296 0.9350 0.8414 0.8412 0.8412 0.8413 0.8417 0.8422 0.8430 0.8440 0.8452 0.8465 0.8481 0.8499 0.8519 0.8541 0.8565 0.8591 0.8619 0.8649 0.8681 0.8716 0.8752 0.8790 0.8830 0.8873 0.8917 0.8963 0.9012 0.9062 0.9115 0.9169
surf(Xq,Yq,S(Xq,Yq))
  1 Comment
Saeid
Saeid on 21 Aug 2023
Thank you Torsten, that's exactly what I wanted!

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!