Finding maximum value and its location in an array

9 Ansichten (letzte 30 Tage)
Ana Mucalica
Ana Mucalica am 14 Mär. 2022
Beantwortet: KSSV am 15 Mär. 2022
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

Antworten (1)

KSSV
KSSV am 15 Mär. 2022
REad about the function max. If v is your array.
[val,idx] = max(v)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by