Finding x intercept for 2D data

41 次查看(过去 30 天)
University
University 2023-11-24
回答: Pavan Sahith 2023-12-12
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 个评论
Catalytic
Catalytic 2023-11-26
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?

请先登录,再进行评论。

回答(1 个)

Pavan Sahith
Pavan Sahith 2023-12-12
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by