Error integrating when the function handle is an integral: "Limits of integration must be scalars"
82 views (last 30 days)
Show older comments
Jonathan Ken Miyahara Coello
on 1 Aug 2022
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
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)
myint(z)=int(myfun,0,z)
i=myint(2)
Accepted Answer
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
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.
More Answers (1)
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
0 Comments
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!