Finding maximum value and its location in an array

9 ビュー (過去 30 日間)
Ana Mucalica
Ana Mucalica 2022 年 3 月 14 日
回答済み: KSSV 2022 年 3 月 15 日
I am working with a code in matlab that simulates a wave travelling in time. The following picture is my output at time zero.
I need to plot the amplitude of the wave as a function of time, since the wave gets smaller as it interacts with the ramp. To do this it's been suggested to do a loop that goes through the array and compares two values u_j+1 and u_j to check if u_j+1<u_j. This is a sample of my code, where u is a column vector.
for j = 2 : length(t)
diff_first=A*u1/(2*h);
sum_w = B*u1;
diff_middle=2*diff_first.*sum_w;
diff_third=A_third*u1/(2*h^3);
if j == 2
u1 = u - dt*(diff_middle+diff_third); %Computing the first step
figure(1); plot(x,u1,'r.');
else
u2 = u - 2*dt*(diff_middle+diff_third); %Computing further steps
figure(1); plot(x,u2,'r.');
u = u1; u1 = u2;
end
hold on; axis([x(1) x(end) -1 Am]); hold off;
end

回答 (1 件)

KSSV
KSSV 2022 年 3 月 15 日
REad about the function max. If v is your array.
[val,idx] = max(v)

カテゴリ

Help Center および File ExchangeMatched Filter and Ambiguity Function についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by