简体   繁体   English

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

[英]MATLAB: Interpolating to find the x value of the intersection between a line and a curve

Here is the graph I currently have : 这是我目前的图表:

虚线蓝线表示与我正在寻找的x值相对应的y值。我试图找到线的交叉点的x值与蓝色曲线(上)

The Dotted Blue line represented the y value that corresponds to the x value I am looking for. 虚线蓝线表示与我正在寻找的x值相对应的y值。 I am trying to find the x values of the line's intersections with the blue curve(Upper).Since the interesections do not fall on a point that has already been defined, we need to interpolate a point that falls onto the Upper plot. 我试图找到线与蓝色曲线(上)的交点的x值。由于相互作用不落在已经定义的点上,我们需要插入一个落在上图上的点。

Here is the information I have: 这是我的信息:

LineValue - The y value of the intersection and the value of the dotted line( y = LineValue) Frequency - an array containing the x value coordinates seen on this plot. LineValue - 交集的y值和虚线的值(y = LineValue)Frequency - 包含此图中所示x值坐标的数组。 The interpolated values of Frequency that corresponds to LineValue are what we are looking for Upper/Lower - arrays containing the y value info for this graph 对应于LineValue的频率的插值是我们正在寻找的上/下 - 包含此图的y值信息的数组

This solution is an improvement on Amro's answer . 这个解决方案是对Amro答案的改进。 Instead of using fzero you can simply calculate the intersection of the line by looking for transition in the first-difference of the series created by a logical comparison to LineValue . 而不是使用fzero你可以简单地通过寻找一个逻辑比较,以创建系列的一阶差分转换计算线的交点LineValue So, using Amro's sample data: 所以,使用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;

Find the starting indices of those segments of consecutive points that exceed LineValue : 找到超过LineValue的连续点段的起始索引:

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

idx =

    48    52

You can then calculate the x positions of the intersection points using weighted averages (ie linear interpolation): 然后,您可以使用加权平均值(即线性插值)计算交叉点的x位置:

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

x2 =

         -4.24568579887939          4.28720287203057

Plot these up to verify the results: 绘制这些以验证结果:

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

在此输入图像描述

The advantages of this approach are: 这种方法的优点是:

  • The determination of the intersection points is vectorized so will work regardless of the number of intersection points. 交叉点的确定是矢量化的,因此无论交叉点的数量如何都将起作用。
  • Determining the intersection points arithmetically is presumably faster than using fzero . 算术上确定交叉点可能比使用fzero更快。

Example solution using FZERO : 使用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

截图

Are the step sizes in your data series the same? 数据系列中的步长是否相同? Is the governing equation assumed to be cubic, sinuisoidal, etc..? 控制方程是假设为立方,正弦等。?

doc interpl Find the zero crossings doc interpl找到过零点

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

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