MATLAB Answers

Potting sin() wave graph

16 views (last 30 days)
Christopher Clarke
Christopher Clarke on 4 Jun 2020
Answered: Ayush Laddha on 18 Jun 2020
Hi,
i am trying to plot three sign wave graph in one figure but the graphs are not coming out as intended.
wgn: Generate 60 random sample; Amplitude range -0.2 to 0.2
wgn2: wgn*3
A = 2 ;
f = 100;
n = (0:60);
fs = 2000;
x = A*sin(2*pi*n*(f/fs));
t = x/1000;
wgn = rand(1,61)*0.4-0.2;
wgn2 = wgn*3;
subplot (3,1,1)
plot(x,t,'m--d')
xlabel ('x')
ylabel ('time (ms)')
title ('Graph')
grid
subplot (3,1,2)
plot (wgn,t,'b-x')
xlabel ('wgn')
ylabel ('time (ms)')
grid
subplot(3,1,3)
plot((x+wgn2),t,'g-*')
xlabel ('x+wgn2')
ylabel('time (ms)')
grid

  1 Comment

Alberto Mora
Alberto Mora on 4 Jun 2020
Please explain better the problem

Sign in to comment.

Answers (1)

Ayush Laddha
Ayush Laddha on 18 Jun 2020
From your explanation of the question, I infer that you want to have all 3 graphs in a single plot. You can make use of the hold on and hold off commands to do so. You can also use the legend function to specify appropriate details for every line. You also mentioned in the question that you wanted to have 60 random samples however, I can see that your code generates 61 random samples. I have fixed the code for that too.
A = 2;
f = 100;
n = (1:60);
fs = 2000;
x = A*sin(2*pi*n*(f/fs));
t = x/1000;
wgn = rand(1,60)*0.4-0.2;
wgn2 = wgn*3;
plot(x,t,'m--d')
hold on
plot (wgn,t,'b-x')
plot((x+wgn2),t,'g-*')
grid on
title('Graph')
ylabel('time')
legend("x", "wgn", "x+wgn2", "Location", "northwest")
hold off
You can refer to the documentation of the functions for further info –

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!