Joshi Report 1
Joshi Report 1
Joshi Report 1
Report : Assignment 1
Note: MATLAB codes of all the problems are given at the end of the document.
Problem 1
Solving Poisson equation with Dirichlet BC’s and different source functions.
Discretization of equation:
∂ 2u
− =f x ∈ (0 1)
∂x2
with
u(0) = 0, u(1) = 0
where i represents inner grid point of the domain x∈[0 1] and h is the step size of the grid. When
the above equation is written in matrix form for all the inner grid points, it gives
2 −1 0 0 0 ... 0 u1 f1 + uh20
−1 2 −1 0 0 ... 0 u2 f2
1 0 −1 2 −1 0 . . . 0 u3
f3
.. .. =
2
.. .. .. .. .. .. ..
h .
. . . . . . .
.
0 0 0 . . . −1 2 −1 un−2 fn−2
0 0 0 . . . 0 −1 2 un−1 fn−1 + uhn2
By solving above matrix(Au=f) we can get u vector for all the inner grid points x=ih and u val-
ues for the boundaries(u0 & un ) are already given as BC’s.
Solution: The matrix was solved with n=10 for the following source functions. Below graphs
shows comparison of numerical solution to the analytical(exact) solution.
f1 (x) = −1
f2 (x) = −x + x2 − x3 + x4 − x5 + x6
f3 (x) = −sin(πx)
f(x)=-1
0
-0.02
-0.04
-0.06
u(x)
-0.08
-0.1
-0.12 numerical
exact
-0.14
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
-0.005
-0.01
-0.015
u(x)
-0.02
-0.025
-0.03
-0.035 numerical
exact
-0.04
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
f(x)=sin( x)
0
-0.02
-0.04
u(x)
-0.06
-0.08
-0.1 numerical
exact
-0.12
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
From the graphs of u(x) it is clear that for the source function f1 (x), both analytical(exact) and
numerical solution are same but in the case of f2 (x) & f3 (x) there is some difference between the
two solutions.This is discussed in the next page.
Error in the values of numerical solution can be estimated using the below equation. Here e ma-
trix contains estimated error for each grid point.
Ae = h2 p
where p is the approximation error at each grid point and for the discretization procedure adopted
for this problem,
h2 0000
p ≈ u (x)
12
or
h2 00
p ≈ − f (x)
12
00 00 00
For the source functions given in this problem, it is clear that f1 (x)=0. Whereas, f2 (x) & f3 (x)
are 6= 0. This is the reason why analytical and numerical solution graph coincides only for u1 (x) .
The graphs below show the comparison of estimated error and exact error for u2 (x) & u3 (x).
2.5
2
error(x)
1.5
0.5
estimated error
exact error
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
10-4 f(x)=sin( x)
9
5
error(x)
1
estimated error
0
exact error
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x
Problem 2
Original equations,
0 0
−(ku ) = f (x), x ∈ (0 1)
u(0) = 0, u(1) = 1
f (x) = 1, x ∈ [0 1]
k(x) = 1 + sin(πx), x ∈ [0 1]
The discretized equation can also be written in matrix form for i=1,2...n-1 as,
a1 + a2 −a2 0 0 0 ... 0 u1 f1 + ah1 u2 0
−a2 a2 + a3 −a3 0 0 ... 0 u2 f2
1 0 −a 3 a 3 + a 4 −a 4 0 . . . 0 u3 f 3
=
. .. .. .. .. .. .. .. ..
h2 ..
. . . . . .
.
.
0 0 0 . . . −an−2 an−2 + an−1 −an−1 un−2 fn−2
an un
0 0 0 ... 0 −an−1 an−1 + an un−1 fn−1 + h2
Here fi is equal to 1 for all the grid points and u0 & un are given as the boundary conditions. Let
us represents above equation in the form of Au = h2 f , here the A matrix of n−1×n−1 dimension
can be formed using A = DT Ac D, where
1 0 0 0 ... 0
−1 1 0 0 . . . 0
1
Dn×(n−1) = ... ... ... ... .. ..
h . .
0 0 0 . . . −1 1
0 0 0 . . . 0 −1
a1 0 0 0 ... 0
0 a2 0 0 ... 0
An×n = ... ... .. .. .. ..
c
. . . .
0 0 0 ... an−1 1
0 0 0 ... 0 an
In the code, the above matrices are defined as sparse matrices, which means that only non-zero
values are stored in the matrix and rest of the spaces are empty. This saves a lot of memory ,
especially when the matrices are large.
Results:After solving the matrix equation for n=20, we get the solution of u(x) as shown below.
0.9
0.8
0.7
0.6
u(x)
0.5
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x
Problem 3
Original equation,
∂ 2u ∂ 2u
− + = f (x, y), (x, y) ∈ Ω
∂x2 ∂y 2
f (x, y) = 20y 2 sin(πx) (x, y) ∈ Ω
BC’s are,
where i=1,2...m and j=1,2...n. The above equation can also be written in matrix form as Au =
f . Figure in the next page shows how the grid is divided, the blue region shows the domain for
calculating fi,j .
Grid division
where Iy & Ix is identity matrix Im−1 & In−1 and Axx & Ayy is given by,
All the matrices above are stored as sparse matrices to save on memory. Matrix u & f are ar-
ranged in lexicographic order. This is given as,
u1,0
f1,1 + ∆y
u1,1 2
u2,1 f2,1 + u2,02
∆y
..
..
. .
um−1,0
um−1,1 fm−1,1 + ∆y2
u1,2 f1,2
u2,2 f 2,2
.
..
..
um−1×(n−1) = , f m−1×(n−1)
=
.
um−1,2
f m−1,2
.
..
.
.
.
u
f1,n−1
1,n−1
u
f2,n−1
2,n−1
.. .
.
. .
um−1,n−1 fm−1,n−1
In the f matrix, only for j=1 the values of fi,j will get affected. This is because only the bound-
ary next to j = 1 has some value, all other boundaries are equal to zero.
Results: After solving the above equations for m=20 & n=10 we get u(x,y) as shown below,
0.5
u(x,y)
-0.5
1
0.5
2
1.5
y 1
0 0.5
0 x
0.8
0.6
y
0.4
0.2
0
0 0.5 1 1.5 2
x
Problem 1
clc
clear all
close all
invA=inv(A);
for i=1:n-1
B1(i,1)=-dx^2*(-1);
f2(i,1)=x(i+1)^6-x(i+1)^5+x(i+1)^4-x(i+1)^3+x(i+1)^2-x(i+1);
f2e(i,1)=-dx^4/12*(30*x(i+1)^4-20*x(i+1)^3+12*x(i+1)^2-6*x(i+1)+2);
B2(i,1)=-dx^2.*f2(i,1);
f3(i,1)=-sin(pi*x(i+1));
f3e(i,1)=-dx^4/12*(pi^2*sin(pi*(x(i+1))));
B3(i,1)=-dx^2.*f3(i);
end
u1=invA*B1;
u2=invA*B2;
u3=invA*B3;
u2e=invA*f2e;
u3e=invA*f3e;
u1_final=[0; u1 ;0];
u2_final=[0; u2 ;0];
u3_final=[0; u3 ;0];
u2e_final=[0; u2e ;0];
u3e_final=[0; u3e ;0];
func1=-(x)/2+(x).^2/2;
func2=-1/840*(15*x.^8-20*x.^7+28*x.^6-42*x.^5+70*x.^4-140*x.^3+89*x);
func3=-sin(pi*x)/pi^2;
func1=func1’;
func2=func2’;
func3=func3’;
error1=func1-u1_final;
error2=func2-u2_final;
error3=func3-u3_final;
plot(x,u1_final,’o-’)
hold on
plot(x,func1,’o-’)
legend(’numerical’,’exact’,’location’,’southwest’)
xlabel(’x’)
ylabel(’u(x)’)
title(’f(x)=-1’)
set(gca,’fontname’,’times’)
figure
plot(x,u2_final,’o-’)
hold on
plot(x,func2,’o-’)
legend(’numerical’,’exact’,’location’,’southwest’)
xlabel(’x’)
ylabel(’u(x)’)
title(’f(x)=-x+x^2-x^3+x^4-x^5+x^6’)
set(gca,’fontname’,’times’)
figure
plot(x,u3_final,’o-’)
hold on
plot(x,func3,’o-’)
legend(’numerical’,’exact’,’location’,’southwest’)
xlabel(’x’)
ylabel(’u(x)’)
title(’f(x)=sin(\pix)’)
set(gca,’fontname’,’times’)
figure
plot(x,u2e_final,’o-’)
hold on
plot(x,error2,’o-’)
legend(’estimated error’,’exact error’,’location’,’south’)
xlabel(’x’)
ylabel(’error(x)’)
title(’f(x)=-x+x^2-x^3+x^4-x^5+x^6 ’)
set(gca,’fontname’,’times’)
figure
plot(x,u3e_final,’o-’)
hold on
plot(x,error3,’o-’)
legend(’estimated error ’,’exact error’,’location’,’south’)
xlabel(’x’)
ylabel(’error(x)’)
title(’f(x)=sin(\pix) ’)
set(gca,’fontname’,’times’)
Problem 2
clear all
for i=1:n
for j=1:n-1
if i==j
D(i,j)=1;
elseif i==j+1
D(i,j)=-1;
end
end
end
K=sparse(diag(a));
A=D’*K*D; % A matrix
plot(x,Ufinal,’*-’)
ylabel(’u(x)’)
xlabel(’x’)
set(gca,’fontname’,’times’,’fontsize’,12)
Problem 3
clear all
x=0:dx:2; % X vector
y=0:dy:1; % Y vector
Dx=sparse(eye(m,m-1));
Dy=sparse(eye(n,n-1));
%Creating f matrix
for i=1:m
for j=1:m-1
if i==j
Dx(i,j)=1;
elseif i==j+1
Dx(i,j)=-1;
end
end
end
for i=1:n
for j=1:n-1
if i==j
Dy(i,j)=1;
elseif i==j+1
Dy(i,j)=-1;
end
end
end
Dx=Dx/dx; % Dx matrix
Dy=Dy/dy; % Dy matrix
Iy=sparse(eye(n-1));
Ix=sparse(eye(m-1));
A=kron(Iy,Axx)+kron(Ayy,Ix); % A matrix
f=zeros((m-1),(n-1));
for i=1:m-1 % f matrix including BCs
for j=1:n-1
f(i,j)=20*y(j+1)^2*sin(pi*x(i+1));
if j==1
f(i,j)=f(i,j)+ sin(pi/2*x(i+1))/dy^2;
end
end
end
surf(x,y,U_BC’)
xlabel(’x’)
ylabel(’y’)
zlabel(’u(x,y)’)
set(gca,’fontname’,’times’,’fontsize’,12)
figure
contour(x,y,U_BC’,15)
xlabel(’x’)
ylabel(’y’)
set(gca,’fontname’,’times’,’fontsize’,12)