Error integrating when the function handle is an integral: "Limits of integration must be scalars"

82 views (last 30 days)
Hi dear community,
I have a problem with integral(). Below I provide an example but in words, I am trying to integrate and my function handle (inside integral) is an integral on its own. The simplest example is the following
iden = @(x) x;
myfun = @(y) integral(iden,0,y);
myint = @(z) integral(myfun,0,z);
myint(2)
It appears that this creates an error of the type "Limits of integration must be double or single scalars" when trying to evaluate myfun in myint. However, I don't see why there is a problem if y,z will be evaluated as scalars. Of course, this can be solved if I integrate myfun1 analytically but in the actual problem I have, this is not possible. Could you please provide some guidance on how can I solve this problem or if there is other function more suitable for this task? Thank you so much!
  1 Comment
Dyuman Joshi
Dyuman Joshi on 1 Aug 2022
You can use symobilc functions
syms iden(x) myfun(y) myint(z)
iden(x)=x;
myfun(y)=int(iden,0,y)
myfun(y) = 
myint(z)=int(myfun,0,z)
myint(z) = 
i=myint(2)
i = 

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 1 Aug 2022
"However, I don't see why there is a problem if y,z will be evaluated as scalars"
But they will not be. integral always passes in a vector of values by default . You then use the vector as bounds, but integral can only accept scalar bounds.
Use the 'arrayvalued' option for the outer integral .
  3 Comments
Walter Roberson
Walter Roberson on 1 Aug 2022
the integral call in myint would call myfun passing in a scalar because of the option. myfun would receive the scalar as the upper bound and would call iden with a vector of values and would adjust the content of the vector as appropriate to meet local integration tolerances.
On paper you probably would mentally do symbolic integration instead of mentally calling iden a bunch of times and mentally working out where it needed to be called more to get numeric convergence.
Jonathan Ken Miyahara Coello
Hi Walter and Dyuman thank you so much for your help. Now my big code is working using the 'arrayvalued' option. Best regards!

Sign in to comment.

More Answers (1)

RAMIREDDY
RAMIREDDY on 1 Aug 2022
syms iden(x) myfun(y) myint(z)
iden(x)=x;
myfun(y)=int(iden,0,y)
myfun(y) =
myint(z)=int(myfun,0,z)
myint(z) =
i=myint(2)
i = 5/2

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!