Why does fplot think my function is not vectorized
Show older comments
Executing the following as a script in R2018a
a=ones(1,10);
b=a;
b0=1;
fun=@(x) myFourier(x,a,b,b0);
fplot(fun);
function f=myFourier(x,a,b,b0)
n=numel(a);
arg=x(:).*(1:n);
f=b0+sin(arg)*a(:)+cos(arg)*b(:);
f=reshape(f,size(x));
end
results successfully in a plot, but throws warnings (EDIT: and results in non-vectorized execution!!!)
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your
function to return an output with the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 234)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 193)
In fplot>vectorizeFplot (line 193)
In fplot (line 163)
In test (line 7)
But the input function is properly vectorized as is easily verified in simple tests,
>> fun(rand(1,5))
ans =
3.4371 2.5677 14.1698 2.4490 1.0828
>> fun(rand(5,1))
ans =
14.1745
8.3619
-0.2579
1.7024
1.5748
Why the warnings, then?
Accepted Answer
More Answers (3)
dpb
on 31 May 2019
1 vote
Because fplot is just looking at the source code, not the output and it doesn't recognize the reshape operation at the end--only that there are no "dot" operators in the evaluation from which it infers (usually correctly, but as you've shown not infallibly) that the function isn't "vectorized".
Whether there's any chance of being able to get this one right without way more parsing logic than would want to use for performance is, I'm guessing, pretty small. You can't just presume that if the user has a reshape call in the function that always works correctly, either.
14 Comments
Walter Roberson
on 31 May 2019
The code does not look at the source for the function to make this determination.
In older versions the code ran specific tests such as passing in a vector and checking that the size returned was the same. In current versions, unfortunately the tests were moved into a .p file so we do not know how it works.
dpb
on 31 May 2019
I hadn't gone back and looked at implemenation, granted, so I should have noted was a presumption rather than stating as fact...mea culpa.
From behavior and my thinking of how I would implement the warning, I figured "since everything in Matlab is a function" it could look at either the m-file or if compiled the reference to a dot library routine to determine if dot functions were used.
Without something like that, it seems that such cases as the above tests wouldn't trigger the warning is just an observed behavior for which I didn't otherwise have a ready explanation.
Walter Roberson
on 31 May 2019
The test below supports the notion that fplot does in fact examine the source code (but why????) .
aa = fun1(1);
bb = fun2(1);
aaa = fun1([1 2 3]);
bbb = fun2([1 2 3]);
aa-bb
aaa-bbb
ans =
4.44089209850063e-16
ans =
0 0 0
fun1 and fun2 produce the same values as each other for input [1 2 3]. However, fun1 and fun2 produce different values from each other for scalar input [1] . The value produced by fun1(1) is different than the first entry in fun1([1 2 3]) but the value produced by fun2(1) is the same as the first entry in fun2([1 2 3]) . Therefore as far as fplot is concerned, fun2 acts consistently and fun1 does not.
dpb
on 1 Jun 2019
'Tis interesting puzzle; one for Yair Altman and Undocumented MATLAB, maybe. :)
I'll just comment my presumption was that probably since the parser has to have "compiled" the function anyway, that it is looking at the internal code generated rather than actually reading the source file itself again...but again, that's all just presumption/guessing about how it seems to detect and be sensitive to whether one has used the dot functions or written code to produce the same result without.
Walter Roberson
on 1 Jun 2019
fplot with default range tests with [0] then [1 2 3] then [1] then [2] and then makes a decision about whether vectorization was correct or not.
Probe code attached.
dpb
on 1 Jun 2019
Good sticktoitniveness, Walter! :)
Makes more sense than my supposition altho is a lot of overhead for what seems little gain to end user for most part...since it doesn't quit but just gives a warning whatever cost there is in the multiple calls to the function to built the plot it goes ahead and does, anyway.
If it gave the warning and hints on how to fix and terminated would seem to make more sense from the efficiency standpoint if that's the intent but then that's somewhat rude, too.
Walter Roberson
on 1 Jun 2019
It is common for people to write expressions that do not give correct results when used with vectors. Often the expressions do not permit vectorized input, such as if it uses ^ instead of .^ . But it is also common for users to use / instead of ./ and in that case the expression can give an output that is the correct size but is wrong . Any difference between one-by-one calculations and vector calculations must be assumed to be an error in the algorithm as applied to vectors.
dpb
on 1 Jun 2019
Agreed, but the warning isn't implying the functional value is wrong; only that it could be calculated more efficiently if it didn't have to loop through the expression...so if that's what they're trying to catch they don't do a good job of explaining the possible problem in the resulting error/warning message.
dpb
on 2 Jun 2019
This could well be the basis for an enhancement/quality of implemenation improvement request.
Catalytic
on 7 Jun 2019
1 vote
Here's a workaround to trick fplot into doing the right thing.

Yair Altman
on 22 Jun 2019
As far as I can tell (never mind exactly how), internally a check is made whether the output of fun(1:3) is exactly equaln to the output of [fun(1),fun(2),fun(3)]. Even a tiny FP eps difference will cause the warning to be evoked. If you want to disable the error in run-time, run the following command:
warning off MATLAB:fplot:NotVectorized
1 Comment
Categories
Find more on Utilities for the Solver 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!