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일

2 개 추천

g = @(x,y,z)x-x+1;
u = integral3(g,1,2,1,3,1,4)

추가 답변 (1개)

Steven Lord
Steven Lord 2018년 4월 26일

0 개 추천

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

Torsten
Torsten 2018년 4월 27일
편집: Torsten 2018년 4월 27일
Can you explain why
g = @(x, y, z) ones(numel(x),1);
integral3(g, 1, 2, 1, 3, 1, 4)
does not work ?
Steven Lord
Steven Lord 2018년 4월 27일
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.
Torsten
Torsten 2018년 4월 27일
I see - thank you very much.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2018년 4월 26일

댓글:

2018년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by