Poisson Equation is defined to be $-\nabla{f} = \frac{\partial^2 u}{\partial x^2} + \frac{\partial^2 u}{\partial y^2}$ . Using central differencing the equation can be discretized to the following:
\begin{equation}
\frac{1}{h^2} \big( u(x_{i-1},y_{j})+u(x_{i},y_{j-1})-4u(x_{i},y_{j})+
u(x_{i+1},y_{j} +u(x_{i},y_{j+1}) \big) = f(x_{i}, y_{j})
\end{equation}
The grid is setup to be $5x5$, $n=4$. the formula for creating the grid should always be $(n+1)x(n+1)$. The total number of nodes will be $(n+1)^2$. We choose $(n+1)$ because it allows us to model the boundary.
Node list:
1 | 6 | 11 | 16 | 21 |
---|---|---|---|---|
2 | 7 | 12 | 17 | 22 |
3 | 8 | 13 | 18 | 23 |
4 | 9 | 14 | 19 | 24 |
5 | 10 | 15 | 20 | 25 |
In the figure below Node 1 is connected to Node 6
We see that there is a pattern for node connectivity, N1 is connected to N6 and N2. N7 is connected to 2 and 12.
There is an easy way of programming this kind of connectivity. Take a look at the node table again, we see that if we iterate along the row by $i+1$ starting at node 1 we get 2. If we do the same thing except we iterate across the column by $j+1$ we arrive at 6. The same is true for node 7.
function Poisson(n)
A = zeromatrix((n + 1)2; (n + 1)2)
F = zeros((n + 1)2,1)
G = reshape(1:((n + 1)2),n+1,n+1); . Note: 1:((n + 1)2) = 1,2,3,...,(n + 1)2 Reshape changes the
size of the vector 1,2,3,...,(n + 1)2 to a matrix n+1 by n+1
for i 0 to n do
for j 0 to n do
row = G(i+1,j+1);
if i=0 or j=0 or i=n or j =n then . We are on the boundary
A(row,row) = -4;
F(row,1) = 0;
else
A(row,G(i+1,j)) = 1;
A(row,G(i+1,j+2)) = 1;
A(row,G(i,j+1)) = 1;
A(row,G(i+2,j+1)) = 1;
A(row,G(i+1,j+1)) = -4;
F(row,1) = 1;
return A,F . Now solve for Ax=F
120^2 x 120^2 Poisson Solution using Sparse Matricies |
No comments:
Post a Comment