How do I make two waves move simultaneously towards each other to see the change in amplitude?

1 view (last 30 days)
a = -20;
b = 20;
n = 1000;
T = 10;
zeta = @(x) 1;
alpha = @(x) 1.5*exp(-10*x.^2);
beta = @(x) 1.5*exp(-0.25*(x-10).^2);
delta = (b - a)/n;
m = floor(T/delta);
for j = 1:n+1
x(j) = a + (j - 1)*delta;
end
for k = 1:m+1
t(k) = (k - 1)*delta;
end
for j = 1:n
r(j) = (zeta(x(j)) - zeta(x(j+1)))/(zeta(x(j)) + zeta(x(j+1)));
end
for j = 1:n+1
v(1,2*j-1) = alpha(x(j));
v(1,2*j) = beta(x(j));
u_approx(1,j) = v(1,2*j-1) + v(1,2*j);
end
for k = 2:m+1
v(k,1) = alpha(x(1) - t(k));
v(k,2*n+2) = beta(x(n+1) + t(k));
end
for k = 1:m
for j = 2:n+1
v(k+1,2*j-1) = (1 + r(j-1))*v(k,2*j-3) - r(j-1)*v(k,2*j);
end
end
for k = 1:m
for j = 1:n
v(k+1,2*j) = r(j)*v(k,2*j-1) + (1 - r(j))*v(k,2*j+2);
end
end
for k = 1:m
for j = 1:n+1
u_approx(k+1,j) = v(k+1,2*j-1) + v(k+1,2*j);
end
end
for i = 1:8
figure
plot(u_approx(25*i,:))
end
I am trying to learn how to make a video where one curve is moving to the right, and the other curve is moving to the left. Thus, the two waves will eventually collide and separate with each other. I currently have them as separate plots where each wave moving left and right at certain times, but now, I basically want them to combine those plots together as a video.
u_approx is a m x n matrix, which makes it harder for me to learn how to work this out. I looked online but the animations mainly uses a function, when I am using a matrix. please help me.

Answers (1)

Roshni Garnayak
Roshni Garnayak on 7 Feb 2020
You can use the 'pause' and 'hold' functions to create a dynamic plot. Refer to the following code to get an idea:
x = 1:1000; % Coordinates on x-axis
y1 = 1: 1000; % Coordinates of y-axis of the first line
y2 = 1000:-1:1; % Coordinates of y-axis of the second line
Y1 = y1(1);
Y2 = y2(1);
X = x(1);
for i = 1:1000
X = [X, x(i+1)];
Y1 = [Y1, y1(i+1)];
Y2 = [Y2, y2(i+1)];
plot(Y1, X)
hold on
plot(Y2, X)
pause(0.1)
axis([0 1000 0 1000]);
end
For more information about the 'pause' and 'hold' commands, refer to the following links:

Community Treasure Hunt

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

Start Hunting!