Asked by Serena Solanki
on 4 Nov 2017

I have plotted a graph based on known sets of data for x and y. I have then added a horizontal line to the plot at the y-value where I want to find out the corresponding x-values (shown in red on the plot).

I want to know what the x-values are where these two lines intercept. I do not have the equation for the blue signal. Is there a function/ command I could use that will do this?

% step one of inputting file

filename='Test_Results_19.xls'

ColumnA= xlsread(filename,'A2:A2088')

ColumnB= xlsread(filename,'B2:B2088')

B=ColumnB

A=ColumnA

%Step 2- do the FFT of the values

Z=fft(A)

W=fft(B)

%Step 3 find the modulus of values in column B

X=abs(Z)

Y=abs(W)

plot(X,Y)

title('Magnitude of frequency against frequency')

xlabel('Frequency')

ylabel('H(w)')

%finding the peak value and W1 and W2

[fmax, imax]=max(Y);

Qmax=fmax;

Q2=(Qmax/sqrt(2));

%Need to find corresponding x-values to the found y-values

x1=0

x2=45*10^4

y1=Q2

plot(X,Y,[x1 x2],[y1 y1])

hold on;

Answer by Akira Agata
on 7 Nov 2017

One possible way to find the threshold-crossing points is interpolating the data by spline function and find the crossing points by using fzero function. The following is the sample script to do this.

% Sample data y = f(x)

x = linspace(-2*pi, 2*pi, 15);

y = sech(x) + 0.2*rand(size(x));

% Threshold

th = 0.5;

% Spline interpolation

pp = spline(x,y);

% Find the indexes where y(i) < th and y(i+1) > th

idx = find((y(1:end-1)-th).*(y(2:end)-th) < 0);

% Find the root of f(x)-th = 0

x0 = zeros(size(idx));

y0 = zeros(size(idx));

pt = 1;

for kk = idx

x1 = x(kk);

func = @(x) pp.coefs(kk,:)*[(x-x1)^3; (x-x1)^2; (x-x1);1]-th;

[x0(pt),y0(pt)] = fzero(func,x(kk));

pt = pt+1;

end

% Show the result

xx = -2*pi:0.1:2*pi;

yy = spline(x,y,xx);

figure

plot(x,y,'-o')

hold on

plot(xx,yy)

plot([-2*pi 2*pi],[th th])

plot(x0,y0+th,'ro')

legend({'Data','Spline interpolation','Threshold','Estimated points'})

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Rik (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/365105-finding-x-value-with-known-y-value-on-plot#comment_501043

## Serena Solanki (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/365105-finding-x-value-with-known-y-value-on-plot#comment_501805

## Kelb77 (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/365105-finding-x-value-with-known-y-value-on-plot#comment_501816

## Walter Roberson (view profile)

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/365105-finding-x-value-with-known-y-value-on-plot#comment_501897

Sign in to comment.