Plot Multiple Functions in a single call to fplot

30 views (last 30 days)
I want to draw 2 concentric circles using fplot. Suppose that radius of 1st circle is 1 and 2nd circle is 0.8 then

For Loop Method

f = figure;
ax =axes(f);
y = [1 0.8];
for i=1:2
xt = @(x) y(i)*cosd(x);
yt = @(x) y(i)*sind(x);
fplot(ax,xt,yt,[0 360])

Direct Method

Now instead of applying for loop, add two functions in xt and two in yt as shown below
xt = {@(x) cosd(x);@(x) 0.8*cosd(x)};
yt = {@(x) sind(x);@(x) 0.8*sind(x)};
Now if i use fplot
fplot(xt,yt,[0 360])
Following error occured
This error will not occur for fplot(xt,yt{1},[0 360])
I opened fplot function and i think this line is causing problem (line 69-72)
if ~isempty(args) && (isa(args{1},'function_handle') || isa(args{1}, 'sym'))
fn{end+1} = args(1);
args(1) = [];
Note that before these lines the first argument (i.e. xt) is passed to fn and removed from args which means that now args(1) means the 2nd argument (i.e. yt)
Now the problem is that args{1} is a cell containing two functions.
returns logic 0 because
ans =
2×1 cell array
{ @(x)sind(x)}
instead of args{1} there must be condition which check every element of args{1}{1,2,...}
isa(args{1}{1},'function_handle') % or 2 (equal to length of args{1}
Now i suppose that somehow this condition is true and we are on next line
fn{end+1} = args(1);
there should be an iscell condition here and if it is true the code should be
fn{end+1} = args{1};
Now if i make only these 2 corrections fplot works perfectly because at line 166
hObj = vectorizeFplot(cax,fn,limits,extraOpts,args);
has no problem with it
Long story short,
  1. how to add changes in fplot code (as it is a static workspace and i dont know if it is allowed to edit them)
  2. is this issue resolved in future release

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jun 2020
  • is this issue resolved in future release
fplot() operates in different modes:
  • if an x or y input is a function handle, then it must return an output that is the same size as the (vector) input. . It cannot return multiple values for each input, because a mismatch on sizes is how fplot() tests to see whether the function is vectorized to see whether it must call the function once per value (and if it is not vectorized, then it must return one value for each input.)
  • if x is numeric, then y must be scalar symbolic or y must be a function handle that returns one value for each input. One line will be drawn for each value of x
  • y numeric is not expected by fplot, as fplot expects trailing numeric values to be the plot range
  • if x and y are both present and are both symbolic, then they must both be scalar.
  • if x is present without y, then x can be a symbolic vector, and one line will be drawn for each element of x.
  • cell arrays are not permited for x or y.
I think it would be a reasonable enhancement if Mathworks were to permit x and y to both be symbolic and compatible size, but it is not implemented at this time.
I do not think this is likely for the function handle case to be changed, because it is quite common that people ask to fplot function handles that are not vectorized, and that must be automatically detected. However, I could imagine that potentially Mathworks could add an 'arrayvalued' option.
  • how to add changes in fplot code (as it is a static workspace and i dont know if it is allowed to edit them)
That could possibly be a bit tricky, as the changes would need to fit in with the automatic detection of discontinuities, and that detection only expects one function at a time (if I recall correctly.)
You could make a local copy of fplot.m and of private/countvars.m from toolbox/matlab/graphics/function/ and edit away.
I would suggest to you, however, that it would make more sense to write a small wrapper routine that accepted cell arrays of function handles and ran fplot() as many times as needed with "hold on" set.
  1 Comment
Mehmed Saad
Mehmed Saad on 5 Jun 2020
Thank you for your advice as always, I am grateful to hear your viewpoint.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 4 Jun 2020
Long story short , it's not possible to use fplot(...) in a single call.





Community Treasure Hunt

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

Start Hunting!

Translated by