Clear Filters
Clear Filters

Finding x intercept for 2D data

16 views (last 30 days)
University
University on 24 Nov 2023
Answered: Pavan Sahith on 12 Dec 2023
Hi,
Please how can i find x intercept at y=0 for 2D array. For instance, I have V which is loop over xi=0:1:10 and l=0:0.1:1.
I want to find V(xi, l)=x-intercept and plot the intercepts.
  1 Comment
Catalytic
Catalytic on 26 Nov 2023
If your V is a function of xi and l, then what is meant by the "x-intercept at y=0"? Is l supposed to be the same as y? xi the same as x?

Sign in to comment.

Answers (1)

Pavan Sahith
Pavan Sahith on 12 Dec 2023
Hello,
I understand that you are working with a 2D array and would like to identify all the x-intercepts at y=0 and plot them.
You can do that in MATLAB using the "find(diff(sign('your_data')))" and "interp1" refer to this code with the consideration of sample 2D arrays.
  • Considered using sine and cosine functions to generate 2D arrays,as there will be multiple intersections with the x-axis
% Creating sample Data
t = linspace(0, 2*pi, 1000);
ph = -rand(10,1);
x = cos(2*pi*t*5 + zeros(size(ph)));
y = sin(2*pi*t*5 + ph);
  • reshaping the matrices x and y into column vectors
x = reshape(x.',[],1);
y = reshape(y.',[],1);
  • "y0 = find(diff(sign(y)))";: Finds the indices where the sign of y changes, which corresponds to approximate zero-crossings.
  • The code then uses a for loop to iterate over the identified zero-crossings (y0) and performs linear interpolation (interp1) around each zero-crossing to estimate the x-values where y is zero.
  • xv(k,:) = interp1(y(idxrng), x(idxrng), 0);: Interpolates the x-values at y=0 for each identified zero-crossing. The result is stored in the matrix xv.
y0 = find(diff(sign(y)));
for i = 1:size(y0)
idxrng = max(1, y0(i)-1) : min(numel(x),y0(i)+1);
xv(i,:) = interp1(y(idxrng), x(idxrng), 0);
end
% Plotting
plot(x, y, 'DisplayName', 'Original Data')
hold on
plot(xv, zeros(size(xv)), '+r', 'DisplayName', 'X-Intersections')
hold off
grid
legend('Location','eastoutside')
Please refer to the following MathWorks documentation to know more about
Hope that helps.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!