Why I can't plot the integral function with SymPy and Plots?

Hi all,

I have this simple integral function:

\int (5 + \sin \ x)^{4} \ dx

and I want to plot the integral function above so I use this code:

using SymPy, Plots

f(x) = integrate((5 + sin(x))^4)


but it gets errors:

I also concern about the Warning when I type using Plots why is this occurring? I added today a new package QuadGK and maybe that is why…

My package list:

 Status `~/LasthrimProjection/Project.toml`
  [537997a7] AbstractPlotting v0.18.3
  [6e4b80f9] BenchmarkTools v1.3.2
  [3391f64e] CDDLib v0.7.0
  [13f3f980] CairoMakie v0.5.10
  [5ae59095] Colors v0.12.8
  [39dd38d3] Dierckx v0.5.2
  [b4f34e82] Distances v0.10.7
  [5752ebe1] GMT v0.43.1
  [d997a800] Implicit3DPlotting v0.2.3 `https://github.com/matthiashimmelmann/Implicit3DPlotting.jl.git#main`
  [95701278] ImplicitEquations v1.0.9
  [a98d9a8b] Interpolations v0.14.6
  [d1acc4aa] IntervalArithmetic v0.20.8
  [d8418881] Intervals v1.8.0
  [b964fa9f] LaTeXStrings v1.3.0
  [b4f0291d] LazySets v2.3.0
  [ae8d54c2] Luxor v3.5.0
  [ebaf19f5] MTH229 v0.2.11
  [961ee093] ModelingToolkit v8.11.0
  [429524aa] Optim v1.7.3
  [1dea7af3] OrdinaryDiffEq v6.11.2
  [f0f68f2c] PlotlyJS v0.18.10
  [91a5bcdd] Plots v1.36.0
  [67491407] Polyhedra v0.6.17
  [f27b6e38] Polynomials v3.2.0
  [438e738f] PyCall v1.94.1
  [1fd47b50] QuadGK v2.6.0
  [ce6b1742] RDatasets v0.7.7
  [6e93f119] SchwarzChristoffel v0.1.11
  [24249f21] SymPy v1.1.7
  [78aadeae] SymbolicNumericIntegration v0.8.6
  [9ec6d097] TruthTables v0.4.2
  [e88e6eb3] Zygote v0.6.44
  [de0858da] Printf


I don’t think this does what you think it does:

julia> f(x) = integrate((5 + sin(x))^4)
f (generic function with 1 method)

julia> f(1)
ERROR: MethodError: no method matching integrate(::Float64)

when you pass x, (5 + sin(x))^4 is just a nuber - e.g. for x = pi you get 5^4 = 625 so you are essentially doing

julia> integrate(625.0)
ERROR: MethodError: no method matching integrate(::Float64)

which doesn’t make a lot of sense. integrate is expecting a function rather than a value, so you probably meant:

julia> g = integrate(y -> (5 + sin(y))^4)
       4             2       2                            4
3β‹…xβ‹…sin (x)   3β‹…xβ‹…sin (x)β‹…cos (x)           2      3β‹…xβ‹…cos (x)           2
─────────── + ─────────────────── + 75β‹…xβ‹…sin (x) + ─────────── + 75β‹…xβ‹…cos (x)
     8                 4                                8

               3                                             3
          5β‹…sin (x)β‹…cos(x)         2             3β‹…sin(x)β‹…cos (x)
+ 625β‹…x - ──────────────── - 20β‹…sin (x)β‹…cos(x) - ──────────────── - 75β‹…sin(x)β‹…
                 8                                      8

         40β‹…cos (x)
cos(x) - ────────── - 500β‹…cos(x)

Now you can pass a value which will be substituted to derive the integral:

julia> g(2)
     4                  3           3                  2       2           4
3β‹…cos (2)   3β‹…sin(2)β‹…cos (2)   5β‹…sin (2)β‹…cos(2)   3β‹…sin (2)β‹…cos (2)   3β‹…sin (2
───────── - ──────────────── - ──────────────── + ───────────────── + ────────
    4              8                  8                   2               4

)   40β‹…cos (2)         2                    2                                2
─ - ────────── - 20β‹…sin (2)β‹…cos(2) + 150β‹…cos (2) - 75β‹…sin(2)β‹…cos(2) + 150β‹…sin

(2) - 500β‹…cos(2) + 1250

That’s a really clear and easy to understand explanation thanks @nilshg

Why is the integral calculation for function g that is created from

g = integrate(y -> (5 + sin(y))^4)

different with the function that I input directly:

julia> integrate(g, 0, 2Ο€)
julia> integrate((5+sin(x))^4, (x, 0, 2pi))

Those are two different functions you are integrating (compare g to (5+sin(x))^4).

The method integrate(f::Function, ...) was intended to be deprecated (which might help clear this up). I’d suggest only integrating symbolic expressions formed directly, as with (5+sin(x))^4 or through a function, like f(x) (not just f).

1 Like

g is already the integrated function and you do not need to integrate again.

To have the finite integral you want, just use


which yields 4400.5859095159

1 Like

Thanks a lot for helping me to understand more.

Thanks a lot, I think it is clearer now to me.