How to Interpolate and Find X given Y

조회 수: 12 (최근 30일)
raymond
raymond 2025년 7월 10일
편집: Matt J 2025년 7월 11일
Hello,
I have a problem where I need to get the x-values corresponding to where a horizontal line intersects with the data below.
I've tried using interp1 but it seems like it only returns to upper bound value, when I would like for it to return the location of both interception points.
Attached is the graphical output to the code below.
x = [50 43 35 22 15 12 11 10 8.5 6.5 5.75 5 4 3 2.75 2.5 2.25 2 1.67 1.37 1.2 1 0.7 0.55 0.3 0.25];
y = [0.3 0.35 0.43 0.52 0.6 0.67 0.79 0.91 1.02 1.1 1.2 1.35 1.23 1.23 1.20 1.17 1.15 1.14 1.07 1.02 0.95 0.83 0.73 0.31 0.21 0.18];
y_target = 0.8;
figure(1)
semilogx(x,y*0.75)
grid on
title('x y reverse interpolation')
xlabel('x')
ylabel('y')
hold on
y1 = yline(y_target,'-');
x1 = xline(1.67,'-'); %would like function or method of finiding x1 at a given y_target
x2 = xline(7.2,'-');

채택된 답변

Matt J
Matt J 2025년 7월 10일
편집: Matt J 2025년 7월 10일
One way:
x = [50 43 35 22 15 12 11 10 8.5 6.5 5.75 5 4 3 2.75 2.5 2.25 2 1.67 1.37 1.2 1 0.7 0.55 0.3 0.25];
y = [0.3 0.35 0.43 0.52 0.6 0.67 0.79 0.91 1.02 1.1 1.2 1.35 1.23 1.23 1.20 1.17 1.15 1.14 1.07 1.02 0.95 0.83 0.73 0.31 0.21 0.18];
x=flip(x); y=flip(y)*0.75;
y_target = 0.8;
[ymax,i]=max(y);
xmax=x(i);
fun=@(xq) abs(interp1(x,y,xq) -y_target );
x1=fminbnd( fun , x(1), xmax)
x1 = 1.6500
x2=fminbnd( fun , xmax, x(end)),
x2 = 7.3333
semilogx(x,y); xline([x1,x2]); yline(y_target);

추가 답변 (1개)

Matt J
Matt J 2025년 7월 11일
편집: Matt J 2025년 7월 11일
Here's yet another approach that uses linexlines2D, downloadable from,
xy = [[50 43 35 22 15 12 11 10 8.5 6.5 5.75 5 4 3 2.75 2.5 2.25 2 1.67 1.37 1.2 1 0.7 0.55 0.3 0.25];
0.75*[0.3 0.35 0.43 0.52 0.6 0.67 0.79 0.91 1.02 1.1 1.2 1.35 1.23 1.23 1.20 1.17 1.15 1.14 1.07 1.02 0.95 0.83 0.73 0.31 0.21 0.18]];
y_target = 0.8;
I=linexlines2D( xy(:,1:end-1), xy(:,2:end), [0,1,-y_target]); %intersections
I(:,all(isnan(I),1))=[];
semilogx(xy(1,:) , xy(2,:)); yline(y_target); hold on
plot(I(1,:), I(2,:),'or'); hold off

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

제품


릴리스

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by