MATLAB Answers

## Coefficents not reflecting the curve obtained using fit function

Asked by Harry Mahon

### Harry Mahon (view profile)

on 10 Apr 2019
Latest activity Edited by David Wilson

### David Wilson (view profile)

on 10 Apr 2019
Accepted Answer by David Wilson

### David Wilson (view profile)

I am trying to write a piece of code that outputs the constants a and b for a curve of the following format:
f(x)=1/(1+exp(1).^(b*(x-a)))
My code (below) is producing the logistic function plot (below) which i believe to be correct, however the coefficients being extracted from the code are not representative of the plot being produced. Is there a fix for this which would mean that the correct values of a and b could be extracyed from the function? Any help would be much appreciated
function [a,b] = fit_lightoff(Temperature,Gas_Conversion)
ft = fittype( '1/(1+exp(1).^(-b*(x-a)))');
options = fitoptions;
options.Normal = 'on';
f=fit(Temperature,Gas_Conversion, ft, options)
MyCoeffs=coeffvalues(f);
a= MyCoeffs(1);
b= MyCoeffs(2);
h = plot( f, Temperature,Gas_Conversion);
end #### 0 Comments

Sign in to comment.

## 1 Answer

Answer by David Wilson

### David Wilson (view profile)

on 10 Apr 2019
Edited by David Wilson

### David Wilson (view profile)

on 10 Apr 2019
Accepted Answer

I suspect that the problem is that you have normalised the data, and then forgot that when you looked at the parameter values.
For example, since you didn't bother give us any data to work with, I've made my own to roughly follow yours. In this case I know the true parameters, "a" & "b". I've also added a small amount of noise just for fun.
x = linspace(180,380,8)';
a = 280; b=0.05; % approx constants
y = 1.0./(1+exp(-b*(x-a))) + 0.05*randn(size(x))
plot(x,y,'o')
If you plot this data, you'll see something similar to your original data points.
Now we are ready to fit. Always a good idea to give it a reasonable starting guess.
Temperature=x; Gas_Conversion=y;
ft = fittype( '1.0./(1+exp(-b*(x-a)))');
options = fitoptions('method','NonlinearLeastSquares'); % Must do this, in order to specify an initial guess
options.Normal = 'off'; % TURN OFF !!! (or re-scale the coefficients)
options.StartPoint = [280, 0.05]'; % default start point for a & b
f=fit(Temperature,Gas_Conversion, ft, options)
MyCoeffs=coeffvalues(f);
a= MyCoeffs(1);
b= MyCoeffs(2);
h = plot( f, Temperature,Gas_Conversion);
Now we get a reasonable plot with
f =
General model:
f(x) = 1.0./(1+exp(-b*(x-a)))
Coefficients (with 95% confidence bounds):
a = 278 (272.3, 283.7)
b = 0.06609 (0.04472, 0.08745)
sensible coefficients, close to what I generated the original data with.

#### 0 Comments

Sign in to comment.