Problems with integration using functions
Show older comments
Dear all, I have a problem when doing integration twice with a defined function. My code is as follows: First I define a function psitimesX(x) with variable x, this function has to be integrated over variable p by a function handle under function psitimesX. Next I call back the function and integrate it over x from -infinity to +infinity. I am not sure what has does wrongly as matlab just does not gives me input(need to wait for a long time), I think there is something wrong. Or is there an alternative way to do this task of integrating twice?
clear all;
%%%integrating variable over x
expecX_xbasis = integral(@psitimesX,-inf, inf,'ArrayValued',true)
function psitimesX = psitimesX(x)
%%%Define parameters
J = -0.4 ;
theta = 0.4;
omega_y =0.04;
omega_z = -0.052;
m=1/(-2*J);
gammavar= 5;
gamma_profile=0.05;
correctionfactor = (pi/gamma_profile)^0.25;
%%%function of x, with function handle with variable p
wavefunction_xbasis_component1 = @(p)(1/sqrt(2)).*(1./sqrt(2.*pi)).*correctionfactor.*((exp(-1i.*(0:0.1:150).*(2.*J*cos(p).*cos(theta))-gammavar.*p.^2 + 1i.*p.*x))).*(cos((0:0.1:150).*sqrt(omega_y^2 +omega_z^2 + J^2 -(J^2).*cos(2*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))) ) + 1i.*(omega_z - 2.*J*sin(p).*sin(theta)).*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2)*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))/sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta)*sin(p) + 2.*omega_z*sin(theta))) +omega_y.*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta)))) ;
wavefunction_xbasis_component2 = @(p)(1/sqrt(2)).*(1./sqrt(2.*pi)).*correctionfactor.*((exp(-1i.*(0:0.1:150).*(2.*J.*cos(p).*cos(theta))-gammavar.*p.^2 + 1i.*p.*x))).*(cos((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2*theta).*sin(p) + 2.*omega_z.*sin(theta)))) -1i.*(omega_z - 2.*J.*sin(p).*sin(theta)).*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))) -omega_y.*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta)*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2*omega_z.*sin(theta)))) ;
%%%psitimesX is a function of x, after integrating over variable p
psitimesX = x.*(abs(integral(wavefunction_xbasis_component1,-inf, inf,'ArrayValued',true)).^2 + abs(integral(wavefunction_xbasis_component2,-inf, inf,'ArrayValued',true)).^2 ) ;
end
3 Comments
Star Strider
on 5 Sep 2024
Moved: Star Strider
on 5 Sep 2024
With this slight change:
expecX_xbasis = integral(@(x)psitimesX(x),-inf, inf,'ArrayValued',true)
I was able to get it to work, however it is taking forever and throwing this warning:
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 2.5e-06. The
integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
three times in the first 15 miinutes of running time.
I am stopping it.
You can run it and then see what the problems are wiith it.
clear all;
%%%integrating variable over x
expecX_xbasis = integral(@(x)psitimesX(x),-inf, inf,'ArrayValued',true)
function psitimesX = psitimesX(x)
%%%Define parameters
J = -0.4 ;
theta = 0.4;
omega_y =0.04;
omega_z = -0.052;
m=1/(-2*J);
gammavar= 5;
gamma_profile=0.05;
correctionfactor = (pi/gamma_profile)^0.25;
%%%function of x, with function handle with variable p
wavefunction_xbasis_component1 = @(p)(1/sqrt(2)).*(1./sqrt(2.*pi)).*correctionfactor.*((exp(-1i.*(0:0.1:150).*(2.*J*cos(p).*cos(theta))-gammavar.*p.^2 + 1i.*p.*x))).*(cos((0:0.1:150).*sqrt(omega_y^2 +omega_z^2 + J^2 -(J^2).*cos(2*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))) ) + 1i.*(omega_z - 2.*J*sin(p).*sin(theta)).*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2)*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))/sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta)*sin(p) + 2.*omega_z*sin(theta))) +omega_y.*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta)))) ;
wavefunction_xbasis_component2 = @(p)(1/sqrt(2)).*(1./sqrt(2.*pi)).*correctionfactor.*((exp(-1i.*(0:0.1:150).*(2.*J.*cos(p).*cos(theta))-gammavar.*p.^2 + 1i.*p.*x))).*(cos((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2*theta).*sin(p) + 2.*omega_z.*sin(theta)))) -1i.*(omega_z - 2.*J.*sin(p).*sin(theta)).*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p)*(J.*cos(2.*theta).*sin(p) + 2.*omega_z.*sin(theta))) -omega_y.*sin((0:0.1:150).*sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta)*sin(p) + 2.*omega_z.*sin(theta))))./sqrt(omega_y.^2 +omega_z.^2 + J.^2 -(J.^2).*cos(2.*p) -2.*J.*sin(p).*(J.*cos(2.*theta).*sin(p) + 2*omega_z.*sin(theta)))) ;
%%%psitimesX is a function of x, after integrating over variable p
psitimesX = x.*(abs(integral(wavefunction_xbasis_component1,-inf, inf,'ArrayValued',true)).^2 + abs(integral(wavefunction_xbasis_component2,-inf, inf,'ArrayValued',true)).^2 ) ;
end
.
Tsz Tsun
on 5 Sep 2024
Moved: Star Strider
on 5 Sep 2024
Star Strider
on 5 Sep 2024
Moved: Star Strider
on 5 Sep 2024
I am runniing it in the background, on MATLAB Online. It appears to me to be not close to converging.
Accepted Answer
More Answers (0)
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!