How to Symbolic and Numerical Integration

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

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.

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

Do you want the answer symbolically or numerically?

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

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

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.

4 Likes

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

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

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

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

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.