How to find the closest values (positive and negative) to the line when X is zero?
4 views (last 30 days)
Show older comments
Valentin Augusto Simbron Mckay
on 26 Jan 2023
Commented: Valentin Augusto Simbron Mckay
on 26 Jan 2023
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1275510/image.png)
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 Comments
Accepted Answer
Matt J
on 26 Jan 2023
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])
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!