Why do I receive matrix dimensions errors when using INTEGRAL in MATLAB 7.14 (R2012a)?

When using the INTEGRAL command as follows,
integral(@(x) x^2,0,100)
I receive an error:
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Error in @(x)x^2
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
This issue also occurs when I multiply "x" in functions such as "x*sin(x)". Additionally, I get another type of error when using divisions:
integral(@(x) x/exp(x),0,100)
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is
an array-valued integrand, set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);

 Réponse acceptée

The first error indicates that the matrix dimensions do not agree. This is because the INTEGRAL command passes the value of "x" into the function argument as a vector. However, since the "^" operator tries to square this vector, the matrix dimensionality error occurs. This is same as trying:
[1 2 3]^2
For divisions, the vectors involved in the division undergo matrix division and yield a scalar. However, the output that is expected by INTEGRAL is a vector. Therefore, although there is no error when calculating the division, an error occurs while evaluating the integral.
To work around these issues, please use element-wise math operators in the function definition. The correct commands would be:
integral(@(x) x.^2,0,100)
integral(@(x) x./2,0,100)

Plus de réponses (0)

Catégories

Produits

Version

R2012a

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by