MATLAB: Interpolation to find the x value of the intersection between a line and a curve

Here is the schedule that I have

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)

The Dotted Blue line represents the y value corresponding to the x value I am looking for. I am trying to find the x values ​​of the intersections of lines with a blue curve (Upper). Since interesections do not fall into a point that is already defined, we need to interpolate a point that falls into the upper graph.

Here is the information I have:

LineValue - the y value for the intersection and the value of the dashed line (y = LineValue) Frequency - an array containing the coordinates of the x value that are visible on this graph. The interpolated Frequency values ​​that correspond to LineValue are what we are looking for. Upper / lower - arrays containing information about the y value for this plot.

+4
source share
3 answers

An example solution using 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
source

This solution is an improvement on Amro's answer . Instead of using fzero you can simply calculate the intersection of the line by looking for the transition in the first difference of the series created by a logical comparison with LineValue . So, using Amro data:

  >> 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 consecutive point segments that exceed LineValue :

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

 idx =

     48 52

Then you can calculate the x position of the intersection points using weighted averages (e.g. linear interpolation):

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

 x2 =

          -4.24568579887939 4.28720287203057

Schedule them to check the results:

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

enter image description here

The advantages of this approach are as follows:

  • The definition of intersection points is vectorized, so it will work regardless of the number of intersection points.
  • Defining intersection points is arithmetically supposedly faster than using fzero .
+6
source

Are the step sizes in your data series the same? Is the control equation supposed cubic, sinusoidal, etc.?

doc interpl Find the intersection of zero

0
source

All Articles