how to plot sine and cosine waves in one graph ?

used for academic use

댓글 수: 5

Please read the guide to tags and retag this question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags
Write a program to plot the function sin(x) between 0≤x≤4π.
Okay, I've done that. Now what?
How can I plot cos(ax+b)
syms x b
a = randn()
a = -1.0804
f = cos(a*x + b);
fsurf(f, [-10 10 -2*pi 2*pi])
xlabel('x'); ylabel('b')

댓글을 달려면 로그인하십시오.

 채택된 답변

Wayne King
Wayne King 2012년 9월 28일

2 개 추천

It sounds like the OP wants this in one graph (not subplotted)
t = 0:0.01:(2*pi);
x = cos(t);
y = sin(t);
plot(t,x,'k'); hold on;
plot(t,y,'r-.');
axis([0 2*pi -1.5 1.5])
legend('cos(t)','sin(t)','Location','NorthEast')

추가 답변 (4개)

Ravi Kumar
Ravi Kumar 2020년 1월 24일

1 개 추천

x=0:0.1:2*pi;
plot(x,sin(x))
hold on
plot(x,cos(x))
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2012년 9월 28일

0 개 추천

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

Thanks. What is subplot 121 and 122?
Stephen23
Stephen23 2017년 3월 2일
편집: Stephen23 2017년 3월 2일
@Lee Johnson: syntax not supported by MATLAB. Looks like MatPlotLib to me.
The MATLAB subplot documentation gives this syntax:
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).
subplot 121 is archaic, no longer documented, but still works. It is interpreted as subplot(1, 2, 1)

댓글을 달려면 로그인하십시오.

Abdullah Rajput
Abdullah Rajput 2020년 8월 19일

0 개 추천

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
Konstantin Kuzminov 2021년 3월 11일

0 개 추천

hold on not needed
x=0:0.1:2*pi;
plot(x,sin(x),"b",x,cos(x),"g");
"b" and "g" - color

댓글 수: 4

This is correct.
For old enough MATLAB (before R2017a) replace the "b" by 'b' and "g" by 'g'
Walter Roberson, thanks for the clarification about older versions! I did not know that.
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.)
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2012년 9월 28일

댓글:

2022년 4월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by