Last active
August 29, 2015 14:13
-
-
Save ericjang/c0e0c1bff4a1d775b559 to your computer and use it in GitHub Desktop.
Automatic Differentiation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# testing the automatic differentiation capabilities | |
using Cairo; | |
using DataFrames; | |
using Gadfly; | |
using ForwardDiff; | |
f(x) = sin(x[1]); | |
x = [-pi:.1:pi]; | |
outf=[f(i) for i in x]; | |
g = forwarddiff_gradient(f, Float64, fadtype=:typed); | |
outg = [g([i])[1] for i in x]; | |
# plot using dataframes + gadfly | |
n = length(x); | |
d=DataFrame( | |
x=vcat(x, x), | |
y=vcat(outf, outg), | |
key = vcat(rep("f(x)",n), | |
rep("grad(x)",n)) | |
); | |
p = plot(d, | |
x="x", | |
y="y", | |
color="key", | |
Geom.line); | |
# save it out | |
draw(PNG("myplot.png", 6inch, 3inch), p); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment