如何解决MATLAB:使用 MultiStart 进行非线性拟合可找到局部但不是全局最小值
我正在为一些非线性数据拟合一个函数。但是,我得到的输出是:
Attempt to execute SCRIPT MultiStart as a function:
/MATLAB Drive/Amsterdam/2 Fitting/MultiStart.m
Error in MultiStart (line 42)
ms = MultiStart('PlotFcn',@gsplotbestf);
这是我的代码。我已经指定了参数、初始值、目标函数和边界。我的代码确实找到了局部最小值,但找不到全局最小值。
function [EPSC,IPSC,CPSC,t] = generate_current(G_max_chl,G_max_glu,EGlu,EChl,Vm,tau_rise_In,tau_decay_In,tau_rise_Ex,tau_decay_Ex,tmax)
dt = 0.1; % time step duration (ms)
t = 0:dt:tmax-dt;
% Compute compound current
IPSC = ((G_max_chl) .* ((1 - exp(-t / tau_rise_In)) .* exp(-t / tau_decay_In)) * (Vm - EChl));
EPSC = ((G_max_glu) .* ((1 - exp(-t / tau_rise_Ex)) .* exp(-t / tau_decay_Ex)) * (Vm - EGlu));
CPSC = IPSC + EPSC;
end
用前面的函数生成数据后,我有拟合过程:
% Simulated data
[~,~,t] = generate_current(80,15,-70,-30,0.44,0.73,3,120);
% % Parameters
G_max_chl = 80; % Maximal conductance of Chl
G_max_glu = 15; % Maximal conductance of Glu
tau_rise_Ex = 0.73; % Tau rise for E
tau_rise_In = 0.44; % Tau rise for I
tau_decay_Ex = 3; % Tau decay for E
tau_decay_In = 15; % Tau decay for I
p = [G_max_chl,tau_decay_Ex];
dt = 0.1;
tmax = 121;
t = 1:0.1:tmax-dt;
Vm = -30;
EGlu = 0;
EChl = -70;
% Create the objective function
fitfcn = @(p,t,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));
preal = [80,3]; % Real coefficients
xdata = t;
ydata = CPSC; % Response data with noise
% Set bounds and initial point.
lb = [0,0];
ub = [150,150,5,20,20];
p0 = [50,50,1,1]; % Arbitrary initial points
% Find the best local fit
[xfitted,errorfitted] = lsqcurvefit(@(p,t)fitfcn(p,EChl),p0,xdata,ydata,lb,ub)
%Set up the problem for MultiStart.
problem = createOptimProblem('lsqcurvefit','x0','objective',fitfcn,...
'lb','ub',ub,'xdata','ydata',ydata);
% Find a global solution
ms = MultiStart('PlotFcn',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,200)
解决方法
您在位置 MultiStart.m
中有一个名为 /MATLAB Drive/Amsterdam/2 Fitting
的脚本,该位置位于 Matlab 的搜索路径中。当您调用 MultiStart()
时,Matlab 假定您正在调用此脚本而不是内置的 MultiStart
函数。
您需要将脚本重命名为与 Matlab 的内置函数名称不冲突的名称,或者,remove /MATLAB Drive/Amsterdam/2 Fitting
来自 Matlab 的搜索路径。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。