How to find the closest values (positive and negative) to the line when X is zero?
7 次查看(过去 30 天)
显示 更早的评论
Valentin Augusto Simbron Mckay
2023-1-26
评论: Valentin Augusto Simbron Mckay
2023-1-26
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)
0 个评论
采纳的回答
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!