Why do I get this function warning (Function behaves unexpectedly on array inputs)

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 件のコメント

Adam
Adam 2017 年 12 月 19 日
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
Faris Alfaris 2017 年 12 月 19 日
Honestly I've spent few hours trying to figure out where I made a mistake but I couldn't find anything. I hope someone can explain. Thanks
Stephen Munkachy
Stephen Munkachy 2018 年 9 月 21 日
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 ')'.

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2017 年 12 月 19 日
function s = camdisp(t)
w = 2*(pi/5);
for i=1:numel(t)
if t(i)>=0 && t(i)<80
s(i) = ((10.*1.25)*((t(i)./80).^3))+((-15.*1.25).*((t(i)./80).^4))+((6.*1.25).*((t(i)./80).^5));
elseif t(i)>=80 && t(i)<170
s(i) = 1.25;
elseif t(i)>=170 && t(i)<250
s(i) = 1.25+((-10.*1.25).*(((t(i)-170)./80).^3))+((15.*1.25)*(((t(i)-170)./80).^4))+((-6.*1.25).*(((t(i)-170)./80).^5));
elseif t(i)>=250 && t(i)<=360
s(i) = 0;
end
end
end
Best wishes
Torsten.

3 件のコメント

I tried to do the same thing to the rest of the functions but it messed everything up. I only know basic matlab so I'm sorry. Can you please explain or help with that. Thanks
Second function
% velocity
function v = camvel(t)
w = 2*(pi/5);
if t>=0 && t<80
v = (w./((2.*pi.*80)./360)).* (((10.*1.25*3).*((t./80).^2))+((-15.*1.25*4).*((t./80).^3))+((6.*1.25.*5).*((t./80).^4)));
elseif t>=80 && t<170
v = 0;
elseif t>=170 && t<250
v = (w./((2.*pi.*80)./360)).* (((-10.*1.25.*3).*(((t-170)./80).^2))+((15.*1.25.*4).*(((t-170)./80).^3))+((-6.*1.25.*5).*(((t-170)./80).^4)));
elseif t>=250 && t<=360
v = 0;
end
end
Third function
% accelecartion
function a = camacc(t)
w = 2*(pi/5);
if t>=0 && t<80
a = ((w/((2*pi*80)/360))^2)* (((10*1.25*3*2)*((t/80)))+((-15*1.25*4*3)*((t/80)^2))+((6*1.25*5*4)*((t/80)^3)));
elseif t>=80 && t<170
a = 0;
elseif t>=170 && t<250
a = ((w/((2*pi*80)/360))^2)* (((-10*1.25*3*2)*(((t-170)/80)))+((15*1.25*4*3)*(((t-170)/80)^2))+((-6*1.25*5*4)*(((t-170)/80)^3)));
elseif t>=250 && t<=360
a = 0;
end
end
Fourth function
% jerk
function j = camjerk(t)
w = 2*(pi/5);
if t>=0 && t<=80
j = ((w/((2*pi*80)/360))^3)* (((10*1.25*3*2*1))+((-15*1.25*4*3*2)*((t/80)))+((6*1.25*5*4*3)*((t/80)^2)));
elseif t>=80 && t<=170
j = 0;
elseif t>=170 && t<=250
j = ((w/((2*pi*80)/360))^3)* (((-10*1.25*3*2*1))+((15*1.25*4*3*2)*(((t-170)/80)))+((-6*1.25*5*4*3)*(((t-170)/80)^2)));
elseif t>=250 && t<=360
j = 0;
end
end
Guillaume
Guillaume 2017 年 12 月 19 日
Note that w is never used in camdisp. As I explained in my answer, and Torsten implied without any explanation, you just need to loop over the input array using your original code. See the template I've given.
Faris Alfaris
Faris Alfaris 2017 年 12 月 19 日
Thanks a lot

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2017 年 12 月 19 日
編集済み: Guillaume 2017 年 12 月 19 日
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 件のコメント

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.

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by