Curve fitting(toolbox) 3d plot is different when using surf

I use the curve fitting toolbox and is was what I get:
However, when I try to use 'surf' to plot the 3d plot above it turns out:
[Xtest1, Xtest2] = meshgrid(xtest1, xtest2);
p00 = -0.4888 ;
p10 = 0.04507 ;
p01 = 0.1304 ;
p20 = 0.005239 ;
p11 = -0.01134 ;
p02 = -0.009979 ;
p30 = 0.0005176 ;
p21 = 0.0002514 ;
p12 = 0.0005639 ;
p03 = 0.0002402 ;
z = p00 + p10.*Xtest1 + p01.*Xtest2 + p20.*Xtest1.^2 + p11.*Xtest1.*Xtest2 + p02.*Xtest1.^2 + p30.*Xtest1.^3 + p21.*Xtest1.^2.*Xtest2 + p12.*Xtest1.*Xtest2.^2 + p03.*Xtest2.^3;
surf(Xtest1,Xtest2,z)
The plot using 'surf' is not as same as the plot from curve fitting toolbox at all. Apparently the maximum in curve fitting toolbox and surf plot are different. The data xtest1 and xtest2 are same for both curve fitting toolbox and meshgrid, I wonder what is wrong with the code above?

 Accepted Answer

Matt J
Matt J on 27 Feb 2022
Edited: Matt J on 27 Feb 2022
It looks like you have transcribed the fitted coefficients manually from the display, and only to 4 decimal places. (It also looks like you might have interchanged xtest1 with xtest2.)
The more reliable thing to do would be to save the fit tot the Matlab workspace, and then use the fit object to Evaluate the Surface Fit.

11 Comments

Thanks, I try the 'save to workspace' but the plot is still different, is there any reason why the 'surf' plot is different than the toolbox plot?
[Xtest1, Xtest2] = meshgrid(xtest1, xtest2);
p00 = -0.488757962540892 ;
p10 = 0.045069636734440 ;
p01 = 0.130432889884869 ;
p20 = 0.005238941763348 ;
p11 = -0.011336736979518 ;
p02 = -0.009978572315984 ;
p30 = 5.176228350092313e-04 ;
p21 = 2.514499348728134e-04 ;
p12 = 5.638521763257720e-04 ;
p03 = 2.401613755575195e-04 ;
z = p00 + p10.*Xtest1 + p01.*Xtest2 + p20.*Xtest1.^2 + p11.*Xtest1.*Xtest2 + p02.*Xtest1.^2 + p30.*Xtest1.^3 + p21.*Xtest1.^2.*Xtest2 + p12.*Xtest1.*Xtest2.^2 + p03.*Xtest2.^3;
surf(Xtest1,Xtest2,z)
If you attach the fit object that you exported in a .mat file, we can more easily examine why differerences may be occurring.
The difference in the appearance of the two plots is simply that the cftool is overlaying the input zzz data onto the final fit. Here is what the fit, without the data, looks like.
load fittedmodel
plot(fittedmodel); view(20,30)
If you wanted to reproduce the plot with the overlaid data, you would do,
plot(fittedmodel,[xtest1(:), xtest2(:)],zzz(:))
Thanks, but I still do not understand why I cannot create the same plot with 'surf', I need to use that function so I need to try to create the plot with 'surf'. Also, an error occurs with the code above:
plot(fittedmodel,[xtest1(:), xtest2(:)],zzz(:))
error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Thanks, but I still do not understand why I cannot create the same plot with 'surf', I need to use that function so I need to try to create the plot with 'surf'.
No, you don't need surf(). All fit post plotting can be done with the fittedmodel object.
Also, an error occurs with the code above:
Again, hard to do anything if you don't attach the data. We need the input data xtest1, xtest2, zzz that was used to generate the fit.
Sorry, but I mean I need to use the function from curve fitting toolbox 'f(x,y) = p00 + p10*x + p01*y + p20*x^2 + ...' in other project, so I need to make sure the 'surf' plot is correct, however it seems incorrect when using 'surf'.
Here are the files:
Here is a conventionally generated surf plot, together with that from the cftool app. As you can see, there is close agreement between them, and with the plot we generated earlier using the fittedmodel object.
load fittedmodel
coeffs=num2cell( coeffvalues(fittedmodel) );
[p00 p10 p01 p20 p11 p02 p30 p21 p12 p03]=deal(coeffs{:});
[x,y]=meshgrid(xtest1,xtest2);
z= p00 + p10.*x + p01.*y + p20.*x.^2 + p11.*x.*y + p02.*y.^2 + p30.*x.^3 + p21.*x.^2.*y + p12.*x.*y.^2 + p03.*y.^3;
surf(x,y,z,'EdgeColor','none'); view(20,30)
hold on
scatter3(x(:),y(:),zzz(:),'filled','SizeData',5,'MarkerFaceColor','k')
hold off
Thanks, I think the problem is probably due to 'meshgrid', but anyway I can create the plot by using 'surf' now.
No, I've revised my code above to use meshgrid(). There is still very close agreement.
Yeah, the problem is I use the 'meshgrid' incorrectly.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!