This Julia notebook accompanies the book "Numerical Methods for Engineers: by D. V. Griffiths and I.M. Smith (NMfE).
The Julia package NMfE.jl and these notebooks are not a replacement for NMfE. The intention is to use the notebooks as a replacement for the Fortran programs while studying the book.
NMfE.jl is useful by itself. It can also be viewed as an introduction to the methods used in a follow on package I'm working on, CSoM.jl
, which makes the Finite Element toolkit explained in "Programming the Finite Element Method" (PtFEM), also by D. V. Griffiths and I.M. Smith, available in Julia.
Using the notebooks requires a couple of preliminary steps:
Inside the REPL of Julia (do steps 1 to 6 just once):
Pkg.clone("https://github.com/goedman/NMfE.jl")
and on e.g. OSX, to enable the notebooks:
ENV("PYTHON") = ""
Pkg.add("Gadfly")
Still inside the REPL:
using IJulia
@async notebook()
This should start a browser and the Jupyter/Julia notebook interface.
Some more info can be found at https://github.com/stevengj/julia-mit.
The approach taken in the NMfE.jl package is to provide 2 versions of most programs described in the NMfE book.
The first version is named p_xx_yy_for.jl
and is pretty much a direct translation of the Fortran program (i.e. $\mathbf{Program}$ $\mathbf{xx.yy}$) in the book.
Often, at the end of the ..._for.jl
program, a Julia solution is shown and the results are compared using an @assert statement.
If deemed useful a 'pure' Julia version is also available, labeled p_xx_yy.jl
.
Some of the Fortran functions that come with the book are contained in a separate library (nm_lib). Equivalent Julia functions are provided in the /src directory of the NMfE Julia package NMfE.jl.
The Fortran programs that come with both books use the Skyline format to store sparse matrices. My intention is to use the Julia sparse matrices in all versions. I do provide a few conversion functions such as fromSkyline()
.
In [26]:
using NMfE, IterativeSolvers
For an equation like:
$[\mathbf{A}]\{\mathbf{x}\}=\{\mathbf{b}\}$
we will provide $[\mathbf{A}]$ and intial values for $\{\mathbf{x_0}\}$:
In [27]:
A = [10. 5. 6.; 5. 20. 4.; 6. 4. 30.]
Out[27]:
Initial guess for $\{\mathbf{x}\}$:
In [28]:
x0 = [1., 1., 1.];
A ';' at the end of the last line in a cell suppresses the output.
In [29]:
tol = 1.0e-5
limit = 100;
In [30]:
(iters, eigval, eigvec) = nmex(A, x0; tol=tol, limit=limit, show=4);
In [31]:
println("No of iters: $iters")
println("Largest eigen value: $eigval")
println("Corresponding eigen vector: $eigvec")
Julia has the built-in function eig():
In [32]:
?eig
Out[32]:
In [33]:
jeigval, jeigvec = eig(A)
Out[33]:
No response to the '@assert ...' macro means 'true':
In [34]:
@assert round(maximum(jeigval), 3) == round(eigval, 3)
In [35]:
@assert round(maximum(jeigval), 4) == round(eigval, 4)
The values agree up to the 4th decimal.