integral() doesn't accept scalar function as input

조회 수: 7 (최근 30일)
Julian Groß-Funk
Julian Groß-Funk 2023년 7월 5일
댓글: Star Strider 2023년 7월 5일
Psi = @(x) [1 x]
Psi = function_handle with value:
@(x)[1,x]
fun = @(x) abs(Psi(x)*Psi(x)')
fun = function_handle with value:
@(x)abs(Psi(x)*Psi(x)')
integral(fun,0,1)
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);
It is necessary for me to numerically integrate over a function (fun), that gets a vector-function (Psi) as input. fun is NOT array-valued. Why doesn't this work?

채택된 답변

Star Strider
Star Strider 2023년 7월 5일
It works correctly with the 'ArrayValued' name-value pair —
Psi = @(x) [1 x]
Psi = function_handle with value:
@(x)[1,x]
fun = @(x) abs(Psi(x)*Psi(x)')
fun = function_handle with value:
@(x)abs(Psi(x)*Psi(x)')
integral(fun,0,1, 'ArrayValued',1)
ans = 1.3333
.
  댓글 수: 2
Julian Groß-Funk
Julian Groß-Funk 2023년 7월 5일
Thank you very much!
Star Strider
Star Strider 2023년 7월 5일
As always, my pleasure!

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

추가 답변 (1개)

Jayant
Jayant 2023년 7월 5일
The error you encountered occurs because the function fun you defined is not compatible with the integral function in MATLAB. The integral function expects the integrand function to have a scalar output for each input value, but in your case, the fun function returns a vector output.
To resolve this issue, you can modify the fun function to return a scalar value by taking the norm (magnitude) of the vector output of Psi(x):
Psi = @(x) [1 x];
fun = @(x) norm(Psi(x))^2;
integral(fun, 0, 1)
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);
Please note that I used norm(Psi(x))^2 instead of abs(Psi(x)*Psi(x)') to calculate the squared norm, as it is more efficient and mathematically equivalent.
  댓글 수: 3
Jayant
Jayant 2023년 7월 5일
I misunderstood your previous statement. To resolve the error, you can modify the fun function to handle array-valued inputs by setting the 'ArrayValued' option to true in the integral function. This will inform MATLAB that the integrand function is array-valued. Below is the modified code:
Psi = @(x) [1 x];
fun = @(x) abs(Psi(x)*Psi(x)');
integral(fun, 0, 1, 'ArrayValued', true)
By including the 'ArrayValued', true option in the integral function, the error related to array-valued inputs should be resolved, and the integration should proceed correctly.
Please try the updated code & let me know if you face any further issues. Apologies for the confusion.
Julian Groß-Funk
Julian Groß-Funk 2023년 7월 5일
Great, thank you! I even randomly tried the ArrayValued-option before posting here, but got a wrong result due to another error that was still in my script back then.

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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by