How to compute 4D numeric integrals?

2 ビュー (過去 30 日間)
Marco Benedetti
Marco Benedetti 2021 年 12 月 15 日
回答済み: Sachin Lodhi 2024 年 2 月 15 日
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

回答 (1 件)

Sachin Lodhi
Sachin Lodhi 2024 年 2 月 15 日
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.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by