# How to Symbolic and Numerical Integration

**URL:** <https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051>\
**Category:** New to Julia\
**Created:** [February 4, 2023, 4:47pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051 "2023-02-04T16:47:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 4, 2023, 4:47pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/1 "2023-02-04T16:47:44Z")

</div>

I have a problem in using SymPy and QuadGK packages as follows:  
In a double integral, the inner integration has lower limit is `x` where `x` is the variable of the outer integral. Here is my code

```julia
using SymPy 
@syms y
f(z) = integrate(2*z*cos(y+z)/(1+y^2),(y,z,10))^2
t1 = integrate(f,0,1)
t1.evalf() # failed
# I also tried with QuadGK
using QuadGK
t2 = quadgk(f,0,1) # also failed 

```

How could I obtain numerical value of this double integral.

Thank you.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [February 4, 2023, 5:06pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/2 "2023-02-04T17:06:45Z")

</div>

Is this function really the one you want ? because i feel like you may be able to obtain the value analytically.

---

<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:** [February 4, 2023, 5:37pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/3 "2023-02-04T17:37:23Z")

</div>

> [@Firstname\_Lastname](#):
>
> `I also tried with QuadGK`

Do you want the answer symbolically or numerically?

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 4, 2023, 6:46pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/4 "2023-02-04T18:46:24Z")

</div>

Sorry for my late reply. The function here is just an example. In my own problem, the function `f(z)` is more complicated that I am not sure to have analytical form of `t1`. Anyway, I would like to have both analytical and numerical value of this example problem. Thank you @Irnv

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 4, 2023, 6:48pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/5 "2023-02-04T18:48:44Z")

</div>

Sorry for my late reply. I would like to have the answer both symbolically and numerically. In case there is no symbolic solution, I would like to have it numerically. Thank you @stevengj

---

<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:** [February 4, 2023, 7:17pm UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/6 "2023-02-04T19:17:46Z")

</div>

> [@Firstname\_Lastname](#):
>
> In case there is no symbolic solution, I would like to have it numerically.

In this case (likely for more complicated integrands), you shouldn’t use Symbolics at all. Either use nested calls to QuadGK or use a multi-dimensional integration package like HCubature.

---

<div class="post-metadata">

**Author:** ![WojciechBojewski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wojciechbojewski/32/46526_2.png) [@WojciechBojewski](https://discourse.julialang.org/u/WojciechBojewski)\
**Post date:** [February 5, 2023, 12:02am UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/7 "2023-02-05T00:02:12Z")

</div>

Could you describe your problem, paste some tracelog? What does it mean failed?  
I run your code into my Julia and works…

```julia
julia> @syms y
(y,)

julia> f(z) = integrate(2*z*cos(y+z)/(1+y^2),(y,z,10))^2
f (generic function with 1 method)

julia> t1 = integrate(f,0,1)

  1
  ⌠
  ⎮ 2
  ⎮ ⎛10 ⎞
  ⎮ ⎜⌠ ⎟
  ⎮ 2 ⎜⎮ cos(x + y) ⎟
4⋅⎮ x ⋅⎜⎮ ────────── dy⎟ dx
  ⎮ ⎜⎮ 2 ⎟
  ⎮ ⎜⎮ y + 1 ⎟
  ⎮ ⎜⌡ ⎟
  ⎮ ⎝x ⎠
  ⌡
  0

julia>

julia> t1.evalf()
    1
    ⌠
    ⎮ 2
    ⎮ ⎛10 ⎞
    ⎮ ⎜⌠ ⎟
    ⎮ 2 ⎜⎮ cos(x + y) ⎟
4.0⋅⎮ x ⋅⎜⎮ ────────── dy⎟ dx
    ⎮ ⎜⎮ 2 ⎟
    ⎮ ⎜⎮ y + 1 ⎟
    ⎮ ⎜⌡ ⎟
    ⎮ ⎝x ⎠
    ⌡
    0

```

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 5, 2023, 12:49am UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/8 "2023-02-05T00:49:54Z")

</div>

From your suggestion, I came to the final workable solution below

```julia
using QuadGK
f(z) = quadgk(y->2*z*cos(y+z)/(1+y^2),z,10)[1]^2
t = quadgk(f,0,1)

```

Thank you @stevengj

---

<div class="post-metadata">

**Author:** ![Firstname\_Lastname](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firstname_lastname/32/34201_2.png) [@Firstname\_Lastname](https://discourse.julialang.org/u/Firstname_Lastname)\
**Post date:** [February 5, 2023, 12:54am UTC](https://discourse.julialang.org/t/how-to-symbolic-and-numerical-integration/94051/9 "2023-02-05T00:54:36Z")

</div>

By “failed”, I meant that no analytical nor numerical solution is found. But from suggestion of @stevengj, I could have numerical value of this double integral by using nested quadgk.
