# install package: # julia -e 'using Pkg; Pkg.add("ForwardDiff"); Pkg.add("Plots"); Pkg.add("PyPlot")' # run from Julia console: # include("aziz-diff.jl") using ForwardDiff: derivative using Printf # definition of the function in file aziz.jl in current directory push!(LOAD_PATH,pwd()) # add current directory to module search path using Aziz # alternative: you could # include("aziz.jl") # but then you'll need to use Aziz.aziz everywhere, unless you define aziz = Aziz.aziz r = [0.01:0.5:10.0;] V = aziz.(r) Vp = derivative.(aziz, r); @printf("%15s %15s %30s\n","r","V(r)","V'(r)") for (a,b,c) in zip(r,V,Vp) @printf("%15.5f %15.5f %30.20f\n",a,b,c) end r0 = 7.61 println("\ncomparing numerical der=(f(r+h)-f(r))/h and present AD result at r=$r0:\n") Vp0 = derivative.(aziz, r0); h = 0.1 a0 = aziz(r0) while h > 1.e-14 global h der = (aziz(r0+h)-a0)/h @printf("h=%10.3e der=%25.20f AD = %25.20f der-AD=%25.20f\n", h, der, Vp0, der-Vp0) h = 0.1*h end println("plotting...") using Plots pyplot() # pyplot backend # more points for plotting r = [0.01:0.01:10.0;] V = aziz.(r) Vp = derivative.(aziz, r); p = plot(r,V,label="V(r)",ylims=(-20,20)) p = plot!(r,Vp,label="V'(r)") # update previous plot # just to make the plot appear long enough to see it display(p) readline()