高斯非线性最小二乘法(全局优化)拟合

高斯拟合属于非线性拟合,这里最小二乘法,全局最优拟合方法为例,写一个代码,供大家参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
close all;
clear all;
clc;
%% Pharmacokinetic Data
t = -10:0.2:10;              %#ok<*NOPTS>
c = 5*exp(-((t)/4).^2)+randn(size(t))*0.1;
plot(t,c,’o’), xlabel(‘t’), ylabel(‘c’);hold on
%% 3 Compartment Model
model = @(b,t) (b(1)+b(2)*exp(-((t-b(3))./b(4)).^2))
%% Define Optimization Problem
problem = createOptimProblem(‘lsqcurvefit’,  ‘objective’, model, ‘xdata’, t, ‘ydata’, c, ‘x0’,ones(1,4))
%   ‘lb’, [-10 -10 -10  0],’ub’, [ 10  10  10 0.5],’options’,optimset(‘OutputFcn’, @curvefittingPlotIterates)
%% solve
% b = lsqcurvefit(problem)  %Direct fitting may lead to local optimum
%% Multistart
ms = MultiStart(‘PlotFcns’,@gsplotbestf)
[b,fval,exitflag,output,solutions] = run(ms, problem, 100)                   %#ok<*NASGU,*ASGLU>
%%
figure;
plot(t,c,’o’), xlabel(‘t’), ylabel(‘c’);hold on
hold on;
xfdata=-10:0.2:10;
yfdata= b(1)+b(2)*exp(-((xfdata-b(3))./b(4)).^2);
plot(xfdata,yfdata,’r’)

hold off;

 

(1)迭代过程

ezgif.com-video-to-gif

   (2) 拟合结果

注:18行代码,为直接用最小二乘法拟合,但这种方法往往只能找到局部最优。

 

Updated: 2015-01-10 — pm5:51

3
Leave a Reply

1 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
  Subscribe  
newest oldest most voted
Notify of
谁染霜林醉

点个赞,话说 ms = MultiStart(‘PlotFcns’,@gsplotbestf) 我变成 ms = MultiStart 这样会有什么区别呢???