fplot gives me an error "function behaves unexpectedly on array inputs".

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

Show us exact equation....it may not be required to use sym. It can be achieved more simply.
I've attached the summation as a picture. x is the independent variable that I want to graph. b and d are arrays. i is 501. We don't want to let j = i because the ith index of d is zero and we don't want to divide by 0.
Thanks for the quick responses!
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?

Sign in to comment.

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

I see what you're saying, but unfortunately, this results in the error shown in the picture. Thank you so much for the info. Could you also explain more on why you used
nan(size(x))
"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))"
I had the function definition and function call in the same script file. If this was not what you meant, then I didn't understand your implementation... I now have the myfun function in a function file and the function call in a script. This is a new error message that I get.
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: Matrix dimensions must agree.
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.

Sign in to comment.

b = rand(1, 2000);
d = (-500:1:1499);
x=1:2000;
y=cumsum( (b./d.^2)./(x-1./d));
plot(x,y);

3 Comments

Thanks for the feedback. However, this isn't what I'm trying to achieve. I want to graph this summation with x as a variable like I would y = 2*x. With your code, x becomes a number that is being added. The summation part of my code is simply to iterate through the array. I have updated my original post to explain more about what I wanted to achieve. Thanks again.

Sign in to comment.

Products

Release

R2017a

Tags

Asked:

on 29 Oct 2018

Edited:

on 29 Oct 2018

Community Treasure Hunt

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

Start Hunting!