MATLAB Answers

what is the difference between hold and plot of two curves in the same figure ?

13 views (last 30 days)
diadalina
diadalina on 12 Dec 2017
Commented: diadalina on 12 Dec 2017
i want to plot cos(x) and sin(x) in the same figure, i have two ways :
i can write for example :
X=0:pi,
Y=cos(X);
Z=sin(X);
Plot(X,Y,X,Z)
or the second way :
X=0:pi;
Y=cos(X);
Z=sin(Z);
Plot(X,Y)
Hold on
Plot(X,Z)
what is the difference between these two ways ?

  0 Comments

Sign in to comment.

Accepted Answer

Honglei Chen
Honglei Chen on 12 Dec 2017
If that's all you need, for the practical purpose, there isn't much difference. However, the internal settings in the two figures are different. For example, say you have another vector
W = cos(2*X);
and you want to plot it using
plot(X,W)
In the hold case, the new line will be added to the figure while in the other case, the axes will be cleared and the new plot contains only the new line.
HTH

  3 Comments

diadalina
diadalina on 12 Dec 2017
thank Mr.Honglei for your answer, I did not understand correctly, look to what i had written :
figure(1)
x = 0 :0.1 :2*pi;
y1 = sin(x);
y2 = cos(x);
W = cos(2*x);
plot(x,W)
plot(x,y1,x,y2)
figure(2)
plot(x,y1)
hold on
plot(x,y2)
plot(x,W)
i can't see the difference ?
diadalina
diadalina on 12 Dec 2017
no, they have 3 curves,i want to know the difference when i use plot and when i use hold on ?

Sign in to comment.

More Answers (0)


Translated by