forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FD_hyperbComp.m
45 lines (33 loc) · 1014 Bytes
/
FD_hyperbComp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%%% Finite Differences Theta Scheme for Parabolic Equation
T=10; %% final time
xmin=-3; xmax=15; %% spatial interval
L=xmax-xmin;
Nx=400; %% number of spatial discretization intervals
h=L/Nx;
x=xmin:h:xmax;
a=1; %% value of speed
nu=0.8;
dT=nu*h/a;
Nt=floor(T/dT);
u=zeros(Nx+1,1); %% initialization
%u(abs(x)<1)=1; %% Initial condition N1
u(abs(x-1)<=1)=1; %% initial condition N2
u(abs(x+1)<1)=.5; %% initial condition N2
figure(1); plot(x,u); axis([xmin xmax -.25 1.25]); drawnow;
pause(1)
u2=u;
%%%% Construction of matrices A and B
if a>0
A=(1-nu)*eye(Nx-1)+nu*[zeros(1,Nx-1) ; eye(Nx-2,Nx-1)];
end;
if a<0
A=(1+nu)*eye(Nx-1)-nu*[zeros(Nx-1,1) eye(Nx-1,Nx-2)];
end;
B=(1-(nu)^2)*eye(Nx-1)...
-1/2*nu*(1-nu)*[zeros(Nx-1,1) eye(Nx-1,Nx-2)]...
+1/2*nu*(1+nu)*[zeros(1,Nx-1) ; eye(Nx-2,Nx-1)];
for i=1:Nt
u(2:Nx)=(A*u(2:Nx));
u2(2:Nx)=B*u2(2:Nx);
figure(1); plot(x,u,'b',x,u2,'r'); axis([xmin xmax -.25 1.25]); drawnow;
end;