Matlab fminsearch中的错误以解决此4变量问题?

Matlab fminsearch中的错误以解决此4变量问题?,matlab,fminsearch,Matlab,Fminsearch,我试图用fminsearch解下面的方程,但我认为目标函数是错误的 我应该如何编写目标函数或修改代码的任何其他部分?这基本上是一个拟合问题,优化过程应将方程拟合到给定数据 % Consider the following data: Data = ... [0.0000 5.8955 0.1000 3.5639 0.2000 2.5173 0.3000 1.9790 0.4000 1.8990 0.5000 1.3938

我试图用
fminsearch
解下面的方程,但我认为目标函数是错误的

我应该如何编写目标函数或修改代码的任何其他部分?这基本上是一个拟合问题,优化过程应将方程拟合到给定数据

% Consider the following data:

Data = ...
  [0.0000    5.8955
   0.1000    3.5639
   0.2000    2.5173
   0.3000    1.9790
   0.4000    1.8990
   0.5000    1.3938
   0.6000    1.1359
   0.7000    1.0096
   0.8000    1.0343
   0.9000    0.8435
   1.0000    0.6856
   1.1000    0.6100
   1.2000    0.5392
   1.3000    0.3946
   1.4000    0.3903
   1.5000    0.5474
   1.6000    0.3459
   1.7000    0.1370
   1.8000    0.2211
   1.9000    0.1704
   2.0000    0.2636];

% Let's plot these data points.
t = Data(:,1);
y = Data(:,2);

plot(t,y,'ro')
title('Data points')
hold on

% fit the function: y =  c(1)*exp(-lam(1)*t) + c(2)*exp(-lam(2)*t)
%
% define the parameters in terms of one variable x:
%  x(1) = c(1)
%  x(2) = lam(1)
%  x(3) = c(2)
%  x(4) = lam(2)
%
% Then define the curve as a function of the parameters x and the data t:

F = @(x,t)(x(1)*exp(-x(2)*t) + x(3)*exp(-x(4)*t));

% We arbitrarily set our initial point x0 as follows: c(1) = 1,
% lam(1) = 1, c(2) = 1, lam(2) = 0:

x0 = [1 1 1 0];

% We run the solver and plot the resulting fit
options = optimset('TolFun',1e-5,'TolX',1e-5,'MaxFunEvals',10,'MaxIter',4000,'Display','iter');
[x,fval,exitflag,output] = fminsearch(F,x0,options)

plot(t,F(x,t))
hold off

你是对的,你的目标函数没有意义。你可以做一个测试。那么目标函数应定义为:

F = @(x,t) (x(1)*exp(-x(2)*t) + x(3)*exp(-x(4)*t));
Obj = @(x) (norm(F(x, Data(:,1))-Data(:,2)));
然后

给我:

编辑:

假设您已经知道一个参数,并且希望使用函数句柄,您可以这样做

p = ... % Your calculation to get the parameter. In your case x(3) from the previous F
F = @(x, p, t) (x(1)*exp(-x(2)*t) + p*exp(-x(3)*t));
helper = @(x, p) (norm(F(x, p, Data(:,1))-Data(:,2)));
Obj = @(x) (helper(x, p));

x0 = [1 1 0]; % Note that there's now one variable/parameter less
options = optimset('TolFun',1e-5, 'TolX', 1e-5, 'MaxFunEvals',1000, 'MaxIter', 4000,'Display','iter');
[x,fval,exitflag,output] = fminsearch(Obj,x0,options);

tp = 0:0.01:2;
plot(Data(:,1), Data(:,2), 'ro');
title('Data points')
hold on
plot(tp,F(x,p,tp)); % Note that you need to pass p to F
hold off

你是对的,你的目标函数没有意义。你可以做一个测试。那么目标函数应定义为:

F = @(x,t) (x(1)*exp(-x(2)*t) + x(3)*exp(-x(4)*t));
Obj = @(x) (norm(F(x, Data(:,1))-Data(:,2)));
然后

给我:

编辑:

假设您已经知道一个参数,并且希望使用函数句柄,您可以这样做

p = ... % Your calculation to get the parameter. In your case x(3) from the previous F
F = @(x, p, t) (x(1)*exp(-x(2)*t) + p*exp(-x(3)*t));
helper = @(x, p) (norm(F(x, p, Data(:,1))-Data(:,2)));
Obj = @(x) (helper(x, p));

x0 = [1 1 0]; % Note that there's now one variable/parameter less
options = optimset('TolFun',1e-5, 'TolX', 1e-5, 'MaxFunEvals',1000, 'MaxIter', 4000,'Display','iter');
[x,fval,exitflag,output] = fminsearch(Obj,x0,options);

tp = 0:0.01:2;
plot(Data(:,1), Data(:,2), 'ro');
title('Data points')
hold on
plot(tp,F(x,p,tp)); % Note that you need to pass p to F
hold off

谢谢,除了最小二乘法之外,还有其他更精确/稳健的拟合吗?如果您安装了曲线拟合工具箱,您可以看看哪一个具有稳健拟合选项,因为离群值在拟合中的权重较低。但是对于您的示例数据,不存在任何异常值,因此最小二乘拟合应该很好。在另一方面,你可以做一个。还有一个内置的粒子群优化matlab@JohnnyDrama非常感谢。当x(3)来自ode45时,你能建议F和obj将如何变化吗?@JohnnyDrama,这非常有帮助,谢谢!我试图使上述代码适应一个新问题,即ode45中的参数需要合适,但我无法使其工作。您能看一下代码吗?我已将其粘贴在codeshare上,可以在那里编辑-谢谢,除了最小二乘法之外,还有其他更精确/稳健的拟合吗?如果您安装了曲线拟合工具箱,您可以查看哪一个具有稳健拟合选项,因为离群值在拟合中的权重较低。但是对于您的示例数据,不存在任何异常值,因此最小二乘拟合应该很好。在另一方面,你可以做一个。还有一个内置的粒子群优化matlab@JohnnyDrama非常感谢。当x(3)来自ode45时,你能建议F和obj将如何变化吗?@JohnnyDrama,这非常有帮助,谢谢!我试图使上述代码适应一个新问题,即ode45中的参数需要合适,但我无法使其工作。您能看一下代码吗?我已经把它粘贴在代码共享上,可以在那里编辑-