In my eyes there is no need for an external tool, the convenient cftool
can help you in most of the cases. It will generate you the following function:
function [fitresult, gof] = expfit(x, y, z)
[xData, yData, zData] = prepareSurfaceData( x, y, z );
% Set up fittype and options.
ft = fittype( 'c0 + c1.* exp(-(c2 .* x)) + c3.* (y.^1);', 'independent', {'x', 'y'}, 'dependent', 'z' );
opts = fitoptions( ft );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf -Inf];
opts.StartPoint = [0.0975404049994095 0.278498218867048 0.546881519204984 0.957506835434298];
opts.Upper = [Inf Inf Inf Inf];
% Fit model to data.
[fitresult{1}, gof(1)] = fit( [xData, yData], zData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult{1}, [xData, yData], zData );
legend( h, 'untitled fit 1', 'z vs. x, y', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
grid on
Finally you just need a script to evaluate your fitobject:
[res,gof] = expfit(x,y,z)
% gives you the coeffcients
coeffvalues(res{1})
The short version (without additional function and plot) would be:
[xData, yData, zData] = prepareSurfaceData( x, y, z );
functionToFit = 'c0 + c1.* exp(-(c2 .* x)) + c3.* (y.^1);';
ft = fittype( functionToFit, 'independent', {'x', 'y'}, 'dependent', 'z' );
fitobj = fit( [xData, yData], zData, ft );
coeffvalues( fitobj{1} )
Be aware that for 2-dimensional fits you need to use prepareCurveData
instead of prepareSurfaceData
. These are Matlab built-in functions somehow similiar to meshgrid
, but especially "prepared" for curve/surface fits.