在我看来,不需要外部工具,方便cftool
可以在大多数情况下为您提供帮助。它将为您生成以下功能:
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
最后,您只需要一个脚本来评估您的 fitobject:
[res,gof] = expfit(x,y,z)
% gives you the coeffcients
coeffvalues(res{1})
简短的版本(没有额外的功能和情节)将是:
[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} )
请注意,对于二维拟合,您需要使用prepareCurveData
而不是prepareSurfaceData
. 这些是 Matlab 内置函数,在某种程度上类似于meshgrid
,但特别是“准备”用于曲线/曲面拟合。