how to plot sine and cosine waves in one graph ?
Afficher commentaires plus anciens
used for academic use
5 commentaires
Walter Roberson
le 28 Sep 2012
Please read the guide to tags and retag this question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags
Manikandan annamalai
le 18 Fév 2021
Write a program to plot the function sin(x) between 0≤x≤4π.
Walter Roberson
le 18 Fév 2021
Okay, I've done that. Now what?
Krishan Guiya
le 6 Avr 2022
How can I plot cos(ax+b)
syms x b
a = randn()
f = cos(a*x + b);
fsurf(f, [-10 10 -2*pi 2*pi])
xlabel('x'); ylabel('b')
Réponse acceptée
Plus de réponses (4)
Ravi Kumar
le 24 Jan 2020
x=0:0.1:2*pi;
plot(x,sin(x))
hold on
plot(x,cos(x))
Sabarinathan Vadivelu
le 28 Sep 2012
a = 10;
t = 0 : 0.01 : 10;
A = a*sin(t);
subplot(121),plot(t,A);
B = a*cos(t);
subplot(122),plot(t,B);
3 commentaires
Lee Johnson
le 2 Mar 2017
Thanks. What is subplot 121 and 122?
@Lee Johnson: syntax not supported by MATLAB. Looks like MatPlotLib to me.
subplot(m,n,p)
and explains what m, n, and p are. There is of course no point in simply copying this info here when you can read in the documentation (see link).
Walter Roberson
le 1 Fév 2021
subplot 121 is archaic, no longer documented, but still works. It is interpreted as subplot(1, 2, 1)
Abdullah Rajput
le 19 Août 2020
close all; clear all; clc;
t=[0:0.5:180]
x=sind(t);
plot(t,x),hold on
y=cosd(t);
plot(t,y)
legend('Sine wave','Cos wave')
Konstantin Kuzminov
le 11 Mar 2021
hold on not needed
x=0:0.1:2*pi;
plot(x,sin(x),"b",x,cos(x),"g");
"b" and "g" - color
4 commentaires
Walter Roberson
le 12 Mar 2021
This is correct.
For old enough MATLAB (before R2017a) replace the "b" by 'b' and "g" by 'g'
Konstantin Kuzminov
le 12 Mar 2021
Walter Roberson, thanks for the clarification about older versions! I did not know that.
Walter Roberson
le 12 Mar 2021
I would not normally have mentioned a backwards compatibility like that now that we are 4 years on, but I suspect this question is getting read by people who are using old versions of MATLAB. (We are getting a rush of people using MATLAB from 2013 and 2015.)
Konstantin Kuzminov
le 13 Mar 2021
You are absolutely right. This is very useful information, because not everyone has the latest versions, they are not required for simple tasks. Correct syntax - 50% success.
Catégories
En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
