![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1617888/image.png)
How to compute 4D numeric integrals?
5 views (last 30 days)
Show older comments
Hi, i'm trying to use the code available at https://it.mathworks.com/matlabcentral/fileexchange/47919-integraln-m to compute 4D integrals numerically. I cannot get it to work. Any suggestions? Many thanks. My script is as follows:
clc;
clear;
f4 = @(x,y,z,w)ones(size(x));
q4 = integralN(f4,0,1,0,1,0,1,0,1,'AbsTol',1e-5,'RelTol',1e-3) %this integral is computed ok, the function integralN is linked above
f=@(x,y,z,w)arrayfun(func,x,y,z,w);
q = integralN(f,0,1,0,1,0,1,0,1,'AbsTol',1e-5,'RelTol',1e-3) %this integral gives errors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = func(z1,z2,z3,z4)
z=[z1,z2,z3,z4];
out=func_vec(z);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = func_vec(z);
out = z(1)+z(2)+z(3)+z(4);
end
0 Comments
Answers (1)
Sachin Lodhi
on 15 Feb 2024
Hello Marco,
I noticed an issue in your code when reviewing it. Specifically, the line:
f=@(x,y,z,w)arrayfun(func,x,y,z,w);
is causing an error. When passing a function as an argument to another function, you need to use the @ symbol to indicate that it is a function handle. Here's an example for your reference from the Mathworks documentation page: https://in.mathworks.com/help/matlab/matlab_prog/pass-a-function-to-another-function.html#:~:text=b%20%3D%205%3B%0Aq1%20%3D-,integral(%40log%2Ca%2Cb),-q1%20%3D%203.0472
To correct your code, you should update the line to:
f=@(x,y,z,w)arrayfun(@func,x,y,z,w);
Implement this fix, and your code should work as expected. This is the output after executing updated code.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1617888/image.png)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!