![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/338026/image.jpeg)
Graph Amplitude vs Displacement at different times
3 次查看(过去 30 天)
显示 更早的评论
I have a code created to solve 1D convection equation using finite difference approximation. I am able to get a graph where amplitude vs displacement is graphed at one time. How to I change the code so that I can plot amplitude vs displacement at multiple times, say (3 seconds, 6 seconds, 10 seconds, 30 seconds).
% solve 1D convection equation
clear
%Defined Parameters
Lmax = 10; % Maximum Length [meters]
Tmax= 3; % Maximum Time [seconds] (I chose the value)
c = 1; % constant wave speed [m/s]
%Parameters needed to solve the equation
imax = 1001; % number of space steps (x direction)
i = 0; % iteration counter
uint = 1; % initial displacement [m]
n = 0; % time iteration counter
%Courant Number
C = .8;
%change in x
dx = Lmax/(imax-1);
for i = 1:(Lmax/dx)+1
x(i) = dx*i;
end
dt = C*dx/c; %change in time calculation
%Initialize Conditions
t = 0; %time to zero
n = 0; %time counter
un = zeros(imax,1);
% Initial value of function u
imid = ((5.0/Lmax)*(imax-1))+1;
for i = 1:imid
un(i) = uint;
end
% Initial wave displacement
u0 = un;
% Boundary Conditions
unp1(1) = uint;
unp1(imax) = 0;
while (t < Tmax) % loop for when time less than maximum time
n = n + 1;
t = t + dt; % increment time
%unp1 = un;
% Value of amplitude at the boundary at any time
for i=2:imax-1
unp1(i) = un(i) - .5*C*(un(i+1)-un(i-1)); %CASE A
% unp1(i) = un(i) - C*(un(i)-un(i-1)); %CASE B
%unp1(i) = un(i) - .5*C*(3*un(i)-4*un(i-1)+un(i)); %CASE C
end
%solve unp1 at every grid
un = unp1;
end
% Graphical Representation Function
plot (x, u0, "-m", x, unp1, "-k"); %colors magenta and black
xlabel('Wave Displacement (m)');
ylabel('Amplitude (m)');
axis ([0 10 0 2]);
title('Amplitude vs. Wave Displacement at Different Times');
2 个评论
Maria
2020-7-28
I don't understand the question. Your code plots the following:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/338026/image.jpeg)
By different times, do yo mean plotting different waves? As in the magenta and black? Please explain a bit further. Thanks.
回答(1 个)
Maria
2020-7-29
You need to storage the different responses for each Tmax in different arrays and the use the command "hold on" and plot all of them on the same figure. You could try and make a for loop where you vary Tmax on each iteration, and store the 'unp' values of each iteration in a matrix, as: [unp(t1), unp(t2), unp(t3)]. Then you could just plot all the different signals on one figure.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!