The symbol "1" is not a scalar in integral3

32 visualizaciones (últimos 30 días)
Richard
Richard el 20 de Oct. de 2025 a las 13:32
Editada: Walter Roberson el 21 de Oct. de 2025 a las 5:14
I can numerically evaluate a triple integral of a function like x.*y.*z. But when I try to integrate a constant function 1, I get errors. How do a signal to Matlab that 1 is a scalar. My goal is to compute a volume by doing a triple integral over a 0,1-function.
integral3(@(x,y,z) x.*y.*z, 0,1,0,1,0,1)
ans =
0.1250
>> integral3(@(x,y,z) 1, 0,1,0,1,0,1)
Error using integral2Calc>tensor (line 253)
Integrand output size does not match the input size.
Error in
integral2Calc>integral2t (line 55)
[Qsub,esub,FIRSTFUNEVAL,NFE] = tensor(thetaL,thetaR,phiB,phiT,[],[], ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
integral3>innerintegral (line 128)
Q1 = integral2Calc( ...
^^^^^^^^^^^^^^^^^^
Error in
integral3>@(x)innerintegral(x,fun,yminx,ymaxx,zminxy,zmaxxy,integral2options) (line 111)
f = @(x)innerintegral(x, fun, yminx, ymaxx, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
integralCalc>iterateScalarValued (line 334)
fx = FUN(t);
^^^^^^
Error in
integralCalc>vadapt (line 148)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
integralCalc (line 77)
[q,errbnd] = vadapt(vfunAB,interval, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in
integral3 (line 113)
Q = integralCalc(f,xmin,xmax,integralOptions);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1 comentario
Dyuman Joshi
Dyuman Joshi el 20 de Oct. de 2025 a las 13:51
See the error message - "Integrand output size does not match the input size."
From the documentation - "The function fun must accept three arrays of the same size and return an array of corresponding values. It must perform element-wise operations."
When you define your function as -
f = @(x,y,z) 1;
It does not necessary accept three arrays of same size, and the output size is not always same as the input size.
@the cyclist shows an approach below on how to perform the integral.

Iniciar sesión para comentar.

Respuestas (2)

the cyclist
the cyclist el 20 de Oct. de 2025 a las 13:35
Here is one way:
integral3(@(x,y,z) ones(size(x)), 0,1,0,1,0,1)
ans = 1

John D'Errico
John D'Errico el 20 de Oct. de 2025 a las 14:25
Editada: John D'Errico el 20 de Oct. de 2025 a las 14:31
If ALL you want to do is compute a volume, especially of such a simple domain, there are better ways to do so. If the domain is a complicated one, then yes, integral3 will do so. But will it be fast? Not necessarily, as integral3 is not really targetted to solve that class of problem, even though you can make it work.
In this case, since the volume is a simple convex one, I'll use an alpha shape, with alpha set to inf.
xyz = dec2bin(0:7) - '0'
xyz = 8×3
0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = alphaShape(xyz,inf);
volume(S)
ans = 1.0000
Essentially, as long as the domain can be dissected into a tessellated one, then any tool that can compute the volume of each simplex, then add them all up will suffice. You can find them all over, look on the File Exchange, I think I even have one up there.
@the cyclist has already shown how to solve the problem using integral3.

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Productos


Versión

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by