Hi all,
I've been plotting a surface plot using surf(x, y, z) where x is a 1 x 8 double, y is a 1 x 11 double and z is an 11 x 8 double and fitting that surface using cftool with no issues. I now need to change my approach slightly and perform the fit programatically (as opposed to using cftool).
However surffit = fit([x,y],z,'poly32','normalize','on') does not accept my current format. How I can I adjust or massage my raw data (x, y and z) for surffit to accept it?
Thanks

 Accepted Answer

Use meshgrid to make a grid out of your x and y vectors:
x = 1:8;
y = 1:11;
z = rand(11,8);
[xx,yy] = meshgrid(x,y);
surf(xx,yy,z)

3 Comments

Tim Fulcher
Tim Fulcher on 27 Aug 2023
Edited: Tim Fulcher on 27 Aug 2023
Thanks cyclist. I punched in :
[XX, YY] = meshgrid(X, Y);
figure;
surf(XX, YY, Z);
and that worked fine but when I tried:
surffit = fit([XX, YY], Z, 'poly32', 'normalize', 'on');
Matlab objected and gave me the following errors:
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 ICRU_Report_49_Geant4_Water_800_1mm_Samples (line 342)
surffit = fit([XX, YY], Z, 'poly32', 'normalize', 'on');
Sorry, I should have mentioned that the surface fitting function requires you to turn all those matrices into vector again. The key concept being that all the vectors need to be the same length, and specify a series of (x,y,z) coordinates.
You should double-check that my syntax really put the correct z with the corresponding (x,y). It's easy to get the (x,y) swapped.
x = 1:8;
y = 1:11;
z = rand(11,8);
[xx,yy] = meshgrid(x,y);
surf(xx,yy,z)
surffit = fit([xx(:), yy(:)], z(:), 'poly32', 'normalize', 'on')
Linear model Poly32: surffit(x,y) = 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 where x is normalized by mean 4.5 and std 2.304 and where y is normalized by mean 6 and std 3.18 Coefficients (with 95% confidence bounds): p00 = 0.4901 (0.371, 0.6093) p10 = -0.06547 (-0.2478, 0.1169) p01 = -0.0336 (-0.1297, 0.06249) p20 = -0.02249 (-0.09529, 0.05031) p11 = -0.004547 (-0.06809, 0.059) p02 = 0.03962 (-0.03234, 0.1116) p30 = 0.01978 (-0.06944, 0.109) p21 = 0.01987 (-0.05335, 0.09309) p12 = 0.04835 (-0.02401, 0.1207)
Thanks cyclist that all worked great.

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!