how to plot sine and cosine waves in one graph ?

used for academic use

5 件のコメント

Walter Roberson
Walter Roberson 2012 年 9 月 28 日
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
Manikandan annamalai 2021 年 2 月 18 日
Write a program to plot the function sin(x) between 0≤x≤4π.
Walter Roberson
Walter Roberson 2021 年 2 月 18 日
Okay, I've done that. Now what?
Krishan Guiya
Krishan Guiya 2022 年 4 月 6 日
How can I plot cos(ax+b)
Walter Roberson
Walter Roberson 2022 年 4 月 7 日
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')

2 件のコメント

AJAY VISHNU
AJAY VISHNU 2020 年 2 月 28 日
thanks
roger xiang
roger xiang 2020 年 9 月 11 日
thanks

サインインしてコメントする。

その他の回答 (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 件のコメント

Lee Johnson
Lee Johnson 2017 年 3 月 2 日
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).
Walter Roberson
Walter Roberson 2021 年 2 月 1 日
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 件のコメント

Walter Roberson
Walter Roberson 2021 年 3 月 12 日
This is correct.
For old enough MATLAB (before R2017a) replace the "b" by 'b' and "g" by 'g'
Konstantin Kuzminov
Konstantin Kuzminov 2021 年 3 月 12 日
Walter Roberson, thanks for the clarification about older versions! I did not know that.
Walter Roberson
Walter Roberson 2021 年 3 月 12 日
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
Konstantin Kuzminov 2021 年 3 月 13 日
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 Exchange2-D and 3-D Plots についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by