MATLAB: Is it possible to perform a non-linear surface fit using MATLAB

233ddfitlsqcurvefitOptimization Toolboxregressionsurface

I am trying to fit a non-linear surface between the "x" , "y" and "z" vectors to a function of the form z = f(x, y).

Best Answer

This enhancement has been incorporated in MATLAB 7.8 (R2009a).
The Curve Fitting Toolbox 2.0 (R2009a) can be used to fit surfaces to data both interactively and programmatically. For more information on surface fitting in Curve Fitting Toolbox 2.0 (R2009a), refer to the following online documentation:
Several examples can be found toward the bottom of the FITOPTIONS page:
For previous versions of MATLAB, try the following workarounds:
Non-linear data fitting can be performed using routines from the Optimization Toolbox by minimizing a function, or in some cases, the error. Below is a small example on how this can be done using the function LSQCURVEFIT.
To fit "x", "y", and "z" data to the following function:
z = a1yx^2 + a2sin(x) + a3y^3;
use the following MATLAB function:
function F = myfun(a, data)
x = data(1, :);
y = data(2, :);
F = a(1)*y.*x.^2 + a(2)*sin(x) + a(3)*y.^3;
xdata = [3.6 7.7 9.3 4.1 8.6 2.8 1.3 7.9 10.0 5.4];
ydata = [16.5 150.6 263.1 24.7 208.5 9.9 2.7 163.9 325.0 54.3];
zdata = [95.09 23.11 60.63 48.59 89.12 76.97 45.68 1.84 82.17 44.47];
data = [xdata; ydata];
a0 = [10 10 10]; % Starting guess
[a, resnorm] = lsqcurvefit(@myfun, a0, data, zdata)
This produces the following result:
a =
0.0074 -19.9749 -0.0000
resnorm =
2.1959e+004
For more curve fitting options, see the Related Solution listed below.
Related Question