fplot gives me an error "function behaves unexpectedly on array inputs".
Show older comments
I've looked at the different answers that others have given for the same question, but I can't seem to implement the correct solution. Below is my code.
b = rand(1, 2000);
d = (-500:1:1499);
syms x;
summation = 0;
for i = 1:2000
if(i == 501)
continue
else
summation2 = summation2 + (b(i)./d(i).^2)./(x-1./d(i));
end
end
fplot(@(x) summation2);
I basically have 2 1x2000 arrays. One is random, the other contains values from -500 to 2000. My for loop is an attempt to create a summation.
The error I get is
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 226)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 185)
In fplot>vectorizeFplot (line 185)
In fplot (line 156)
In main (line 25)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update: sym
output type is not supported. Set 'UniformOutput' to false.
Does anyone know how to fix this? Any insight on this would be greatly appreciated!
EDIT: I have attached a picture of the summation and equation I want to graph.
5 Comments
madhan ravi
on 29 Oct 2018
What’s x?
KSSV
on 29 Oct 2018
Show us exact equation....it may not be required to use sym. It can be achieved more simply.
Andrew Soong
on 29 Oct 2018
Edited: Andrew Soong
on 29 Oct 2018
KSSV
on 29 Oct 2018
Have a look on symsum
There error message states clearly "...sym output type is not supported."
Nowhere have you given any reason why you need to use symbolic variables. I can't see any reason why you need to use symbolic variables. These symbolic variables are causing you problems, and are not required. Why not just write simpler, more efficient numeric code using standard MATLAB?
Answers (2)
"Doese anyone know how to fix this?"
The fplot documentation describes that its function input (usually the first input) "...must accept a vector input argument and return a vector output argument of the same size." Your code accepts one input argument x and then totally ignores it, the output of your function is totally independent of x because you have already calculated some symbolic formula beforehand. If you want to use fplot then you will need to write a function that accepts x as a numeric vector and returns a numeric vector of the same size, just as the documentation states. This is easy to do, and will be very easy to test too.
"Any insight on this would be greatly appreciated!"
Using symbolic variables is a red-herring. You do not need to slow down your code with symbolic variables. You only need to write a simple numeric function, that is all.
Something like this (I have no idea if the calculation is correct, you have to check this yourself):
function y = myfun(x,b,d)
y = nan(size(x));
for k = 1:numel(x)
y(k) = nansum((b./d.^2)./(x(k)-1./d));
end
end
And then call it like this:
>> b = rand(1,2000);
>> d = -500:1499;
>> fplot(@(x)myfun(x,b,d),[1,2])
Probably you can vectorized it quite easily too:
5 Comments
madhan ravi
on 29 Oct 2018
Doesn’t work @ stephen
Andrew Soong
on 29 Oct 2018
"this results in the error shown in the picture"
Read the error message again (I highlighted the most important part):

Where did you define myfun ?
"Could you also explain more on why you used nan(size(x))"
Andrew Soong
on 29 Oct 2018
Make sure that you use nansum or sum with the 'omitnan' option, because your calculation sometimes produces NaN values, and this will cause problems with fplot:
y(k) = sum((b./d.^2)./(x(k)-1./d),'omitnan');
^^^^^^^^^^ you need this, or NANSUM.

madhan ravi
on 29 Oct 2018
b = rand(1, 2000);
d = (-500:1:1499);
x=1:2000;
y=cumsum( (b./d.^2)./(x-1./d));
plot(x,y);
3 Comments
madhan ravi
on 29 Oct 2018
The series tends to be divergent
Andrew Soong
on 29 Oct 2018
madhan ravi
on 29 Oct 2018
is it a homework?
Categories
Find more on Calculus 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!