Lecture 2
Lecture 2
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
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;