0% found this document useful (0 votes)
67 views5 pages

Q1-Wave Equation Program

The document presents programs that solve the wave equation and heat equation numerically using finite difference methods. For the wave equation program, it defines parameters, initializes matrices, uses a for loop to calculate values of U at each point in space and time, and plots the results. For the heat equation program, it similarly defines parameters, initializes matrices, uses a for loop to calculate values of U, and plots the results. Both programs discretize the equations and iterate the solutions to model propagation over space and time.

Uploaded by

Ahmed Hwaidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views5 pages

Q1-Wave Equation Program

The document presents programs that solve the wave equation and heat equation numerically using finite difference methods. For the wave equation program, it defines parameters, initializes matrices, uses a for loop to calculate values of U at each point in space and time, and plots the results. For the heat equation program, it similarly defines parameters, initializes matrices, uses a for loop to calculate values of U, and plots the results. Both programs discretize the equations and iterate the solutions to model propagation over space and time.

Uploaded by

Ahmed Hwaidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1-Wave Equation Program:

clear
a=1;
b=.5;
c=sqrt(4);
h=.1;
k=.05;
n=a/h+1;
m=b/k+1;
R=c*k/h;
R2=R^2;
R22=R2/2;
S1=1-R2;
S2=2-2*R2;
syms x
y=sin(pi*x)+sin(2*pi);
G=zeros(n);
for j=1:m
U(1,j)=0;
U(n,j)=0;
end
for i=2:n-1
U(i,1)=double(subs(y,x,h*(i-1)));
U(i,2)=S1*double(subs(y,x,h*(i-1)))+k*G((i-1))
+R22*double((subs(y,x,h*(i)))+double(subs(y,x,h*(i-2))));
end
for j=3:m;
for i=2:n-1
U(i,j)=S2*U(i,j-1)+R2*(U(i-1,j-1)+U(i+1,j-1)-U(i,j-2));
end
end
fprintf('U(x,t)')
disp(U')
x=0:h:a;
t=0:k:b;
[xn tn]=meshgrid(x,t);
mesh(xn,tn,U')
xlabel('x')
ylabel('t')
figure
plot(x,U)
legend( [repmat('t=',length(t),1) num2str(t')])
xlabel('x')
grid on
U(x,t)

0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511 0.8090 0.5878 0.3090 0


0 0.2939 0.5590 0.7694 0.9045 0.9511 0.9045 0.7694 0.5590 0.2939 0
0 0.2500 0.4755 0.6545 0.7694 0.8090 0.7694 0.6545 0.4755 0.2500 0
0 0.1816 0.3455 0.4755 0.5590 0.5878 0.5590 0.4755 0.3455 0.1816 0
0 0.0955 0.1816 0.2500 0.2939 0.3090 0.2939 0.2500 0.1816 0.0955 0
0 0.0000 -0.0000 0 0 -0.0000 0 0 -0.0000 0 0
0 -0.0955 -0.1816 -0.2500 -0.2939 -0.3090 -0.2939 -0.2500 -0.1816 -0.0955 0
0 -0.1816 -0.3455 -0.4755 -0.5590 -0.5878 -0.5590 -0.4755 -0.3455 -0.1816 0
0 -0.2500 -0.4755 -0.6545 -0.7694 -0.8090 -0.7694 -0.6545 -0.4755 -0.2500 0
0 -0.2939 -0.5590 -0.7694 -0.9045 -0.9511 -0.9045 -0.7694 -0.5590 -0.2939 0
0 -0.3090 -0.5878 -0.8090 -0.9511 -1.0000 -0.9511 -0.8090 -0.5878 -0.3090 0

0.5

-0.5

-1
0.8
0.6 1
0.8
0.4 0.6
0.2 0.4
0.2
t 0 0
x
1
t= 0
0.8 t=0.05
t= 0.1
0.6
t=0.15
t= 0.2
0.4
t=0.25
0.2 t= 0.3
t=0.35
0 t= 0.4
t=0.45
-0.2 t= 0.5

-0.4

-0.6

-0.8

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Q2-Heat Equation Program:
clear
a=1;b=.2;c=1;
h=.2;k=.02;
c1=0;c2=0;
syms x
y=4*x-4*x^2;
n=a/h+1;
m=b/k+1;
R=c^2*k/h^2;
S=1-2*R;
for j=1:m
U(1,j)=c1;
U(n,j)=c2;
end
for i=2:n-1
U(i,1)=double(subs(y,x,h*(i-1)));
end
for j=2:m;
for i=2:n-1
U(i,j)=S*U(i,j-1)+R*(U(i-1,j-1)+U(i+1,j-1));
end
end
fprintf('U(x,t)')
U
x=0:h:a;
t=0:k:b;
[xn tn]=meshgrid(x,t)
mesh(xn,tn,U')
xlabel('x')
ylabel('t')
figure
plot(x,U)
legend( [repmat('t=',length(t),1) num2str(t')])
xlabel('x')
grid on

U(x,t)

0 0 0 0 0 0 0 0 0 0 0
0.6400 0.4800 0.4000 0.3200 0.2600 0.2100 0.1700 0.1375 0.1113 0.0900 0.0728
0.9600 0.8000 0.6400 0.5200 0.4200 0.3400 0.2750 0.2225 0.1800 0.1456 0.1178
0.9600 0.8000 0.6400 0.5200 0.4200 0.3400 0.2750 0.2225 0.1800 0.1456 0.1178
0.6400 0.4800 0.4000 0.3200 0.2600 0.2100 0.1700 0.1375 0.1113 0.0900 0.0728
0 0 0 0 0 0 0 0 0 0 0
1

0.8

0.6

0.4

0.2

0
0.2
0.15 1
0.8
0.1 0.6
0.05 0.4
0.2
t 0 0
x

1
t= 0
0.9 t=0.02
t=0.04
0.8
t=0.06
t=0.08
0.7
t= 0.1
0.6 t=0.12
t=0.14
0.5 t=0.16
t=0.18
0.4 t= 0.2

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

You might also like