Answer To Experiments.
Answer To Experiments.
I n t r o d u c t i o n t o M AT L A B
Questions:
and
q=3 cos ( xy ) +2 y 2 .
y=0.05 x +2.01
three functions p,q, and z=p+q. Verify the correctness of your plot from the figures below.
Write your code on the space provided.
Note:
When a dot precedes an operator, as using .* for multiplication, it implies that each element
in the vector (matrix) results from applying that operator to corresponding elements in the
first and second vectors (matrices). For example, dot multiplication of two m x 1 vectors
results in an m x 1 vector where each element is the product of the corresponding elements
in the first and second vectors. This type of multiplication requires that the vectors
(matrices) must be of the same size and is called pointwise, rather than vector or matrix,
multiplication.
Write your code here:
clf;
x=[0:0.01:5];
y=0.05.*x+2.01;
p=3*sin(x.^2)+2*cos(y.^3);
q=3*cos(x.*y)+2*y.^2;
z=p+q;
figure(1),plot(p);
figure(2),plot(q);
figure(3),plot(z);
4
3
2
1
0
-1
-2
-3
-4
-5
100
200
300
400
500
600
13
12
11
10
9
8
7
6
5
100
200
300
400
500
600
12
10
100
200
300
400
500
600
increments.
Answer: (Sketch or attach the resulting figure below)
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
50
100
150
200
250
300
The polynomial is
x4+4x3+9x2-16x-52
4
3
2
1
0
-1
-2
-3
-4
-4
-3
-2
-1
b) Plot the polynomial function for the range of x from -5 to 5. You may wish to use
help to look up the functions polyval, and linspace to obtain a plot similar to the
one shown below .
1400
1200
1000
800
600
400
200
0
-200
-5
-4
-3
-2
-1
f ( t )=3 e j 2 t + /4
Plot the real and imaginary parts as a function of time from 0 to 3 seconds in 0.01second increments. Also plot the magnitude and phase of f as a function of time. All
four plots must be contained in one graphics window. You may wish to look up the
functions subplot, title, and plot to see how to generate more than one plot in the
graphics window at the same time. You will also need to look up the functions abs
and angle. Verify the correctness of your own plot to be similar to the one shown
below.
Re(f(t))
8
6
-2
-2
-4
-4
-6
-8
Im(f(t))
-6
0
0.5
1.5
2.5
Magnitude of f(t)
10
-8
0.5
2.5
2.5
Phase of f(t)
1.5
6
0
4
-2
2
0
0.5
1.5
2.5
-4
0.5
1.5
Create an m-file for the program above and then execute. Draw the figure generated.
Amplitude
0.8
0.6
0.4
0.2
0
-10
The
The
The
The
The
-5
purpose
purpose
purpose
purpose
purpose
5
Time index n
of
of
of
of
of
10
15
20
STEP 2 Modify Program EXP2_1 to generate a delayed unit sample sequence sd[n] with a delay of
11 samples by completing the code below:
% Generation of a Delayed Unit Sample Sequence
clf;
n = -10:20;
sd = [zeros(1,21) 1 zeros(1,9)];
%or
sd =[zeros(1,10) zeros(1,11) 1 zeros(1,9)];
stem(n,sd);
xlabel('Time index n'); ylabel('Amplitude');
title('Delayed Unit Sample Sequence');
axis([-10 20 0 1.2]);
STEP 3
Modify Program Exp2_1 to generate a unit step sequence u[n] similar to the plot shown.
Write your code below.
Unit Step Sequence
Amplitude
0.8
0.6
0.4
0.2
0
-10
-5
5
Time index n
10
15
20
STEP 4
Modify the Program
Exp2_1 to generate a unit step
sequence ua[n] with an advance
of 7 samples by completing the
code below:
axis([-10 20 0 1.2]);
% Program Exp2_2
% Generation of a complex exponential sequence
clf;
c = -(1/12)+(pi/6)*i;
K = 2;
n = 0:40;
x = K*exp(c*n);
subplot(2,1,1);
stem(n,real(x));
xlabel('Time index n');ylabel('Amplitude');
title('Real part');
subplot(2,1,2);
stem(n,imag(x));
xlabel('Time index n');ylabel('Amplitude');
title('Imaginary part');
STEP 5
Answer the
following questions:
Which of the figures below is generated? Both were generated
The parameter controlling the rate of growth or decay of this sequence is the real
part of c.
The parameter controlling the amplitude of this sequence is the K parameter.
The result of changing the parameter c to (1/12)+(pi/6)*i is the envelope of the signal will
grow with n
The purpose of the operator real is to extract the real component of a complex number or
matrix.
The purpose of the operator imag is to extract the imaginary component of the complex
number or matrix.
The purpose of the command subplot is to plot more than one graph in the same Matlab
figure.
STEP 6
The following code can be used to generate a real-valued exponential
sequence:
% Program Exp2_3
% Generation of a real exponential sequence
clf;
n = 0:35; a = 1.2; K = 0.2;
x = K*a.^n;
stem(n,x);
xlabel('Time index n');ylabel('Amplitude');
Execute the given code and observe the figure. Which of the following figures is
produced? Figure(a)
The parameter controlling the rate of growth or decay of this sequence is the a parameter
The parameter controlling the amplitude of this sequence is the K parameter
The
difference
between
the
arithmetic
operators
^
and
.^
is
_________________________________.
^ raises a square matrix to a power using matrix multiplication.
.^ raises the
elements of a matrix or vector to a power (pointwise operation)
STEP 7 Draw the sequence generated by running Program Exp2_3 with the parameter a changed
to 0.9 and the parameter K changed to 20.
20
18
16
Amplitude
14
12
10
8
6
4
2
0
10
15
20
Time index n
25
30
35
title('Sinusoidal Sequence');
xlabel('Time index n');
ylabel('Amplitude');
axis;
STEP 8
2
1.5
1
Amplitude
0.5
0
-0.5
-1
-1.5
-2
10
15
20
25
Time index n
30
35
40
EXPERIMENT 3
Other Sig na l Wa veform
Generation
% Generation of a sinusoidal sequence
n = 0:40;
f = 0.1;
phase = 0;
A = 1.5;
arg = 2*pi*f*n + phase;
x = A*cos(arg);
clf;
stem(n,x);
axis([0 40 -2 2]);
grid;
title('Sinusoidal Sequence');
MATLAB CODES
%Generationofthesquarewave
n=0:30;
f=0.1;
phase=0;
duty=60;
A=2.5;
arg=2*pi*f*n+phase;
x=A*square(arg,duty);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([03033]);
grid;
title('SquareWaveSequence');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;
%Generationofthesawtoothwave1
n=0:50;
f=0.05;
phase=0;
peak=1;
A=2.0;
arg=2*pi*f*n+phase;
x=A*sawtooth(arg,peak);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([05022]);
grid;
title('SawtoothWave1');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;
%Generationofthesawtoothwave2
n=0:50;
f=0.05;
phase=0;
peak=0.5;
A=2.0;
arg=2*pi*f*n+phase;
x=A*sawtooth(arg,peak);
clf;%Clearoldgraph
stem(n,x);%Plotthegeneratedsequence
axis([05022]);
grid;
title('SawtoothWave2');
xlabel('Timeindexn');
ylabel('Amplitude');
axis;