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

Crank Nicolson (Matlab) PDF

This document describes a numerical method called Crank-Nicolson for solving the wave equation. It provides code to implement the Crank-Nicolson algorithm using the Thomas algorithm to solve the tridiagonal systems at each time step. The code takes parameters like spatial and temporal step sizes as input and outputs the numerical solutions on the grid points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Crank Nicolson (Matlab) PDF

This document describes a numerical method called Crank-Nicolson for solving the wave equation. It provides code to implement the Crank-Nicolson algorithm using the Thomas algorithm to solve the tridiagonal systems at each time step. The code takes parameters like spatial and temporal step sizes as input and outputs the numerical solutions on the grid points.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CRANK-NICOLSON

(Wavethomasalg.m)

function x=wavethomasalg(C,D,E,B)
%Filname:thomasalg.m
%input
% c subdiagonal of A
% d maindiagonal of A
% e superdiagonal of A
%output
%x solution of Ax=b
n=length(D);
alpha=zeros(n,1);
beta=zeros(n-1,1);
alpha(1)=D(1);
beta(1)=E(1)/alpha(1);
for i=2:n-1
alpha(i)=D(i)-C(i-1)*beta(i-1);
beta(i)=E(i)/alpha(i);
end;
alpha(n)=D(n)-C(n-1)*beta(n-1);

%Forward substitution to solve Lw=b


w=zeros(n,1);
w(1)=B(1)/alpha(1);
for i=2:n

w(i)=(B(i)-C(i-1)*w(i-1))/alpha(i);
end;
%Backward substitution to solve Ux=w
x=zeros(n,1);
x(n)=w(n);
for i=n-1:-1:1
x(i)=w(i)-beta(i)*x(i+1);

end;
(Hyperwavecrankthomas_in)
%INPUT
%Filename: hypwavecrankthomas_in.m
clc
c=1; %coefficient in PDE;
L=3.142; %length of string in x
Tmax=1; %maxmimum time t
h=0.3142; %step size for space x
k=0.05; %step size for space t
delta=1; %value of delta
r=k/h;

f1=inline('cos(x)','x'); %IC1: u(x,0)=f1(x) at t=0


