Plot function along one direction

I want to plot a multivariate function (here as an example I take the max function, but it really could be any multivariate function f I want) along a direction. I have the following codes
F = @(x) max([3,4]+x.*[5,6]);
fplot(F)
But I get the following warning
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.
How can I resolve this? It doesn't make difference whether I put a dot before * or not, I still get this warning. Also I would need the function F defined since I want to do stuff with it like finding a maximum of F, i.e. the maximum of f in a specified line. Also I want my code to work for any multivariate function f no matter how many variables it has, so I can't write explicitly F=@(x) f(3+x*5,4+x*6), i.e. f need to take in one vector, not a fixed amount of scalars. Thanks a lot!

 Accepted Answer

"Specify a function of the form y = f(x). The function must accept a vector input argument and return a vector output argument of the same size."
In practice with a single variable the function is passed a row vector of values. In the above case you could change your code to
F = @(x) max([3;4] + [5;6] * x);
for R2016b or later. For earlier you would use
F = @(x) max( bsxfun(@plus, [3;4], [5;6] * x) );
"Also I want my code to work for any multivariate function f no matter how many variables it has"
That is a problem since fplot can only handle functions of one variable.
What I suspect you are asking is how to hold some variables constant and plot along the remaining direction. For that you should have a look at http://www.mathworks.com/help/matlab/math/parameterizing-functions.html. To avoid the warnings from fplot you should might need to combine that with arrayfun() .

3 Comments

Indeed now if I plot
F = @(x) max([3;4] + [5;6] * x);
there is no warning. However if I do it with a custom function f, i.e.
f = @(x) x(1)+x(2)^2;
F = @(u) f([3;4]+[5;6]*u);
fplot(F)
then I get the warning again. I don't understand why, is there a fix for this custom f?
Also, you probably misunderstood my remark "Also I want my code to work for any multivariate function f no matter how many variables it has". I simply mean that I need f to take in a vector and not a fixed number of scalars, since I need a program such that given arbitrary f and vectors x0 and y, it defines F = @(u) f(x0+y*u) and I want this program to work no matter how many veriable the f I put in has. So f should be like f([3,4]) and not f(3,4), since the latter would restrict me to 2 variables.
Thanks a lot!
As I quoted, fplot passes a row vector of values to be evaluated at. You (now) build two rows out of the original row vector and you hand that entire 2 x N matrix to f() but f expects a vector with two elements.
Probably the easiest way to handle this for functions like F that you are expecting will be given a scalar, is to write a small anonymous function that arrayfun() the individual values into the call to f() and then to fplot the wrapper. This is the easiest, but the better way is to vectorize your f to extract rows or columns instead of individual elements. x(2,:).^2 instead of x(2)^2 for example
Ah I see, thank you very much!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!