Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB Computational Probability and Statistics CIS 2033 Section 003 Djordje Gligorijevic, Temple University, Spring 2015.

Similar presentations


Presentation on theme: "Introduction to MATLAB Computational Probability and Statistics CIS 2033 Section 003 Djordje Gligorijevic, Temple University, Spring 2015."— Presentation transcript:

1 Introduction to MATLAB Computational Probability and Statistics CIS 2033 Section 003 Djordje Gligorijevic, Temple University, Spring 2015

2 About MATLAB  MATLAB (MATrix LABoratory) is a high level language made for:  Numerical Computation (Technical computing)  Visualization  Application Development  Everything is a matrix – easy to do Linear Algebra and other mathematical fields that use Linear Algebra (Statistics, Fourier Analysis, Optimization, Filtering,…)  Useful tool for mathematical analysis and simulation Djordje Gligorijevic, Temple University, Spring 2015

3 MATLAB Software Access  All Temple University in-campus computers have student-version MATLAB installed  Temple University also provides a student license for every student, just go to MATLAB Site Licensed Software page: https://computerservices.temple.edu/matlab-site-licensed- software https://computerservices.temple.edu/matlab-site-licensed- software Djordje Gligorijevic, Temple University, Spring 2015

4 Matlab Layouts Layouts of versions MATLAB 1.0 to MATLAB 8.2 Djordje Gligorijevic, Temple University, Spring 2015 Current folder view Editor Workspace Command History Command Window

5 Matlab Layouts – new movement Layouts of versions MATLAB Djordje Gligorijevic, Temple University, Spring 2015 Current Window Workspace Editor/Variables View Command Window

6 MATLAB competition  Due to its cost, MATLAB has a hard time finding a dominance in the industry.  Other languages provide a “bigger bang for the buck” thanks to the active communities that maintain widely used libraries.  An examples of such languages that have became a “hot topic” recently are:  Python (NumPy, SciPy, Matplotlib, SciKit Learn,…)  R  However, MATLAB is still completely dominating when it comes to performance. Djordje Gligorijevic, Temple University, Spring 2015

7 MATLAB Variables There are a few predefined variables in MATLAB: Djordje Gligorijevic, Temple University, Spring 2015 ans Variable which has been assigned a value of most recent expression, which hasn’t had assigned a value of its output to a specific variable pi Number pi = eps Smallest difference between two numbers MATALB can see inf Stands for an infinite value NaN Undefined value (Not-a-Number), Ex. 0/0 i Imaginary one

8 Matlab Structures Numeric Types: Matlab provides wide range of numerical and floating point types such are double, int (8,16,32 and 64 bits), uint ( 8,16,32,64 bits). Some functions on numeric types: Djordje Gligorijevic, Temple University, Spring 2015

9 Elementary math functions Djordje Gligorijevic, Temple University, Spring 2015 sqrt(x) Square root sin(x) Sinus x (in radians) exp(x) Exponential function - e x cos(x) Cosinus s abs(x) Absolute value tan(x) Tangens x log(x) Natural logarithm - ln(x), log e (x) cot(x) Cotangens x log10(x) Logarithm with basis 10 round(x) Round to an integer factorial(x) Factoriel x – x! ceil(x) Round to higher value fix(x) Round to lower value rem(y,x) Remainder of x/y division sign(x) Signum function

10 Matlab Structures, Contd. Matrices and Vectors To enter a matrix: >> A = [2, 5, 3; 6, 4, 1] >> B = [1:1.5:6; ] >> for i=1:4 for j=1:3 C(i,j)=i*j; end >> D =[]; D=[D;5]; D=[D;6;7] >> E = zeros(4, 5) Djordje Gligorijevic, Temple University, Spring 2015

11 Matlab Structures, Contd. Matrices and Vectors As every variable in MATLAB can be a matrix, we can do basic operations on them: Addition: >> C = A + B Subtraction: >> D = A – B Multiplication: >> E = A * B (Matrix multiplication) >> E = A.* B (Element wise multiplication, A and B same size) Division: Left Division and Right Division >> F = A. / B (Element wise division) >> F = A / B = A*inv(B) (A * inverse of B) >> F = A. \ B (Element wise division) >> F = A \ B=inv(A)*B (inverse of A * B) Djordje Gligorijevic, Temple University, Spring 2015

