MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course
2 Purpose of the lecture This lecture contains a short introduction to the MATLAB –For further details see other sources Later we will apply Matlab to a simple simulation exercise of a communication system In that exercise we study, at the same time as learning MATLAB, different aspects of communication simulations
3 MATLAB MATLAB is an interactive, matrix-based system for scientific and engineering numeric computation and visualization You can solve complex numerical problems in a fraction of the time required with a programming language such as Fortran or C. The name MATLAB is derived from MATrix LABoratory
4 MATLAB … MATLAB is an expression language –the expressions you type are interpreted and evaluated MATLAB statements are usually of the form –variable = expression, or simply –expression
5 MATLAB … MATLAB contains several build-in functions –Written either using C, or as a form of m-files You can create your own functions (m-files) –Most of work is writing functions that are used to execute simulations You can obtain functions (m-files) written by others BUT, AS ALWAYS, –Remember to validate all the functions (if not already validated by others) –Even build-in functions may contain errors, or are not usable in your problem since every numeric algorithm has its limitations remember to check the function manual
6 MATLAB … MATLAB has its core with some basic functions (which can be used to build everything) In addition, it has TOOLBOXes that include build- in functions to support different areas or multiple of areas TOOLBOXes ease and fasten simulations in their areas –Naturally, one has to learn how to use functions in TOOLBOXes
7 Toolboxes
8
9 Information sources Matlab primer – –Guide to basic properties, –very helpful if read and executed simultaneously Mathworks home page – –Information of all Matlab related products –Helps –User created m-files from several areas
10 Matrices MATLAB can efficiently use matrices and vectors –MATLAB handles vector vector and matrix vector products faster than for loops (if sizes are not too large) –Many books and papers where you can find algorithms use matrix notation –So, learn to use matrices and vectors efficiently! –A hint: always write vectors in the same way Either as a column or row vector Column form is the most common
11 M-files You can call m-files in the command line or inside m-files –You can use other m-files in your simulations A good practice is to make small, general-usable m-files (if possible) and then apply them in your master simulation m-file –The generality allows you to apply the same m-file in many places Build-in m-files are general M-files are called such since they have file type “.m”
12 M-files M-files can be either script files or function files A script file consists of a sequence of normal MATLAB statements When the m-file is called, the statements are executed Variables in a script file are global and will change the value of variables of the same name in the environment of the current MATLAB session
13 Script file example We want to calculate logarithm (base 10) of 10 In command line it is log10(10) (since log is ln) Put this as a variable a=log10(10) Create m-file which calculates logarithm on 100, and puts the result in a variable a, name this as testA.m Execute first a=log10(10) and then testA.m
14 Value of variable a changes since script files have global variables If the result is not a named variable Matlab gives ans
15 M-files … Function files provide extensibility to MATLAB You can create new functions specific to your problem which will then have the same status as other MATLAB functions Variables in a function file are by default local –A variable can, however, be declared global (see help global) –Locality means that a value of variable on the command line is not changed even if a function file contains the same variable
16 M-files … Inputs to function files may be variables Remember to comment your files –you may even create a help part that explains what the function does –Commenting helps you and others to remember what the different lines do and what for the different variables are
17 Function file example We create a function file that –computes a sinusoidal function with a frequency f over an interval 0 - T –results values of a sinusoidal and the interval –And plots the signal In addition –We make a help –and comment the file
18 function [y,t]=testB(f,T,n); %[y,t]=testB(f,T,n) plots a sinusoidal with a frequency f [Hz] %over an interval 0 - T [s] and results values of the function y and %corresponding time instants. n+1 is the number of time instants, %the default being 128. % %by Michael Mfilewriter, 2006 %check if n is given or not %if not set it as default if nargin<3, n=127; else n=n-1; %makes sure that we have n+1 time instants end
19 %create time scale with n steps t=0:T/n:T; t=t(:); % t(:) makes a column vector %compute values of sinusoideal y=sin(2*pi*f*t); %plot it with t as a x-axis and y as a y-axis plot(t,y) xlabel('time [s]') %creates text for x-label ylabel('sinusoidal') %creates text for y-label
20 [y,t]=testB(1,10,1024); results
21 Help If m-files have a help it is called as help m-file E.g., if we call help of testB.m as help testB It results [y,t]=testB(f,T,n) plots a sinusoidal with a frequency f [Hz] over an interval 0 - T [s] and results values of the function y and corresponding time instants. n+1 is the number of time instants, the default being 128. by Michael Mfilewriter, 2006 That is, help results everything commented (by %) in the code before first line without %