How to evaluate this triple Integral?

I want to evaluate the triple integral of dxdydz using 'integral3'. But the only code my intuition has helped me it this:
g = @(x,y,z) 1
u = integral3(g,1,2,1,3,1,4)
But this results in errors. Please help me create the correct code.

 Accepted Answer

Torsten
Torsten on 26 Apr 2018
Edited: Torsten on 26 Apr 2018
g = @(x,y,z)x-x+1;
u = integral3(g,1,2,1,3,1,4)

More Answers (1)

From the description of the fun input argument in the integral3 documentation: "Integrand, specified as a function handle, defines the function to be integrated over the region xmin ≤ x ≤ xmax, ymin(x) ≤ y ≤ ymax(x), and zmin(x,y) ≤ z ≤ zmax(x,y). The function fun must accept three arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
Your function does not return an array the same size as the input arrays. Torsten's approach works (as long as x must be finite) but it would be easy (if there is no comment explaining why you're subtracting x from x) for someone to "optimize" that command by eliminating the "x-x". I recommend being a bit more explicit:
g = @(x, y, z) ones(size(x));
integral3(g, 1, 2, 1, 3, 1, 4)

3 Comments

Can you explain why
g = @(x, y, z) ones(numel(x),1);
integral3(g, 1, 2, 1, 3, 1, 4)
does not work ?
It would work, if integral3 were guaranteed to pass three column vectors into the integrand function.
All integrand3 says is that the integrand function must accept three arrays of the same size. Those three arrays could be scalars, row vectors, column vectors, matrices, or N-dimensional arrays. Having the same number of elements is not sufficient. The output of the integrand function must be exactly the same size and shape as the inputs.
I see - thank you very much.

Sign in to comment.

Asked:

on 26 Apr 2018

Commented:

on 27 Apr 2018

Community Treasure Hunt

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

Start Hunting!