These Julia notebooks accompany the book "Numerical Methods for Engineers" by D. V. Griffiths and I.M. Smith NMfE.
Neither the Julia package NMfE.jl nor these notebooks are a replacement for NMfE. The intention is to use the package or the notebooks as a replacement for the Fortran programs while studying the book.
NMfE by itself provides a practical, step by step introduction to the programming of a variety of numerical methods.
NMfE can also be viewed as an introduction to the methods used in a follow on book "Programming the Finite Element Method" PtFEM, also by D. V. Griffiths and I.M. Smith.
My intention is to develop a second package, PtFEM.jl, and associated notebooks which make the Finite Element toolkit explained in PtFEM available in Julia.
I have not started on that package in earnest, although I have experimented in a temporary package called CSoM.jl CSoM covering approximately the first six chapters of PtFEM. Once PtFEM.jl starts to take shape, I will deprecate CSoM.jl.
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 f_xx_yy.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 f_xx_yy.jl
programs, 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 j_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, e.g. fromSkyline().
In [26]:
using NMfE
In Julia, to solve 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 this case I have opted for capturing the core part of program nmex in a Julia function nmex()
stored in the /src/ch01 directory of the NMfE Julia package NMfE.jl:
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, hence above AssertionError.