Environmental Engineering Reference
In-Depth Information
A = spdiags([ones(9,1) -2*ones(9,1) ones(9,1)],[-2 0 2],9,9)
creates a matrix with three diagonals. The content of the diagonals is deter-
mined by the first input parameter, which is a matrix with three columns. The
position of the diagonals in the matrix is determined by the second input
parameter, which is an integer array. A zero entry in this vector denotes the
main diagonal. The other entries are related to that setting, The command
above creates a 9
2 entries in the main diagonal and 1
entries in two off-diagonals. To see the structure of the matrix, use
9 matrix with
spy(A)
and obtain the figure on the right as a result.
All nonzero entries are indicated by a blue star.
Matrix operations, i.e. addition, subtraction, multiplication etc. can be
performed as with usual matrices. For example
A\ones(9,1)
delivers the solution of the system A x ¼ bwith right-hand side vector
containing one in each entry.
21.3 A Finite Difference Example
In the following example we demonstrate the method for the Poisson equation
2 u ¼ q
r
(21.21)
for the unknown function u ( x , y ) and right hand side q ( x , y ).
In the specification part the dimension of the system is assessed. Here the system
has the length 5 ( nx*h ) and the width 20 ( ny*h ). Moreover we specify the boundary
condition at the upper and lower boundary, and a constant source term.
 
Search WWH ::




Custom Search