MATLAB: Evaluating integrals multiple input functions and a matrix of data

anonymous functionembedded matlab functionfunctionMATLABmeshgrid-coordinates within functionsspherical harmonics

Dear all;
I'm working on a project and have need to numerically calculate an overlap integral over r,theta, phi. The function I'm looking at however is also a spherical harmonic (of specific form with l,m). Each coordinate input is a 3D meshgrid as this is being evaluated over a volume.
Currently my integrated function is of the form;
function rY_nm = (R.^n).*spharm(n,m,Theta,Phi); <-- 25ish lines + plotting.
assign variable: rY_nm(n,m,Theta,Phi);
funintegrall = funtest .*rY_nm.*sin(Theta);
inttheta2 = trapz(theta,fint); intphi2 = trapz(phi,inttheta2); intr1(n,mi) = trapz(r,intphi2);
Within a for loop which runs over n,m resulting in a set of coefficients at a each specific intr1(n,m). Here r, theta, phi are the linear coordinates (i.e unmeshed setup).
For my work however I need to use a much higher accuracy numerical method (even at 500x500x500 points the answers aren't following the pattern expected). I have access to a few such as integral and d001ah from the NAG libaries, however these require a function handle as an input which I have zero experience with. I have spent a significant amount of time trying to assign a function handle e.g fint = (R, Theta, Phi) (R.^n) .*spharm(n,m,Theta,Phi) or fint = (R, Theta, Phi,n,m) (R.^n).*spharm(n,m,Theta,Phi) to little success. Would anyone be able to advise or direct me into how i would assign function handles in this type of system?

Best Answer

If you have a speific problem, we can likely provide much more specific solutions.
For the time being, see the documentation on Function Basics (link), and Anonymous Functions (link).
Function handles are not difficult to define and use in practice. If you have yoiur own function file, for example:
function y = myFunction(x,n)
y = x.^n;
end
saved in your MATLAB path as ‘myFunction.m’, you would refer to it (for example in the integral (link) function) as:
n = 0:5;
a = 0;
b = 10;
int_y = integral(@(x)myFunction(x,n), a, b, 'ArrayValued',1);
since as an argument to another function, you would need to pass it as a function handle, using the ‘@’ operator.
This should at least help you understand the barest of essentials of function handle use, as well as an introduction to the integral function.
If you want significant accuracy in your integrals, I would use integral and related functions, if you have functions you want to integrate. The trapz function is for vectors and matrices.