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 Center および File ExchangeLoops 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