f2=inline('0','x'); %IC2: w(x,0))=f2(x) at t=0
g1=inline('0','x'); %ut(x,0)
g2=inline('0','x'); %wt(x,0)
p1=inline('1','t'); %BC1: u(0,t)=p1(t) at t=0
p2=inline('0','t'); %BC1: w(0,t)=p2(t) at t=0
q1=inline('-1','t'); %BC2: u(L,t)=q1(t) at t=0
q2=inline('0','t'); %BC22: w(L,t))=q2(t) at t=0
[u,w]=hypwavecrankthomas(c,L,Tmax,h,k,delta,f1,f2,p1,p2,q1,q2);
(Hyperwavethomas.m)
function [u,w]=hypwavecrankthomas2(c,L,Tmax,h,k,delta,f1,f2,p1,p2,q1,q2)
%function w=pdehypwave(c,L,Tmax,h,k,d1,d2,f2,g2)
%Filename:pdehypwavecrankthomas.m
%Usage: u=pdehypwave(c,L,Tmax,h,k,delta,f1,f2,p1,p2,q1,q2)
%To solve wave equation c^2u_xx=u_tt, 0<x<L, t>0
%with the boundary conditions
% u(0,t=c1, u(L,t)=c2, t>0
% w(0,t=d1, w(L,t)=d2, t>0
%and the initial condtions
% u(x,0)=f1(x), 0<=x<=L
% u_t(u(x,0))=g1(x), 0<=x<=L
% w(x,0)=f2(x), 0<=x<=L
% w_t(u(x,0))=g2(x), 0<=x<=L

%(c)C.R.C.Teh

%OUTPUT
%u matrix of solution value

n=L/h ; %number of subinterval of [0,a]


m=Tmax/k; %number of subinterval of [0,b]
N=n+1; %number of grids [0=x1,x2,....,xN=a]
M=m+1; %number of grids [0=y1,y2,....,yN=a]
x=0:h:L; %x grids
t=0:k:Tmax; %t grids
u=zeros(N,M); %initialization matrix solution u
w=zeros(N,M); %initialization matrix solution w
for i=2:N-1
u(i,1)=f1(x(i)); %IC1: initial values u
w(i,1)=f2(x(i)); %IC1: initial values w
end
for j=1:M
u(1,j)=p1(t(j)); %BC1: boundary values u
u(N,j)=q1(t(j)); %BC2: boundary values u
w(1,j)=p2(t(j)); %BC1: boundary values w
w(N,j)=q2(t(j)); %BC2: boundary values w
end
for i=1:N
u(i,2)=u(i,1);
w(i,2)=w(i,1);
end
D1=zeros(1,N-2);
D2=zeros(1,N-2);
C1=zeros(1,N-3);
C2=zeros(1,N-3);
E1=zeros(1,N-3);
E2=zeros(1,N-3);
B1=zeros(1,N-1);
B2=zeros(1,N-1);
BB1=zeros(1,N-2);
BB2=zeros(1,N-2);

%Crank-Nicolson method
r=k/h;
for j=3:M
for i=1:N-2
D1(i)=2*(1+r^2);
D2(i)=2+2*c^2*r^2;

end;
for i=1:N-3
C1(i)=-r^2;
C2(i)=-r^2;
E1(i)=-r^2;
E2(i)=-r^2;
end;
for i=2:N-1
if i==1

B1(i)=2*r^2*u(1,j-1)-(2+2*r^2)*u(i,j-1)+r^2*u(i+1,j-1)+4*u(i,j)-
2*k^2*delta^2*sin(u(i,j)-w(i,j));
B2(i)=2*c^2*r^2*w(1,j-1)-(2+2*c^2*r^2)*w(i,j-1)+c^2*r^2*w(i+1,j-
1)+4*w(i,j)+2*k^2*sin(u(i,j)-w(i,j));

else
if i==N-1

B1(i)=2*r^2*u(N,j-1)+r^2*u(i-1,j-1)-(2+2*r^2)*u(i,j-1)+4*u(i,j)-
2*k^2*delta^2*sin(u(i,j)-w(i,j));
B2(i)=2*c^2*r^2*w(N,j-1)+r^2*w(i-1,j-1)-(2+2*c^2*r^2)*w(i,j-
1)+4*w(i,j)+2*k^2*sin(u(i,j)-w(i,j));
else

B1(i)=r^2*u(i-1,j-1)-(2+2*r^2)*u(i,j-1)+r^2*u(i+1,j-1)+4*u(i,j)-
2*k^2*delta^2*sin(u(i,j)-w(i,j));
B2(i)=c^2*r^2*w(i-1,j-1)-(2+2*c^2*r^2)*w(i,j-1)+c^2*r^2*w(i+1,j-
1)+4*w(i,j)+2*k^2*sin(u(i,j)-w(i,j));
end
end
for ii=1:N-2
BB1(ii)=B1(ii+1);
BB2(ii)=B2(ii+1);
end

aa=wavethomasalg2(C1,D1,E1,BB1);
for ii=2:N-1
u(i,j)=aa(ii-1);
end

bb=wavethomasalg2(C2,D2,E2,BB2);
for ii=2:N-1
w(i,j)=bb(ii-1);
end
end
end
disp('--------------------------------------')
fprintf(' u=x\\t')
fprintf(' %4,2f ',t)
fprintf('\n')
disp('--------------------------------------')
for i=1:N
fprintf(' %4.4f',x(i))
for j=1:M
fprintf('%10.4f',u(i,j))
end
fprintf('\n')
end

disp('--------------------------------------')
fprintf(' w=x\\t')
fprintf(' %4,2f ',t)
fprintf('\n')
disp('--------------------------------------')
for i=1:N
fprintf(' %4.4f',x(i))
for j=1:M
fprintf('%10.4f',w(i,j))
end
fprintf('\n')
end

You might also like