0% found this document useful (0 votes)
7 views6 pages

Lecture 2

The document provides a series of MATLAB plotting examples, demonstrating how to create various plots using different styles, colors, and markers. It includes instructions on using subplots to display multiple graphs in a single figure and explains the significance of different line styles and markers. Additionally, it covers basic loop structures for summing elements in an array.

Uploaded by

mumtahena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Lecture 2

The document provides a series of MATLAB plotting examples, demonstrating how to create various plots using different styles, colors, and markers. It includes instructions on using subplots to display multiple graphs in a single figure and explains the significance of different line styles and markers. Additionally, it covers basic loop structures for summing elements in an array.

Uploaded by

mumtahena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 2: Plotting

1.
clc;
clear all;
close all;
x = 1:0.1:10; % creating an array of elements from 1 to 10 with 0.1 increment
y = x.^2 + 20*x + 15*x.*sin(x); %computing a function of x
plot(x,y, 'Linewidth', 2) % plotting the figure

Task: significance of dots in y.


Change the plotting line:
plot(x, y, 'Color', 'r', 'LineStyle', '--', 'Marker', 'o')
 Predefined color strings: 'r' (red), 'g' (green), 'b' (blue), 'c' (cyan), 'm' (magenta), 'y'
(yellow), 'k' (black), and 'w' (white).
 '-' for solid line.
 '--' for dashed line.
 ':' for dotted line.
 '-.' for dash-dot line.
 'o' for circle.
 '*' for star.
 '+' for plus sign.
 'x' for cross.
 's' for square.
 Run this:
x = 0:0.1:10;
y = cos(x);
plot(x, y, 'b:', 'Marker', 's', 'MarkerSize', 8, 'MarkerEdgeColor', 'k', 'MarkerFaceColor',
'g');
xlabel('Time');
ylabel('Amplitude');
title('Cosine Wave');
grid on;
xlim([0, 10]); % Set x-axis limits
ylim([-1, 1]); % Set y-axis limits
2. Subplotting
Example 1:
clc;
clear all;
close all;
t = -10:0.01:10; %defining time variable
signal1 = t.^2; % signal1 = t^2
signal2 = t.^3+4*t.^2+5; % signal2 = t^2+2*t+sqrt(t)
subplot(2,1,1)
plot(t, signal1, 'Linewidth', 2) % it will plot the signal in the top portion of the window
subplot(212) %note that (2,1,2) or (212) both are correct
plot(t, signal2, 'Linewidth', 2)
Example 2:
clc;
close all;
clear all;
x=linspace(-2*pi, 2*pi);
subplot(2,2,1)
y1=sin(x);
plot(x,y1, 'r')
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
grid on;
subplot(2,2,2)
y2=sin(4*x);
plot(x,y2, 'b')
xlabel('time');
ylabel('Amplitude');
title('4*Sinusoidal signal');
grid on;
subplot(2,2,3)
y3=cos(x);
plot(x,y3)
xlabel('time');
ylabel('Amplitude');
title('Cosine signal');
grid on;
subplot(2,2,4)
y4=cos(4*x);
plot(x,y4)
xlabel('time');
ylabel('Amplitude');
title('4*Cosine signal');
grid on;
Example 3:
clc;
close all;
clear all;
t=0:0.02:4;
y=sin(pi*t);
plot(t,y, 'rd')
hold on %to draw multiple figures in a single figure
z=sin(pi*t+2*pi/3);
plot(t,z, 'b--')
hold on
m=sin(pi*t+4*pi/3);
plot(t,m, 'g')
hold off;
legend('0 phase', '120 phase', '240 phase')
xlabel('time');
ylabel('Amplitude');
title('plot of curves');
grid on;

3. Loop
clc;
clear all;
close all;
a = [1, 2, 3, 4, 5, 6]; % given array
n = length(a) % length gives the length of the vector
sum = 0; % initializing the sum
for i = 1:n %remember in matlab indices start from 1
sum = sum + a(i) %computes the sum
end % every for loop has an end!
7. Plotting:
clc;
close all;
clear all;
x=linspace(-2*pi, 2*pi);
subplot(2,2,1)
y1=sin(x);
plot(x,y1, 'r')
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
grid on;
subplot(2,2,2)
y2=sin(4*x);
plot(x,y2, 'b')
xlabel('time');
ylabel('Amplitude');
title('4*Sinusoidal signal');
grid on;
subplot(2,2,3)
y3=cos(x);
plot(x,y3)
xlabel('time');
ylabel('Amplitude');
title('Cosine signal');
grid on;
subplot(2,2,4)
y4=cos(4*x);
plot(x,y4)
xlabel('time');
ylabel('Amplitude');
title('4*Cosine signal');
grid on;
Example 3:
clc;
close all;
clear all;
t=0:0.02:4;
y=sin(pi*t);
plot(t,y, 'rd')
0
time
5-5
hold on %to draw multiple figures in a single figure
z=sin(pi*t+2*pi/3);
plot(t,z, 'b--')
hold on
m=sin(pi*t+4*pi/3);
plot(t,m, 'g')
hold off;
legend('0 phase', '120 phase', '240 phase')
xlabel('time');
ylabel('Amplitude');
title('plot of curves');
grid on;

You might also like