0% found this document useful (0 votes)
26 views1 page

Code

The PoissonSolver function solves a Poisson equation on a 1D mesh using finite differences. It first defines the mesh size and grid, then calls subfunctions to compute the load vector b and stiffness matrix A based on the grid and a given source function f. It solves the linear system Au=b and plots the solution u. The subfunctions LoadVector and StiffnessMatrix discretize the Poisson equation using central finite differences to build b and A, while f defines the source term as a sine function.

Uploaded by

She Dil
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)
26 views1 page

Code

The PoissonSolver function solves a Poisson equation on a 1D mesh using finite differences. It first defines the mesh size and grid, then calls subfunctions to compute the load vector b and stiffness matrix A based on the grid and a given source function f. It solves the linear system Au=b and plots the solution u. The subfunctions LoadVector and StiffnessMatrix discretize the Poisson equation using central finite differences to build b and A, while f defines the source term as a sine function.

Uploaded by

She Dil
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/ 1

% PoissonSolver.

m
function PoissonSolver()
h = 0.001; %mesh size
x = 0:h:1; %mesh
A = StiffnessMatrix(x);
b = LoadVector(x,@f);
u = A\b; %solve linear system
plot(x,u)

% LoadVector.m
function b = LoadVector(x,f)
n=length(x)-1;
b=zeros(n+1,1);
for i=1:n
h = x(i+1)-x(i);
b(i) = b(i) + f(x(i))*h/2;
b(i+1) = b(i+1) + f(x(i+1))*h/2;
end
b(1,1)=0;
b(n+1,1)=0;

% StiffnessMatrix.m
function A = StiffnessMatrix(x)
n = length(x)-1;
A = zeros(n+1,n+1);
for i=1:n
h=x(i+1)-x(i);
A(i,i)=A(i,i)+1/h;
A(i,i+1)=A(i,i+1)-1/h;
A(i+1,i)=A(i+1,i)-1/h;
A(i+1,i+1)=A(i+1,i+1)+1/h;
end
A(1,1)=1;
A(n+1,n+1)=1;

% f.m
function y = f(x)
%y=1;
y=(pi^2)*sin(pi*x);

You might also like