Finding maximum value and its location in an array
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!