필터 지우기
필터 지우기

Graph Amplitude vs Displacement at different times

조회 수: 3 (최근 30일)
Lindsay Mau
Lindsay Mau 2020년 7월 26일
답변: Maria 2020년 7월 29일
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
Maria 2020년 7월 28일
I don't understand the question. Your code plots the following:
By different times, do yo mean plotting different waves? As in the magenta and black? Please explain a bit further. Thanks.
Lindsay Mau
Lindsay Mau 2020년 7월 28일
Yes plotting different waves on the same graph. The different waves would be from responses at different times.

댓글을 달려면 로그인하십시오.

답변 (1개)

Maria
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.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by