# Plot integrals

I can plot derivatives using ForwardDiff and Plots

using ForwardDiff
using Plots

f(x)=x^2
df(x)=ForwardDiff.derivative(f(x), x)
plot(df)


but QuadGK.jl gives two answers so this method won’t work. Is there a way to plot the integral using QuadGK, or another integral package, that gives one output, like ForwardDiff?

Just extract the first output from QuadGK? EDIT: And please include reproducible code when you post questions.

Ok, how do I extract an output from QuadGK.

using QuadGK

f(x)=x/2


The same way as anything else in Julia: store the result in a variable, for example:

julia> f(x) = x / 2
f (generic function with 1 method)

julia> y = 10
10

julia> z = quadgk(f, 1, y)
(24.75, 0.0)

julia> z[1]
24.75


And use the REPL or notebook to experiment and play around.

Are you looking for something like plot(x -> quadgk(f, 1, x)[1])?

That is, if you want to take advantage of the fact that the first plot argument can be a function, you pass a function x -> quadgk(f, 1, x)[1] that takes an x and returns \int_1^x f(x')dx', using [1] to get the first return value of quadgk (which returns a tuple).

3 Likes

Perfect, and I’ll be sure to play around with different variations @dpsanders !

1 Like