简体   繁体   中英

MatLab function not properly curve fitting

I have an extremely basic question seeking to answer why the following function in MatLab does not properly fit the given data to a logistic curve, given the correct equation for one. The solution to this is likely quite simple, but I am entirely unfamiliar with the MatLab curve fitting toolbox. My code is as follows:

function [fitresult, gof] = createFit1(Dose, Response)
%CREATEFIT1(DOSE,RESPONSE)
%  Create a fit.
%
%  Data for 'untitled fit 1' fit:
%      X Input : Dose
%      Y Output: Response
%  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 08-Sep-2022 20:16:26


%% Fit: 'untitled fit 1'.

Dose = [1.6*10^4;1.84*10^4;2.08*10^4;2.32*10^4; 2.56*10^4;2.8*10^4;3.04*10^4;3.28*10^4;3.52*10^4;3.76*10^4;4*10^4;4.24*10^4]
Response = [0;0.1004;0.2830;0.4338;0.5638;0.6834;0.8030;0.8680;0.9294;0.9614;0.9978;1]
[xData, yData] = prepareCurveData( Dose, Response );

% Set up fittype and options.
ft = fittype( '1/(1+exp(-k*Dose))', 'independent', 'Dose', 'dependent', 'Response' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = 0.0755269568571602;

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'Response vs. Dose', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'Dose', 'Interpreter', 'none' );
ylabel( 'Response', 'Interpreter', 'none' );
grid on

They are probably two causses of bad fitting.

First: Obviously the points are not located close to a simple logistic curve but close to a shifted logistic curve. So, it is suggested to change the model equation in your code.

在此处输入图像描述

Second: The non-linear regression is an iterative process requiring to set some guessed initial values of parameters. If the initial values are too far from the unknown exact values the calculus might fail to achieve a good fit.

If you don't know how to guess some initial values of the parameters better use a not iterative method which doesn't requires initial values as shown below.

在此处输入图像描述

Numerical example with your data:

在此处输入图像描述

If you want to use your non-linear regression software you can use the above good approximates of the parameters to initiate the iterative process.

An even better fitting would be obtained with a not only horizontal shift (horizontal and vertical shifts).

For information on the non-iterative method used above (linear fitting of an integral equation to which the logistic function is solution ): https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM