preliminary 0
TRANSCRIPT
HACETTEPE UNIVERSITY
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
ELE 326 TELECOMMUNICATIONS THEORY LABORATORY I
EXPERIMENT 0 – INTRODUCTION TO MATLAB AND SIMULINK
PRELIMINARY WORK
Note: Upload your preliminary work before the March 10, 2014, 23:59:59. Use the template supplied to you for your
preliminary work report.
Purpose: Introduction to MATLAB and Simulink.
A) Questions
1. Calculate 2 3
8 2 7 5
3 4 5 8 4
using MATLAB.
2. Calculate 2 3
8 2 7 5
3 4 5 8 4
and assign it to variable x.
3. Calculate the square of x and assign it again to variable x. Observe that the variable x has been changed.
4. Create a variable y which is equal to 10.9842 and add it to x, assign the result to another variable named z.
5. Clear the workspace. Create a vector
1 5 7 8 9 6 5 2 1 0 x . Calculate the vector y which has the
elements 2 2 22 2 2 2 2 2 2
1 5 7 8 9 6 5 2 1 0
.
6. Create a vector z which has the elements 0 0.01 0.02 0.98 0.99 1 .
a. What is the size of z.
b. Create another vector t which has the elements 1 0.99 0.01 0 0.01 0.99 1 . Using
only the vector z.
c. Plot the vector sin t .
7. Clear the workspace. Create the vectors 1 5 7 8 9 6 5 2 1 0j j j x1 ,
2 5 7 8 9 6 3 21 4 7j x2 , and calculate x = x1 + x2. Calculate both the transpose and conjugate
transpose of x.
a. Calculate the frobenius norm of x using the function "norm", name the answer as norm_x_1.
b. Calculate the frobenius norm of x, 2
k
k
x using a for loop, name the answer as norm_x_2.
c. Calculate the frobenius norm of x, 2
k
k
x without using a for loop, name the answer as norm_x_3.
d. Multiply the vector x with its conjugate transpose such that the result is a scalar. Take the square root of the answer,
name it norm_x_4. Observe norm_x_1, norm_x_2, norm_x_3, norm_x_4, are they the same?
8. Create 2 random vectors x and h which have dimensions 8 1 and 5 1 , respectively. Convolve these two vectors using the
built in function “conv” and name the output as y. Observe the length of the resulting vector y.
9. Clear the workspace. Create the matrices shown;
2 3 4
1 2 4 0 4 5
4 3 2 , 2 2 4
5 4 3 1 4 3
1 5 2
A B
Try to multiplicate these matrices as A B , you should get an error because in a multiplication of matrices the number
of columns of the first matrix must be equal to the number of rows of the second matrix. You can do B A or T
A B .
10.
a. Write any MATLAB code to create a matrix M that is a 5 x 5 array of zeros with a 1 in the middle of it.
b. Suppose q is a vector. Write a MATLAB code to change all the 1’s in q to be 2’s, i.e., if q is [0 1 4 3 1] then the
result of the code will be [0 2 4 3 2].
11.
3 4 5 2 3 6
12 95 23 0 29 39
57 64 72 41 8 91
47 28 31 82 84 37
28 40 39 45 64 69
A
a. Write code that will find all numbers in A larger than 5, and puts them in a vector.
b. Write code that will find all numbers that differ more than 50 from the mean, and puts them in a vector.
12.
Pascal's triangle determines the coefficients which arise in binomial expansions: (x + y)n, where n = 0, 1, 2, . . . The top
of this triangle, for n = 0, 1, 2, 3 is shown below.
1
1 1
1 2 1
1 3 3 1
Write a function pasctri(n) that generates the Pascal triangle up to the level n. Remark: Two-dimensional arrays in
MATLAB must have the same number of columns in each row. In order to avoid error messages, add a certain number of
zero entries to the right of last nonzero entry in all the rows but last of the triangle:
t = pasctri(3)
1 0 0 0
1 1 0 0
1 2 1 0
1 3 3 1
t
13. Calculate the determinant, eigenvalues and corresponding eigenvectors of the matrix A below.
1 3 7 22 25
129 86 12 141 22,
141 90 24 121 28
58 37 11 47 9
A b
Also solve the linear system Ax b .
14. Generate a matrix A of size M N
with uniformly distributed random numbers between 0-1. Write a function that compares
the elements of matrix A with 0.2 and creates another matrix B according to this comparison. If the ijth element of A is smaller
than or equal to 0.2 then the ijth element of B should be -1 and if the ijth element of A is larger than 0.2 then the ijth element of
B should be 1 (Here i represents the ith row, j represents the jth coloumn). The input of the function should be the matrix A and
the output should be the matrix B. As an example;
0.1 0.5 0.2 1 1 1
0.7 0.3 0.7 1 1 1
0.2 0.1 0.9 1 1 1
A B
15. Generate the m file below;
clear all;
close all;
clc;
f_signal = 1e3;
duration = 2e-3;
A = 1;
f_sampling = 16e3;
no_samples = 0:round(duration*f_sampling)-1;
time_axis = 0:1/f_sampling:duration-1/f_sampling;
sine_wave = A*sin(2*pi*f_signal*time_axis);
FFT_sine_wave = fftshift(abs(fft(sine_wave)));
frequency_axis = (-f_sampling/2):(f_sampling/length(FFT_sine_wave))...
:(f_sampling/2 - f_sampling/length(FFT_sine_wave));
figure;plot(time_axis,sine_wave)
figure;stem(no_samples,sine_wave)
figure;stem(frequency_axis,FFT_sine_wave)
* Be aware that latest versions of MATLAB is case sensitive.
** There are lots of other ways to create a square wave.
*** The three dots (...) at the end of the 15th line allows MATLAB to understand that the 16th line is the extension of 15th
line, e.g.,
x = 250*...
10;
means
x = 250*10;
When you run the m file, observe that,
It generates a sinusoidal signal of duration "duration", with frequency "f_signal", and amplitude "A".
Takes the Fourier transform of the sinusoidal wave.
Shows the sinusoidal signal and its sampled version with sampling frequency "f_sampling".
Shows the magnitude spectra of the sinusoidal signal.
After observing this m file, you are expected to know how to generate signals in MATLAB, how to generate their
magnitude spectra, and how to display them.
Try to generate a square wave with fundamental frequency 1000Hz, duration 0.002, and amplitude 1 (the same
specifications as the sinusoidal above). Take its FFT and display both the time signal and magnitude spectra in
MATLAB.
16. Start Simulink. Open a new model. Set the simulation time to 0.005 (or 5e-3, they are the same). Take a Signal generator from
the Sources tab under the Simulink tab, and a Scope from the Sinks tab which is again under the Simulink tab. Connect the
Signal generator to the Scope, double click the signal generator and set the waveform to be a Sine wave and frequency to be
1kHz, then click OK to exit the properties of the Signal generator. Double click the Scope and run the simulation, observe the
wave on the scope, observe its period and frequency. Repeat this for a square wave signal.
17. Take a Quantizer from the Discontinuities tab and connect it between the Signal generator and Scope. Set the Quantizer
interval to 0.5 and run the simulation, then do this for a Quantizer interval of 1.0. Observe the changes on the quantized
output.
18. Remove the Quantizer and add a summer and a Constant block as shown in the figure below;
Set the constant value to 3, and observe the output.
19. Consider the transfer function;
2
1
2 0.7 1s s
Build a Simulink block of the above system where the input signal is a unit step function. Display the output from 0 to 10
time units.
Signal
Generator
Scope
Signal
Generator
ScopeQuantizer
Signal
Generator
Scope
3 Constant
20. Suppose that we are dealing with the circuit below;
Where the initial conditions are iL(0-)=0, and vc(0-)=0.5V. This circuit gives the differential equation;
2
024 3 3
c c
c
d v dvv u t
dt dt
Try to simulate this circuit using the differential equation above with Simulink and show the output vc for a unit step input
signal.
B) References
1. www.mathworks.com.
2. Houcque D, “Introduction to MATLAB for Engineering Students”, version 2.1, 2005.