How to find the closest values (positive and negative) to the line when X is zero?

조회 수: 11 (최근 30일)
Hi,
I am trying to find the closest two points (positive and negative) when x is zero, I tried with interp1 and find but it didn't work.
Something similar like the image.
This is my code
Thanks for the help
%Data
load Data
%Create line plot
x = V;
y = J;
%plot(x,y,'b-','LineWidth',5);
plot(x,y,'-o','MarkerIndices',1:19:length(y))
hold on
%Label the axes and add a title
xlabel('Voltage V')
ylabel('density current mA/cm2')
title('J-V curve ')
% Set axis start from origin
axis([-0.06 0.7 -0.001 0.008])
%yDesired = interp1(x,y,0);
%Minimum x value for positive y
%Y line
x1 = [0 0];
y1 = [0 0.1];
line(x1,y1,'Color','red','LineStyle','--')
%X line
x2 = [0 0.7];
y2 = [0 0];
line(x2,y2,'Color','red','LineStyle','--')
%find the value when x=0
y=interp1(x,y,0)

채택된 답변

Matt J
Matt J 2023년 1월 26일
load Data
%Create line plot
[x,is] = sort(V);
y = J(is);
in=find(x<0,1,'last');
ip=find(x>0,1,'first');
[xn,yn, xp,yp]=deal(x(in),y(in), x(ip), y(ip));
plot(x,y,'.',xn,yn,'o',xp,yp,'o');
axis([-0.0056 0.0059 0.0060 0.0063])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by