Finding x intercept for 2D data

조회 수: 22 (최근 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