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.

 采纳的回答

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

更多回答(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 个评论

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.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by