利用cftool生成一下代码:直接把U和I的值输进去,调用就可以了
function [fitresult, gof] = createFit1(U, I)
%CREATEFIT1(U,I)
% Create a fit.
%
% Data for 'fit0' fit:
% X Input : U
% Y Output: I
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 17-May-2016 19:42:14
%% Fit: 'fit0'.
[xData, yData] = prepareCurveData( U, I );
% Set up fittype and options.
ft = fittype( 'a+b*log(x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.Display = 'Off';
opts.Lower = [-Inf -Inf];
opts.StartPoint = [0.138624442828679 0.149294005559057];
opts.Upper = [Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'fit0' );
h = plot( fitresult, xData, yData );
legend( h, 'I vs. U', 'fit0', 'Location', 'NorthEast' );
% Label axes
xlabel( 'U' );
ylabel( 'I' );
grid on
调用格式:
>>[fitresult, gof] = createFit1(U, I)
结果如图: