Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views15 pages

PDEs Assignment-1

The document details a MATLAB programming assignment focused on numerical solutions for partial differential equations (PDEs) using various methods including FTCS, BTCS, Crank-Nicolson, and Richardson. Each method is analyzed for stability, accuracy, and complexity, with error metrics provided for comparison. The results include graphical representations of the numerical and exact solutions, along with maximum and average errors for each method.

Uploaded by

martian427
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)
24 views15 pages

PDEs Assignment-1

The document details a MATLAB programming assignment focused on numerical solutions for partial differential equations (PDEs) using various methods including FTCS, BTCS, Crank-Nicolson, and Richardson. Each method is analyzed for stability, accuracy, and complexity, with error metrics provided for comparison. The results include graphical representations of the numerical and exact solutions, along with maximum and average errors for each method.

Uploaded by

martian427
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/ 15

Department of Engineering Sciences

Numerical Solution for PDEs

Assignment

MATLAB Programming for Numerical Solution for PDEs

Name Reg No
Maaz Khan CV-2463

Submitted To:
Dr. Muhammad Siraj

Programming for FTCS Scheme


n +1
Um = λU nm −1 + (1 - 2 λ)U nm + λU nm +1

a=0;
b=2;
L=b-a;
ti=0;
tf=0.1;
m=4;
T=tf-ti;
n=2;
h=(b-a)/m;
k=T/n;
alpha=1;
lambda=alpha*k/h^2;
% Check stability condition
if lambda > 0.5
warning('The explicit scheme is unstable (lambda = %.2f > 0.5). Reduce time step k for
stability.', lambda);
end
Nx=L/h+1;
Nt=T/k+1;
x=linspace(a,b,Nx);
t=linspace(ti,tf,Nt);
U=zeros(Nt,Nx);
% Bounday Conditions
U(:,1)=0;
U(:,end)=0;
% Initial Condition
U(1,:)= sin((pi/2)*x);
%FTCS
for n=1:Nt-1
for m=2:Nx-1
U(n+1,m)=lambda*(U(n,m+1)+U(n,m-1))+(1-2*lambda)*(U(n,m));
end
end
U_exact=zeros(Nt,Nx);
for n=1:Nt
U_exact(n,:)=exp(-((pi^2)/4)*t(n))*sin((pi/2)*x);
end
error=abs(U_exact-U);
max_error=max(error(:));
mean_error=mean(error(:));

% Line graph
figure;
plot(x, U(end,:), 'b-', 'DisplayName', 'FTCS Numerical');
hold on;
plot(x, U_exact(end, :), 'o-', 'DisplayName', 'Exact Solution');
xlabel('x');
ylabel('U(x, t)');
title(sprintf('Heat Equation Solution at t=%.2f', T));
legend({'FTCS Numerical', 'Exact Solution'}, 'Location', 'northeast');
grid on;
hold off;
% Print errors
fprintf('Maximum Error: %.6f\n', max_error);
fprintf('Average Error: %.6f\n', mean_error);

Exact Solution

Approximate Solution

Error
Maximum Error: 0.001932
Average Error: 0.000487
Plot
MATLAB Programming for BTCS Method
n−1
Um = -λU nm +1 + (1 + 2 λ)U nm + λU nm −1

a=0;
b=2;
L=b-a;
ti=0;
tf=0.1;
T=tf-ti;
alpha=1;
m=4;
N=2;
k=T/N;
h=L/m;
lambda=alpha*k/h^2;
Nt=round(T/k)+1;
Nx=round(L/h)+1;
x=linspace(a,b,Nx);
t=linspace(ti,tf,Nt);
U=zeros(Nt,Nx);
U(:,1)=0;
U(:,end)=0;
U(1,:)=sin((pi/2)*x);
%BTCS
A=diag((1+2*lambda)*ones(Nx-2,1))+...
diag(-lambda*ones(Nx-3,1),1)+...
diag(-lambda*ones(Nx-3,1),-1);
for n=1:Nt-1
B=U(n,2:Nx-1)';
W=A\B;
U(n+1,2:Nx-1)=W;
end
U_exact=zeros(Nt,Nx);
for n=1:Nt
U_exact(n,:)=exp(-((pi^2)/4)*t(n))*sin((pi/2)*x);
end

error=abs(U_exact-U);
max_error=max(error(:));
mean_error=mean(error(:));

% Line graph
figure;
plot(x, U(end,:), 'g-', 'DisplayName', 'BTCS Numerical');
hold on;
plot(x, U_exact(end, :), 'y-', 'DisplayName', 'Exact Solution');
xlabel('x');
ylabel('U(x, t)');
title(sprintf('Heat Equation Solution at t=%.2f', T));
legend({'BTCS Numerical', 'Exact Solution'}, 'Location', 'northeast');
grid on;
hold off;

% Print errors
fprintf('Maximum Error: %.6f\n', max_error);
fprintf('Average Error: %.6f\n', mean_error);
Exact Solution

Approximate Solution

Plot
Error
Maximum Error: 0.019912
Average Error: 0.005006
Crank Nicolson Method
n
−λ n+1 λ n+1 λ λ n
U m−1 + (1 + λ)U nm+1 - U m+1 = U + (1 - λ)U nm + U m+1
2 2 2 m−1 2

a=0;
b=1;
L=b-a;
ti=0;
tf=0.4;
T=tf-ti;
h=0.25;
k=0.1;
alpha=1/16;
lambda=alpha*k/h^2;
Nx=round(L/h)+1;
Nt=round(T/k)+1;
x=linspace(a,b,Nx);
t=linspace(ti,tf,Nt);
U=zeros(Nt,Nx);
U(:,1)=0;
U(:,end)=0;
U(1,:)=sin(pi*x);
% Constant Coefficient Matrix A
A=diag(2*(1+lambda)*ones(Nx-2,1))+...
diag(-lambda*ones(Nx-3,1),1)+...
diag(-lambda*ones(Nx-3,1),-1);
% Constant Coefficient Matrix B
B=diag(2*(1-lambda)*ones(Nx-2,1))+...
diag(lambda*ones(Nx-3,1),1)+...
diag(lambda*ones(Nx-3,1),-1);
% Time-stepping Loop using Crank-Nicolson
for n=1:Nt-1
% Right Hand Side of Scheme B*U(n,2:Nx-1)
Y=B*U(n,2:Nx-1)';
% Solve System: A * U(n+1, 2:Nx-1)' = Y
U(n+1,2:Nx-1)=A\Y;
end
U_exact=zeros(Nt,Nx);
for n=1:Nt
U_exact(n,:) = sin(pi * x) * exp(- (pi^2 / 16) * t(n));
end
error=abs(U_exact-U);
max_error=max(error(:));
mean_error=mean(error(:));
% Line graph
figure;
plot(x, U(end,:), 'black-', 'DisplayName', 'Crank Nicolson Numerical Method');
hold on;
plot(x, U_exact(end, :), 'o-', 'DisplayName', 'Exact Solution');
xlabel('x');
ylabel('U(x, t)');
title(sprintf('Heat Equation Solution at t=%.2f', T));
legend({'Crank Nicolson Method', 'Exact Solution'}, 'Location', 'northeast');
grid on;
hold off;

% Print errors
fprintf('Maximum Error: %.6f\n', max_error);
fprintf('Average Error: %.6f\n', mean_error);
Exact Solution

Approximate Solution
Error
Maximum Error: 0.009557
Average Error: 0.002406

Plot

Richardson Method
n
n +1 λ
Um = U + 2λ[U nm −1- 2U nm + U nm +1]
2 m−1

a=0;
b=2;
L=b-a;
ti=0;
tf=0.1;
T=tf-ti;
m=4;
N=2;
h=L/m;
k=T/N;
alpha=1;
lambda=alpha*k/h^2;
Nx=round(L/h)+1;
Nt=round(T/k)+1;
x=linspace(a,b,Nx);
t=linspace(ti,tf,Nt);
U=zeros(Nt,Nx);
U(:,1)=0;
U(:,end)=0;
U(1,:)=sin(pi*x/2);
% Compute U_j^{-1} using backward extrapolation
U_minus1=zeros(1,Nx);
for m=2:Nx-1
U_minus1(m)=U(1,m)-lambda*(U(1,m+1)-2*U(1,m)+U(1,m-1));
end
% Apply Richardson Scheme
for n=1:Nt-1
for m=2:Nx-1
U(n+1,m)=U_minus1(m)+2*lambda*(U(n,m+1)-2*(U(n,m))+U(n,m-1));
end
U_minus1=U(n,:);
end
U_exact=zeros(Nt,Nx);
for n=1:Nt
U_exact(n,:)=exp(-((pi^2)/4)*t(n))*sin((pi/2)*x);
end

error=abs(U_exact-U);
max_error=max(error(:));
mean_error=mean(error(:));

% Line graph
figure;
plot(x, U(end,:), 'g-', 'DisplayName', 'Richardson Numerical Method');
hold on;
plot(x, U_exact(end, :), 'o-', 'DisplayName', 'Exact Solution');
xlabel('x');
ylabel('U(x, t)');
title(sprintf('Heat Equation Solution at t=%.2f', T));
legend({'Richardson Numerical Method', 'Exact Solution'}, 'Location', 'northeast');
grid on;
hold off;

% Print errors
fprintf('Maximum Error: %.6f\n', max_error);
fprintf('Average Error: %.6f\n', mean_error);
Exact Solution

Approximate Solution

Error:
Maximum Error: 0.011793
Average Error: 0.002074
Plot:
Comparison of Numerical Methods

Error (Max /
Method Stability Accuracy Complexity Remarks
Avg)
Easiest to implement but can
Conditionally stable (λ ≤ Low to Simple, explicit 0.001932 / become unstable and
FTCS
0.5) Moderate scheme 0.000487 inaccurate for large time
steps.
Implicit scheme; Stable for any timetable, but
Moderate to 0.019912 /
BTCS Unconditionally stable requires solving computationally heavier than
High 0.005006
linear systems FTCS.
Best balance between
High (second Semi-implicit;
Crank- 0.009557 / stability and accuracy; very
Unconditionally stable order in time moderate
Nicolson 0.002406 suitable for time-dependent
and space) complexity
PDEs.
Conditionally stable More accurate than FTCS,
Explicit, but more 0.011793 /
Richardson (depends on Moderate but sensitive to initialization
error-prone 0.002074
implementation) and parameter choice.

You might also like