Use of 'ArrayValued' in Matlab numerical integration

86 ビュー (過去 30 日間)
Renzo Del Fabbro
Renzo Del Fabbro 2022 年 9 月 23 日
回答済み: Torsten 2022 年 9 月 23 日
Why in Matlab numerical integration
f = @(x) 5;
integral(f,0,2,'ArrayValued',true)
needs 'ArrayValued',true ... while
f = @(x) x;
integral(f,0,2)
don't need it?

採用された回答

Davide Masiello
Davide Masiello 2022 年 9 月 23 日
編集済み: Davide Masiello 2022 年 9 月 23 日
Let's take a look at the error message
f = @(x) 5;
integral(f,0,2)
Error using integralCalc/finalInputChecks
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 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
The important line here is "Output of the function must be the same size as the input".
I believe this could be due to the fact that integral passes a whole array of x-values in the attempt to integrate the function within the default tolerances.
If your functions does not depend on x, then the ouput would be a single scalar and therefore different from the input.
Specifing 'ArrayValued' as true, you tell the solver that the output must be an array the same size as the x passed to it.
In fact, you could try to integrate an equivalent function where the dependance on x is explicit, and it would work without specifying 'ArrayValued' as true.
f = @(x) 5*(x-x+1);
integral(f,0,2)
10
  1 件のコメント
Renzo Del Fabbro
Renzo Del Fabbro 2022 年 9 月 23 日
Thank you for your help Davide.

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2022 年 9 月 23 日
f = @(x) 5*ones(size(x));
integral(f,0,2)
ans = 10

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by