Using quadgk() after a real() function in Julia

Viewed 31

when I want to extract a real part of g(x) and integrate a new function g1(x), the result b contains a Vector {Float 64} rather than just a number. How to get its integrated result?

Here is the code

g(x) = (exp.(-x).^2).*exp.(1im*x)

R_initial=real(g(x));

g1(x)= R_initial

b = quadgk(g1,0.6,0.7)

c = b[1]
1 Answers

Just do the following:

julia> g(x) = real((exp(-x)^2)*exp(1im*x))
g (generic function with 1 method)

julia> quadgk(g,0.6,0.7)
(0.021750435727577098, 3.469446951953614e-18)

In your code:

R_initial=real(g(x))

takes some global value of x that you probably have and R_initial is bound to some value. So you get something like:

julia> R_initial = [1.0, 2.0]
2-element Vector{Float64}:
 1.0
 2.0

julia> g1(x) = R_initial
g1 (generic function with 1 method)

julia> quadgk(g1,0.6,0.7)
([0.09999999999999998, 0.19999999999999996], 0.0)

which produces a vector in the first element as the input is multi-dimensional. Note that g1 is just a constant function.

Related