What is Matlab(Matrix Lab)? MATLAB is a high-performance language for technical computing It integrates computation, visualization, and programming environment MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not require dimensioning
What is Matlab? It has powerful built-in routines that enable a very wide variety of computations. It also has easy to use graphics commands that make the visualization of results immediately available. Specific applications are collected in packages referred to as toolbox. There are toolboxes for signal processing, symbolic computation, control theory, simulation, optimiza-tion, and several other fields of applied science and engineering.
The Graphical Interface of the Matlab workspace The Command Window The Command History The Workspace The Current Directory The Help Browser The Start button
Creating Variables in MATLAB MATLAB variables are created with an assignment statement: The expression can be: ▫manual entry ▫built-in functions ▫user-defined functions
Try Using the MATLAB as a calculator Type in the command prompt: Repeat the above command but place a semicolon ; at the end of the command Type ans in the command prompt (ans is the default variable for your answer) Try putting the answer in a variable other than ans: Type this:
Controlling the appearance of floating point number MATLAB does numerical calculations in double precision, which is 15 digits. The command format controls how the results of computations are displayed
Managing the Workspace The contents of the workspace persist between the executions of separate commands Therefore, it is possible for the results of one problem to have an effect on the next one. To avoid this possibility, it is a good idea to issue a clear command at the start of each new inde- pendent calculation.
Managing the Workspace In order to display a list of the variables currently in the memory, type Multiple Statements per line, Type this:
Getting help from the MATLAB documentation MATLAB offers a very powerful help which you can get any help for any command in the MATLAB Type in the command prompt: >>doc format
Matrices in MATLAB Matrices are fundamental to MATLAB So far, we have been dealing with scalars(1x1 matrix) We will deal with matrices of size mxn We can also deal with row vectors of size 1xn We can also deal with column vectors of size mx1
Matrices in MATLAB Creating a row vector : Creating a column vector: Type: >> z=v+w Type: >> z=w, v=w (automatic resize) Type: >> z=v+w’ (transpose operator)
Matrices in MATLAB To access individual elements of the vector: v(1) is the first element of vector v, v(2) its second element, and so forth. to access blocks of elements of the vector, we use MATLAB's colon notation (:)[don’t use for loops] ▫Type: To access all elements from the third through the last elements of a vector: ▫Type:
Entering a matrix in MATLAB To enter a matrix A, such as: Type : To view a particular element of the matrix: ▫Type: Matrix Indexing: Type: >>
Colon Operator The colon operator will prove very useful and understanding how it works is the key to efficient and convenient usage of MATLAB For example, suppose we want to enter a vector x consisting of points (0; 0.1; 0.2; 0.3; ….. ; 5). We can use the command:(This is a row vector of 51 elements)
Linear Spacing Generates a row vector y of n points linearly spaced between and including a and b: Try this: This divides the interval [0; ] into 100 equal subintervals, then creating a vector of 101 elements
Matrix or Vector Dimension To get the Dimensions of a matrix or a vector, we use the command size: To get a length of a vector: ▫>> length(A)
Operations on Vectors Note: to get the square of each element we use: X.^2 NOT X^2 Where the dot operator(.) means element by element operation
Vector Multiplication What is the difference? ▫Z = X*Y (the inner dimension of the 2 vectors must agree: one row and the other column) ▫Z = X.*Y vector! (X and Y dimensions must agree) Define 2 vectors and multiply them with and without the dot operator
Plotting 2 vectors Note: minimizing the step size will lead to very smooth curve for the relationship and vice versa. Simply we use the plot order to plot the vectors against each other. ▫Plot (indep_var, depen_var)
Plotting 2 vectors Exercise: plot the average temperature of the months of the year to their order according to the following table:
Plotting 2 vectors Solution: ▫>> Month=[1:1:12]; ▫>> Temperature=[ 17 19 22 24 27 30 34 35 31 26 23 20]; ▫>> plot(Month,Temperature) The result is a continuous curve(Interpolation), but do we need that really in this application? Use stem(Month,Temperature) for discrete sample plotting.