Why do I get this function warning (Function behaves unexpectedly on array inputs)
Show older comments
I want to plot 4 functions on the same graph. The plots works fine but I get this warning. How can I fix it please?
figure(1)
subplot(2,1,1)
fplot(@(x) camdisp(x),[0 360])
title('S plot')
ylabel('S in inches')
xlabel('\theta in degrees')
subplot(2,1,2)
fplot(@(x) camvel(x),[0 360])
title('V plot')
ylabel('V in inches/sec')
xlabel('\theta in degrees')
function s = camdisp(t)
w = 2*(pi/5);
if t>=0 && t<80
s = ((10.*1.25)*((t./80).^3))+((-15.*1.25).*((t./80).^4))+((6.*1.25).*((t./80).^5));
elseif t>=80 && t<170
s = 1.25;
elseif t>=170 && t<250
s = 1.25+((-10.*1.25).*(((t-170)./80).^3))+((15.*1.25)*(((t-170)./80).^4))+((-6.*1.25).*(((t-170)./80).^5));
elseif t>=250 && t<=360
s = 0;
end
end
Then I get this message
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 232)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 191)
In fplot>vectorizeFplot (line 191)
In fplot (line 161)
In Project (line 11)
3 Comments
Adam
on 19 Dec 2017
That's unusually poor English for a warning in Matlab! I read it through 3 or 4 times and it still made no sense in English, nevermind interpreting it for code!
Faris Alfaris
on 19 Dec 2017
Stephen Munkachy
on 21 Sep 2018
Looks like you missed a period in front of a multiplication symbol in the first if statement. It's right after the first closed parenthesis ')'.
Accepted Answer
More Answers (1)
fplot is trying to call your function with a vector input, e.g.
camdisp(0:360)
Your function errors in that case, hence fplot is forced to loop over each element of the vector and called your function for each element. That degrades performance.
You can rewrite your function so that it works for vector inputs. One such way would be to simply loop over the input vector
function s = camdisp(tarray)
s = zeros(size(tarray));
for i = 1:numel(tarray)
t = tarray;
if ...
s(i) = ... %your normal code goes here
elseif ...
s(i) = ...
...
end
end
end
That would give no performance benefit since you just moved the for loop out of fplot to put it in your function. At least, it would shut up fplot.
Another way would be of course to vectorise the function. A vectorised version could be:
function s = camdisp(t)
s = zeros(size(t));
group = discretize(t, [0 80 170 250 360]);
s(group == 1) = ((10.*1.25)*((t(group == 1)./80).^3))+((-15.*1.25).*((t(group == 1)./80).^4))+((6.*1.25).*((t(group == 1)./80).^5));
s(group == 2) = 1.25;
s(group == 3) = 1.25+((-10.*1.25).*(((t(group == 3)-170)./80).^3))+((15.*1.25)*(((t(group == 3)-170)./80).^4))+((-6.*1.25).*(((t(group == 3)-170)./80).^5));
s(group == 4) = 0; %that line not even needed since s is already 0 in that case
end
1 Comment
Guillaume
on 19 Dec 2017
Something I forgot to say:
fplot(@(x) camdisp(x), [0 360]);
can be written more simply as:
fplot(@camdisp, [0 360]);
In addition to the two different implementations, you could also change your fplot calls to:
fplot(@(x) arrayfun(@camdisp, x), [0 360]);
which would get rid of the warning and would not require you to change any of your function code. The downside is that it'll be even slower. arrayfun is another way to implement a loop.
Categories
Find more on Surface and Mesh Plots 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!