用亥维赛阶函数拟合数据

时间:2020-12-25 06:47:08

I have some code to fit data to heaviside function. However, the fitting routine is not working properly. It varies so much dependent on the initial conditions and cant find the minimum. Can someone help? I have pasted the code below, the sample script and attached the file with the y-data.

我有一些代码可以将数据应用到亥维赛函数中。然而,安装程序不能正常工作。它的变化很大程度上取决于初始条件,无法找到最小值。有人可以帮忙吗?我粘贴了下面的代码、示例脚本,并将y数据附加到文件中。

ydata=[10 8 12 8 14 9 11 10 200 210 190 190 201 205 203 206 185 30 28 32 35 28 33 29];  
n=length(ydata);
xdata=[1:n];

%function
predicted = @(a,xdata) a(1)*heaviside(xdata-a(2))-a(3)*heaviside(xdata-a(4));

%initial conditions
a0 = [10;8;200;18];

% Fit model to data.
[ahat,resnorm,residual,exitflag,output,lambda,jacobian]=lsqcurvefit(predicted,a0,xdata,ydata);

% Plot fit with data.
plot(xdata,ydata);   

%plot fits
opts = fitoptions( 'Method', 'NonlinearLeastSquares');

%plot fit function
fitfinal = ahat(1)*heaviside(xdata-ahat(2))-ahat(3)*heaviside(xdata-ahat(4));

hold on 
plot(xdata,fitfinal)
hold off

In the end I would like to extract the lengths of the horizontal sections.

最后,我想要提取横截面的长度。

1 个解决方案

#1


1  

This is a trivial function to fit. Simply look for the two places where there is a big jump:

这是一个平凡的函数。只要看看有一个大跳跃的两个地方:

plot(ydata)
hold on

dy = abs(diff(ydata));
[~,index] = sort(dy,'descend');

% First segment = 1:index(1)
m = mean(ydata(1:index(1)));
plot([1,index(1)],[m,m])

% Second segment = index(1)+1:index(2)
m = mean(ydata(index(1)+1:index(2)));
plot([index(1)+1,index(2)],[m,m])

% Third segment = index(2)+1:length(ydata)
m = mean(ydata(index(2)+1:length(ydata)));
plot([index(2)+1,length(ydata)],[m,m])

As a "fitted function" I've plotted the segments using the mean of the function across those samples, but all the information is there to compose a function using two Heaviside functions.

作为一个“拟合函数”,我已经用函数的平均值在这些样本上绘制了分段,但是所有的信息都是用两个亥维赛函数组成一个函数。

Also, I've taken xdata implicitly as the indices. But you can make that explicit also if your sample locations are irregular.

另外,我已经隐式地将xdata作为索引。但是如果你的样本位置是不规则的,你也可以把它弄清楚。

#1


1  

This is a trivial function to fit. Simply look for the two places where there is a big jump:

这是一个平凡的函数。只要看看有一个大跳跃的两个地方:

plot(ydata)
hold on

dy = abs(diff(ydata));
[~,index] = sort(dy,'descend');

% First segment = 1:index(1)
m = mean(ydata(1:index(1)));
plot([1,index(1)],[m,m])

% Second segment = index(1)+1:index(2)
m = mean(ydata(index(1)+1:index(2)));
plot([index(1)+1,index(2)],[m,m])

% Third segment = index(2)+1:length(ydata)
m = mean(ydata(index(2)+1:length(ydata)));
plot([index(2)+1,length(ydata)],[m,m])

As a "fitted function" I've plotted the segments using the mean of the function across those samples, but all the information is there to compose a function using two Heaviside functions.

作为一个“拟合函数”,我已经用函数的平均值在这些样本上绘制了分段,但是所有的信息都是用两个亥维赛函数组成一个函数。

Also, I've taken xdata implicitly as the indices. But you can make that explicit also if your sample locations are irregular.

另外,我已经隐式地将xdata作为索引。但是如果你的样本位置是不规则的,你也可以把它弄清楚。