Skip to content

Instantly share code, notes, and snippets.

@ericjang
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericjang/c0e0c1bff4a1d775b559 to your computer and use it in GitHub Desktop.
Save ericjang/c0e0c1bff4a1d775b559 to your computer and use it in GitHub Desktop.
Automatic Differentiation Example
# 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