How can I use a numerical solution to an equation as an anonymous function for an integration?

2 views (last 30 days)
I am attempting to do a numerical triple integral using anonymous functions to set up the integrand. Up until now, the approximations I have made in deriving the equations have been sufficient, and to do the integral I have the code:
kf = @(k0,psi) k00+k06*cos(6*(psi))+k012*cos(12*(psi))+k31*sin(c*k0/N).*cos(3*(psi))+k20*cos(2*c*k0/N);
vperp = @(k0,psi) -3*hbar/m.*(2*k06.*sin(6.*(psi))+4*k012.*sin(12.*(psi))+k31.*sin(c.*k0./N).*cos(3.*(psi)));
v = @(k0,psi) hbar*kf(k0,psi)/m;
vx = @(k0,psi) cos(psi).*v(k0,psi)-sin(psi).*vperp(k0,psi);
integrand = @(k0,psi,psipr) vx(k0-k00*cos(phi)*sin(theta),psi).*vx(k0-k00*cos(phi)*sin(theta),psipr).*exp(psipr.*cos(theta)./wt0);
limit = @(k0,psi) psi*cos(theta)+phi;
f = e^2*cos(theta)*m/(4*pi^3*hbar^2*omega_c)*integral3(integrand,-pi/c,pi/c,phi,2*pi/cos(theta)+phi,-Inf,limit);
phi is an angle between 0 and 2pi, N=3, and hbar and m are Planck's reduced constant and the mass of the electron respectively.
However, now I have changed some parameters so that my original approximations are incorrect, so now the 1st argument of integrand must be, instead of that shown above the solution to the equation:
*x* = k0 - (k00 + k31*sin( *x* *c/3)*cos(3*psi))*cos(phi)*sin(theta
This can only be solved numerically, obviously.
I would love to know how to get this to work, either using the method I've currently been using, or by another method, so if anyone has any ideas, that would be great!
Thanks

Accepted Answer

Matt J
Matt J on 11 Jan 2014
Edited: Matt J on 11 Jan 2014
integrand=@(k0,psi,psipr)...
fzero(@(x)k0-(k00+k31*sin(c/3)*cos(3*psi))*cos(phi)*sin(theta)-x,...
[xlow,xhigh] )
  8 Comments
Matt J
Matt J on 20 Jan 2014
Edited: Matt J on 20 Jan 2014
You're missing an @ operator. You need to specify "integrand" using a function handle, not just its name.
sigxxintegrand = @(k0,psi,psipr) arrayfun(@integrand,k0,psi,psipr);
Joseph
Joseph on 20 Jan 2014
Edited: Joseph on 20 Jan 2014
Ahh thank you, I thought it would end up being something stupid like that. I am still experiencing problems with the code, as it gives me NaN for the final result, which seems to be an issue with the integration, as the arrayfun part seems to be working fine. I believe the issue is with the limit which is a function of psi, as if I replace it with 0 the program seems to work fine. I have also slightly changed the definition of limit so that we have
function l = limit(k0,psi)
l = psi.*cos(theta)+phi;
end
Then we have
integrallimit = @(k0,psi) arrayfun(@limit,k0,psi);
Sxx1 = @(k0,psi) integral(@(psipr)sigxxintegrand(k0,psi,psipr),-Inf,integrallimit(k0,psi),'ArrayValued',true);
If no immediately obvious reason or solution to this springs to mind, please don't worry, you've been a great help, thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!