Evaluating integrals multiple input functions and a matrix of data
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Logan Turton
il 9 Mar 2019
Commentato: Star Strider
il 11 Mar 2019
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?
0 Commenti
Risposta accettata
Star Strider
il 9 Mar 2019
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.
2 Commenti
Star Strider
il 11 Mar 2019
I’m lost. (I also don’t see where the ‘D’ matrix comes into all this, although I may have missed something.)
You need to use element-wise operations here:
fint = @(n,m,R,Theta,Phi)...
funnm(R,Theta,Phi).*rYnm(n,m,R,Theta,Phi).*sin(Theta);
You may also need to use the ('ArrayValued',true) name-value pair, as I did in my example code.
Here, you need to specify the variable-of-integration:
inttheta = integral(fint(n,m,R,Theta,Phi),a,b)
Since you defined ‘a’ and ‘b’ as the limits of ‘Theta’, this may be appropriate:
inttheta = integral(@(Theta) fint(n,m,R,Theta,Phi), a, b)
noting that all the other variables and arguments must already be defined in your workspace.
If you are doing multiple integrations, you can use the related integral functions. However only integral accepts ('ArrayValued',true). If your multiple integral is array-valued, you need to use it with the integral function to create an appropriate multiple integration with array-valued evaluations integrating with respect to each variable.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!