plotting two different function in the same figure with ezplot

45 views (last 30 days)
i am trying to plot the functions of y1(t)=t^2 and y2(t)=t^2-16 in the same range of (-8,8) with ezplot in the same figure window,i mean i would like to see these two curve simultaneously in the same figure window. how can i do this with ezplot?i have tried much ways but it gives me the solution in the different figure windows.i'd also like to do this with symbolic expressions.is it possible?
thank you.

Accepted Answer

Paulo Silva
Paulo Silva on 23 Jul 2011
ezplot supports only one expression for x and another for y so you must use two ezplots like this:
y1='t^2'
y2='t^2-16'
fg=figure
ax=axes;
ez1=ezplot(y1,[-8,8])
hold on
ez2=ezplot(y2,[-8,8])
legend('y1=t^2','y2=t^2-16')
set(ez1,'color',[1 0 0])
title(ax,['y1=t^2' ' y2=t^2-16'])
ezplot also doesn't support symbolic expressions so you must convert them to strings.
symbolic example:
syms t
y1=t^2
y2=t^2-16
fg=figure
ax=axes;
ez1=ezplot(char(y1),[-8,8])
hold on
ez2=ezplot(char(y2),[-8,8])
legend('y1=t^2','y2=t^2-16')
set(ez1,'color',[1 0 0])
title(ax,['y1=t^2' ' y2=t^2-16'])
The char function converts symbolic expressions to strings, you can also use the vectorize for the same purpose but it's different, vectorize adds dots before expressions like ^ and * so that operations like this t^2 where t is a vector won't fail because of the size of the vector, vectorize turn the symbolic expression t^2 into t.^2 , operations with the dot before the operation symbol are done element by element.
  2 Comments
murat artan
murat artan on 23 Jul 2011
Yes,this is very critical:"hold on",now i remember.I think i never forget this anymore.
Thank you so much for your help.
Christopher Creutzig
Christopher Creutzig on 20 Nov 2017
Just a quick note for everyone finding this old reply now: MATLAB now has fplot as a more “modern,” hopefully easier to use, alternative to ezplot.

Sign in to comment.

More Answers (1)

Abhinav Raman
Abhinav Raman on 21 Aug 2018
f1='sin(x)' f2='cos(x)' ez1=ezplot(f1,[0,2*3.14]) hold on ez2=explot(f2,[0,2*3.14]) title(ax,['sin(x)','cos(x)'])

Community Treasure Hunt

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

Start Hunting!