Issues with robustness of integral function? - integrating a constant

I am wondering why:
>> integral(@(t)5, 0, 1)
does not work unless I use:
>> integral(@(t)5, 0, 1, 'ArrayValued', true)
or
>> integral(@(t)5 + 0*t, 0, 1)
Since the function is certainly not (inherently) array-valued, I prefer the latter solution, however I am actually using this with the "matlabFunction" function to turn a symbolic expression into a function handle, and thus it does not work there. Is there something fundamental I should be doing differently?
Closer to how I am doing things:
>> syms t
>> f = 5 + 0*t
>> F = matlabFunction(f, 'Vars', t)
>> want = integral(F, 0, 1)
The reason this is an issue is because I am symbolically differentiating a Hamiltonian to get the equations of motion, and depending on the Hamiltonian, one of the equations of motion could turn out to be constant. I want my code to work cleanly for any input.
Thank you

 채택된 답변

John D'Errico
John D'Errico 2017년 3월 5일
편집: John D'Errico 2017년 3월 5일
This has nothing to do with "robustness".
integral expects that if you pass in a vector of inputs to the function, then it can get a vector output from your function. For every input, there MUST be a corresponding output.
So, what happens if you pass in the function
fun = @(t) 5;
fun(1:3)
ans =
5
Clearly, fun is not a function that obeys the rules.
Change fun so that it returns an output for every input, like any of these:
fun = @(t) 5 + 0*t;
fun = @(t) 5*ones(size(t));
fun = @(t) repmat(5,size(t));
fun = @(t) 5 + t - t;
and of course it must now work. It will see the desired constant function.
If you change a flag like this:
integral(@(t)5, 0, 1,'arrayvalued',true)
then integral only ever passes in a scalar argument. It sets up an internal loop with all the elements that it will use. This essentially forces integral to assume the function is not vectorized, although that was not why the 'arrayvalued' option was provided. I wish they had given an option called perhaps 'isvectorized', as that would be the easily understood option.
As I said though, this is not a robustness issue. Merely one of following the rules that integral insists upon. Software cannot read your mind, doing only exactly as it is programmed to do. If you will be arbitrarily changing your function, and wish integral to somehow know when your function is or is not vectorized, then the way is to set the 'arrayvalued' flag.

댓글 수: 3

Thanks for the description! ... If you care to offer more insight: do you know of any reasons why constant functions only produce a single output for multiple inputs?
Um, I'd accept that as a valid question. :) A question of implementation, I guess?
But thinking abut it, I can see why. For example, suppose I created the functions:
fun1 = @(x) dot(x,ones(size(x)));
fun2 = @(x) 15;
Yes, I know the former is a rather non-obvious way to compute the sum of the vector x, for vector input x of any length.
fun1(1:5)
ans =
15
fun2(1:5)
ans =
15
Should MATLAB be smart enough to know that when calling fun1 above, the result should be the scalar value 15, yet when calling fun2 the same way, that some people might want the result expanded into a vector of the same size and shape as the input?
All that MATLAB does is evaluate what it sees in a function handle. The point is, IF MATLAB were to start being too creative about when it decides not to treat a scalar as a scalar, then people will start seeing very difficult to debug problems.
So I can accept that you want MATLAB to be smart here, but smart can be dangerous. Better to be as dumb as possible in some ways.
I do wish there was an 'isvectorized" option for integral though. That is something I have provided for my tools when it makes sense.
Ok, I suppose I was just expecting that the most natural way for a constant function to behave was by acting as multiplication-by-a-constant. Here it just is the constant, for any input. I suppose that would be the most robust (as in behaving in the most general fashion under varied situation) definition. Thanks again!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2017년 3월 5일

댓글:

2017년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by