简体   繁体   English

Matlab-最小二乘数据拟合-具有额外约束的成本函数

[英]Matlab - Least Squares data fitting - Cost function with extra constraint

I am currently working on some MatLab code to fit experimental data to a sum of exponentials following a method described in this paper . 我目前正在按照本文介绍的方法,对一些MatLab代码进行处理,以使实验数据适合指数总和。

According to the paper, the data has to follow the following equation (written in pseudo-code): 根据该论文,数据必须遵循以下方程式(用伪代码编写):

y = sum(v(i)*exp(-x/tau(i)),i=1..n)

Here tau(i) is a set of n predefined constants. tau(i)是一组n预定义常数。 The number of constants also determines the size of the summation, and hence the size of v . 常数的数量还决定了求和的大小,因此也决定了v的大小。 For example, we can try to fit a sum of 100 exponentials, each with a different tau(i) to our data. 例如,我们可以尝试拟合100个指数的总和,每个指数的tau(i)不同。 However, due to the nature of the fitting and the exponential sum, we need to add another constraint to the problem, and hence to the cost function of the least-squares method used. 但是,由于拟合和指数和的性质,我们需要为该问题添加另一个约束,从而需要为所使用的最小二乘法的成本函数添加约束。

Normally, the cost function of the least-squares method is given by: 通常,最小二乘法的成本函数由下式给出:

(y_data - sum(v(i)*exp(-x/tau(i)),i=1..n)^2

And this has to be minimized. 这必须最小化。 However, to prevent over-fitting that would make the time-constant spectrum extremely noisy, the paper adds the following constraint to the cost function: 但是,为防止过度拟合会使时间常数频谱变得非常嘈杂,本文在成本函数中添加了以下约束:

|v(i) - v(i+1)|^2

Because of this extra constraint, as far as I know, the regular algorithms, like lsqcurvefit aren't useable any longer, and I have to use fminsearch to search the minimum of my least-squares cost function with a constraint. 据我所知,由于这种额外的约束,像lsqcurvefit这样的常规算法不再可用,并且我必须使用fminsearch来搜索具有约束的最小二乘成本函数的最小值。 The function that has to be minimized, according to me, is the following: 据我说,必须最小化的功能如下:

(y_data - sum(v(i)*exp(-x/tau(i)),i=1..n)^2 + sum(|v(j) - v(j+1)|^2,j=1..n-1)

My attempt to code this in MatLab is the following. 下面是我在MatLab中编写代码的尝试。 Initially we define the function in a function script, then we use fminsearch to actually minimize the function and get values for v . 最初,我们在函数脚本中定义函数,然后使用fminsearch实际最小化函数并获取v值。

function res = funcost( v )
%FUNCOST Definition of the function that has to be minimised

%We define a function yvalues with 2 exponentials with known time-constants
% so we know the result that should be given by minimising.
xvalues = linspace(0,50,10000);
yvalues = 3-2*exp(-xvalues/1)-exp(-xvalues/10);

%Definition of 30 equidistant point in the logarithmic scale  
terms = 30;
termsvector = [1:terms];
tau = termsvector;
for i = 1:terms
    tau(i) = 10^(-1+3/terms*i);
end

%Definition of the regular function
res_1 = 3;

for i=1:terms
    res_1 =res_1+ v(i).*exp(-xvalues./tau(i));
end

res_1 = res_1-yvalues;

%Added constraint
k=1;
res_2=0;

for i=1:terms-1
    res_2 = res_2 + (v(i)-v(i+1))^2;
end

res=sum(res_1.*res_1) + k*res_2;

end

fminsearch(@funcost,zeros(30,1),optimset('MaxFunEvals',1000000,'MaxIter',1000000))

However, this code is giving me inaccurate results (no error, just inaccurate results), which leads me to believe I either made a mistake in the coding or in the interpretation of the added constraint for the least-squares method. 但是,这段代码给我带来了不正确的结果(没有错误,只是不准确的结果),这使我相信我在编码或对最小二乘法的附加约束的解释中都犯了错误。

I would try to introduce the additional constrain in following way: 我将尝试通过以下方式介绍其他约束:

res_2 = max((v(1:(end-1))-v(2:end)).^2);

eg instead of minimizing an integrated (summed up) error, it does minmax. 例如,它没有最小化积分(累加)错误,而是最小最大值。

You may also make this constrain stiff by 您还可以通过以下方式使此约束更严格

if res_2 > some_number
    k = a_very_big_number;
else
    k=0; % or k = a_small_number
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM