MATLAB: Integral2 won’t work, is there a solution

integral2integrationnumerical integration

Hello everybody,
In the following code I don't understand why integral2 doesn't work. Could you suggest me a solution to achieve the integration? Currently I am calculating many values with vpa and then I use twice trapz to make it work but it takes really long and the accuracy is not very good…
Thank you in advance
clear all
WaveFunction = @(x) exp(-x.^2./2)./pi.^(0.25);
expression1 = @(x,p,y) exp(-i.*p.*y).* WaveFunction(x-y./2).* conj(WaveFunction(x+y./2));
syms y
WignerFunction = @(x,p) 1/(2.*pi).* int(expression1(x,p,y),y,-Inf,Inf);
t = isa(WignerFunction,'function_handle') % outputs 1
%vpa(WignerFunction(1,1)) % outputs the correct value
t = integral2(WignerFunction,-Inf,Inf,-Inf,Inf) % should output 1

Best Answer

int() returns a symbolic expression, which might or might not contain any free variables. integral() and integral2() can only work with numeric expressions, not symbolic expressions.
If your int() returns a symbolic expression that has no unbound variables, then you can use
WignerFunction = @(x,p) 1/(2.*pi).* double(int(expression1(x,p,y),y,-Inf,Inf));