Nonlinear fitting - No output function with MultiStart
2 views (last 30 days)
Show older comments
Samuele Bolotta
on 15 Mar 2021
Commented: Samuele Bolotta
on 16 Mar 2021
I'm fitting a function to some nonlinear data. However, the output that I get is:
MultiStart with properties:
UseParallel: 0
Display: 'final'
FunctionTolerance: 1.0000e-06
MaxTime: Inf
OutputFcn: []
PlotFcn: []
StartPointsToRun: 'all'
XTolerance: 1.0000e-06
No plot and not output function.
This is the code:
% Parameters
p(1) = G_max_chl;
p(2) = G_max_glu;
p(3) = tau_rise_In;
p(4) = tau_decay_In;
p(5) = tau_rise_Ex;
p(6) = tau_decay_Ex;
% Create the objective function
fitfcn = @(p, Vm, EGlu, EChl, dt, tmax) ((p(1)) .* ((1 - exp(-(0:dt:tmax-dt) / p(3))) .* exp(-(0:dt:tmax-dt) / p(4))) * (Vm - EChl)) + ((p(2)) .* ((1 - exp(-(0:dt:tmax-dt) / p(5))) .* exp(-(0:dt:tmax-dt) / p(6))) * (Vm - EGlu));
% Create the training data
rng default % For reproducibility
N = 200; % Number of data points
preal = [80,15,0.44,0.73,15,3]; % Real coefficients
Vm = -30;
Eglu = 0;
EChl = -70;
dt = 0.1;
tmax = 120;
ydata = fitfcn(preal,xdata); % Response data with noise
ydata = awgn(ydata,35,'measured');
% Set bounds and initial point.
lb = [0,0,0,0,0,0];
ub = [150,150,5,5,20,20];
p0 = 5*ones(1,6); % Arbitrary initial points
% Find the best local fit
[xfitted,errorfitted] = lsqcurvefit(fitfcn,p0,xdata,ydata,lb,ub)
%Set up the problem for MultiStart.
problem = createOptimProblem('lsqcurvefit','x0',p0,'objective',fitfcn,...
'lb',lb,'ub',ub,'xdata',xdata,'ydata',ydata);
% Find a global solution.
ms = MultiStart('PlotFcns',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,200)
I am not sure what I'm doing wrong.
0 Comments
Accepted Answer
Star Strider
on 15 Mar 2021
Unfortunately, this:
fitfcn = @(p, Vm, EGlu, EChl, dt, tmax) ((p(1)) .* ((1 - exp(-(0:dt:tmax-dt) / p(3))) .* exp(-(0:dt:tmax-dt) / p(4))) * (Vm - EChl)) + ((p(2)) .* ((1 - exp(-(0:dt:tmax-dt) / p(5))) .* exp(-(0:dt:tmax-dt) / p(6))) * (Vm - EGlu));
is not going to work with lsqcurvefit, or likely any other optimisation function.
The problems (that I can easily see) are:
fitfcn = @(p, Vm, EGlu, EChl, dt, tmax) ((p(1)) .* ((1 - exp(-(0:dt:tmax-dt) / p(3))) .* exp(-(0:dt:tmax-dt) / p(4))) * (Vm - EChl)) + ((p(2)) .* ((1 - exp(-(0:dt:tmax-dt) / p(5))) .* exp(-(0:dt:tmax-dt) / p(6))) * (Vm - EGlu));
↑ ← 't MUST BE AN INDEPENDENT VARIABLE ↑ ↑ ← ↑ ← EXTRA PARAMETERS ↑ ← t ↑← 't ↑ ← ↑ ← EXTRA PARAMETERS
So it should be:
fitfcn = @(p, t, Vm, EGlu, EChl) ((p(1)) .* ((1 - exp(-t / p(3))) .* exp(-t / p(4))) * (Vm - EChl)) + ((p(2)) .* ((1 - exp(-t / p(5))) .* exp(-t / p(6))) * (Vm - EGlu));
and the lsqcurvefit call:
B = lsqcurvefit(@(p,t)fitfcn(p, t, Vm, EGlu, EChl), B0, t, ydata)
with ‘Vm’, ‘EGlu’ and ‘EChl’ existing in the calling function workspace.
That should come close to working, although it may require some revision.
I cannot test it so I am posting it as UNTESTED CODE.
5 Comments
Star Strider
on 16 Mar 2021
The error appears to be here:
ms = MultiStart('PlotFcns',@gsplotbestf);
that will likely be solved by eliminating the ‘s’:
ms = MultiStart('PlotFcn',@gsplotbestf);
I usually use ga (genetic algorithm) of this sort of problem because I have more experience with it. I have used GlobalSearch as well a few times, however not MultiStart.
If the data are generated by your actual objective function to which you just added noise, the Golbal Optimization Toolbox functions will likely converge on the correct parameter set providing that the noise does not completely obscure the underlying signal. Providing the correct magnitudes of the initial parameter set is likely important (or likewise using the bound limits to force them into a specific range of magnitudes), however the initial parameter estimates provided to global optimisation functions are likely less important because the algorithms will eventually find the best set.
More Answers (0)
See Also
Categories
Find more on Global or Multiple Starting Point Search in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!