Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Part(B) 0f the final exam: Wave Equation Program

clear
a=1;
b=.5;
c=sqrt(4);
h=.2;
k=.1;
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);
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.5878 0.9511 0.9511 0.5878 0


0 0.4755 0.7694 0.7694 0.4755 0
0 0.1816 0.2939 0.2939 0.1816 0
0 -0.1816 -0.2939 -0.2939 -0.1816 0
0 -0.4755 -0.7694 -0.7694 -0.4755 0
0 -0.5878 -0.9511 -0.9511 -0.5878 0
1

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.1
t=0.2
0.6
t=0.3
t=0.4
0.4
t=0.5
0.2

-0.2

-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

You might also like