How to Interpolate and Find X given Y
조회 수: 12 (최근 30일)
이전 댓글 표시
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,'-');
댓글 수: 0
채택된 답변
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)
x2=fminbnd( fun , xmax, x(end)),
semilogx(x,y); xline([x1,x2]); yline(y_target);
댓글 수: 0
추가 답변 (1개)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

