MATLAB:插值以找到直线和曲线之间的交点的x值

时间:2011-08-04 21:15:05

标签: arrays matlab interpolation intersection

这是我目前的图表 :

The Dotted Blue line represented the y value that corresponds to the x value I am looking for. I am trying to find the x values of the line's intersections with the blue curve(Upper)

虚线蓝线表示与我正在寻找的x值相对应的y值。我试图找到线与蓝色曲线(上部)的交点的x值。由于相互作用不落在已经定义的点上,我们需要插入一个落在上图上的点。

以下是我的信息:

LineValue - 交集的y值和虚线的值(y = LineValue) 频率 - 包含此图中所示x值坐标的数组。与LineValue对应的频率的插值是我们正在寻找的 上/下 - 包含此图的y值信息的数组

3 个答案:

答案 0 :(得分:8)

此解决方案是对Amro's answer的改进。您可以通过查找由与fzero进行逻辑比较而创建的系列的第一个差异中的转换,而不是使用LineValue来计算线的交集。所以,使用Amro的样本数据:

>> x = linspace(-100,100,100);
>> y =  1-2.*exp(-0.5*x.^2./20)./(2*pi) + randn(size(x))*0.002;
>> LineValue = 0.8;

查找超过LineValue的连续点段的起始索引:

>> idx = find(diff(y >= LineValue))

idx =

    48    52

然后,您可以使用加权平均值(即线性插值)计算交叉点的x位置:

>> x2 = x(idx) + (LineValue - y(idx)) .* (x(idx+1) - x(idx)) ./ (y(idx+1) - y(idx))

x2 =

         -4.24568579887939          4.28720287203057

将它们绘制起来以验证结果:

>> figure;
>> plot(x, y, 'b.-', x2, LineValue, 'go', [x(1) x(end)], LineValue*[1 1], 'k:');

enter image description here

这种方法的优点是:

  • 交叉点的确定是矢量化的,因此无论交叉点的数量如何都可以。
  • 算术上确定交叉点可能比使用fzero更快。

答案 1 :(得分:2)

使用FZERO的示例解决方案:

%# data resembling your curve
x = linspace(-100,100,100);
f = @(x) 1-2.*exp(-0.5*x.^2./20)./(2*pi) + randn(size(x))*0.002;
VALUE = 0.8;

%# solve f(x)=VALUE
z1 = fzero(@(x)f(x)-VALUE, -10);  %# find solution near x=-10
z2 = fzero(@(x)f(x)-VALUE, 10);   %# find solution near x=+10

%# plot
plot(x,f(x),'b.-'), hold on
plot(z1, VALUE, 'go', z2, VALUE, 'go')
line(xlim(), [VALUE VALUE], 'Color',[0.4 0.4 0.4], 'LineStyle',':')
hold off

screenshot

答案 2 :(得分:0)

数据系列中的步长是否相同? 控制方程是假设为立方,正弦等。?

doc interpl 找到过零点