12 Elementary matrix functions Djordje Gligorijevic, Temple University, Spring 2015 max Maximum of elements + Matrix addition min Minimum of elements - Matrix substraction sum Sum of elements * Matrix multiplication prod Product of elements / Matrix division median Median (Middle value) \ Left matrix division mean Mean (Agerage) ^ Power std Standard deviation ' Transpose any Is there any zero invInverse of the matrix Never use it!!!! all Are all elements zero sortSort columns (or rows)

13 Generating basic matrices Matrix with ZEROS: >> A = zeros(m, n) Matrix with ONES: >> B = ones(m, n) IDENTITY Matrix: >> I = eye(m, n) RANDOM Matrix: >> I = rand(m, n) MAGIC SQUARE Matrix: >> I = magic(n) m  Rows n  Columns zeros, ones, eye, rand, magic  Matlab functions Djordje Gligorijevic, Temple University, Spring 2015

14 Random Number Generators  Rand(m,n): matrix with each entry ~ U(0,1)  Randn(m,n): standard normal distribution  Mvnrnd(Mu, Sigma): Multivariate normal random numbers Djordje Gligorijevic, Temple University, Spring 2015

15 Array indexing, obtaining information  Size(A): return [m n]  Length(A): length of a vector  Length(A) = max(size(A))  B = A(2:4,3:5)  B is the subset of A from row 2 to row 4, column 3 to column 5  C = A(2)  C is a scalar with value of second index in matrix A  Matlab matrix indicies go iteratively top-down, column by column  D = A(:, [1,15,20])  Select all rows but a few columns  A(:, 2)=[]  Delete second column Djordje Gligorijevic, Temple University, Spring 2015

16 Characters and Strings Djordje Gligorijevic, Temple University, Spring 2015  A sequence of characters enclosed in single quotes  assign a string to a variable  A = ‘Alice’  use two single quotes within the definition if the text includes a single quote  Concatenaton  With square brackets as concatenate numeric arrays  A = [‘Alice’, ’followed’, ’the’, ’rabbit.’]  Convert numeric values to strings  num2str  int2str

17 Save/Load Data  Save fname  Save all workspace data into fname.mat  Save fname x y z  Save(fname): when fname is a variable  Save large files  save('fname.mat','fname','-v7.3');  Load fname  Load fname x y  No error in data  You can run simulation intermittently  Save/load data between runs Djordje Gligorijevic, Temple University, Spring 2015

18 Programming in MATLAB - Scripts  A file with a.m extension containing  Multiple sequential lines of MATLAB commands  Function calls  To run a script  Save a file in the current folder and type its name at the command line  Run scripts from the Editor by pressing the Run button  Comments  To describe the code  Add comments whenever you write code  For comments add % symbol first Djordje Gligorijevic, Temple University, Spring 2015

19 Programming in MATLAB - Functions  Equivalent to subroutines or methods in other programming languages  To call a function, enclose its input arguments in parentheses  If there are multiple input arguments, separate them with commas  Return output from a function by assigning it to a variable  When there are multiple output arguments, enclose them in square brackets  To call a function that does not require any inputs and does not return any outputs, type only the function name Djordje Gligorijevic, Temple University, Spring 2015

20 Programming in MATLAB – Control Flow  MATLAB allows control flow operations:  Conditional statements, loops, branching Djordje Gligorijevic, Temple University, Spring 2015

21 Plotting in MATLAB – Basic 2-D Figure Plot  Plot(X, Y):  Plots vector Y versus vector X  Hold: next plot action on the same figure  Title(‘title text here’)  Xlabel(‘…’), ylabel(‘…’)  Axis([XMIN XMAX YMIN YMAX])  Or use xlim or ylim  Legend(‘…’)  Grid  Example demo Djordje Gligorijevic, Temple University, Spring 2015

22 Plotting in MATLAB – Basic 3-D Figure Plot  x=[0:10]; y=[0:10]; z=x ’ *y;  mesh(x,y,z); figure; surf(x,y,z); Djordje Gligorijevic, Temple University, Spring 2015

23 MATLAB – Help  The most important thing to remmember about MATLAB – it has great Help and Documentation  Just press Shift + F1 to search for needed function  Or select a function and press F1 Djordje Gligorijevic, Temple University, Spring 2015

24 End of MATLAB introduction Any Questions? Djordje Gligorijevic, Temple University, Spring 2015


Download ppt "Introduction to MATLAB Computational Probability and Statistics CIS 2033 Section 003 Djordje Gligorijevic, Temple University, Spring 2015."

Similar presentations


Ads by Google