# Plot integrals

**URL:** <https://discourse.julialang.org/t/plot-integrals/54087>\
**Category:** Visualization\
**Tags:** plotting, calculus, quadgk, integral\
**Created:** [January 27, 2021, 10:30pm UTC](https://discourse.julialang.org/t/plot-integrals/54087 "2021-01-27T22:30:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 27, 2021, 10:30pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/1 "2021-01-27T22:30:13Z")

</div>

I can plot derivatives using ForwardDiff and Plots

```julia
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?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 27, 2021, 10:58pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/2 "2021-01-27T22:58:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 27, 2021, 11:11pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/3 "2021-01-27T23:11:39Z")

</div>

Ok, how do I extract an output from QuadGK.

```julia
using QuadGK

f(x)=x/2
quadgx(f,1,x)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 27, 2021, 11:20pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/4 "2021-01-27T23:20:55Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 27, 2021, 11:21pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/5 "2021-01-27T23:21:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 27, 2021, 11:25pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/6 "2021-01-27T23:25:14Z")

</div>

> [@brett\_knoss](#):
>
> Ok, how do I extract an output from QuadGK.

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).

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 27, 2021, 11:27pm UTC](https://discourse.julialang.org/t/plot-integrals/54087/7 "2021-01-27T23:27:53Z")

</div>

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