+ All Categories
Home > Documents > Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course –...

Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course –...

Date post: 27-Dec-2015
Category:
Upload: muriel-turner
View: 218 times
Download: 1 times
Share this document with a friend
Popular Tags:
28
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3
Transcript
Page 1: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Introduction to MATLABSession 1Prepared By: Dina El Kholy

Ahmed DalalStatistics Course – Biomedical Department -year 3

Page 2: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Section Contents

Page 3: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 4: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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.

Page 5: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

The Graphical Interface of the Matlab workspace•The Command Window• The Command History• The Workspace• The Current Directory• The Help Browser• The Start button

Page 6: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Let’s take a look at the Matlab Interface

Page 7: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Creating Variables in MATLAB

•MATLAB variables are created with an assignment statement:

•The expression can be:▫manual entry▫built-in functions▫user-defined functions

Page 8: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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:

Page 9: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 10: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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.

Page 11: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Managing the Workspace

•In order to display a list of the variables currently in the memory, type

•Multiple Statements per line, Type this:

Page 12: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 13: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 14: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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)

Page 15: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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:

Page 16: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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: >>

Page 17: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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)

Page 18: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 19: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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)

Page 20: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

List of Elementary Matrices

Page 21: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 22: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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

Page 23: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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)

Page 24: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Plotting 2 vectors

•Exercise: plot the average temperature of the months of the year to their order according to the following table:

Page 25: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

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.

Page 26: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Page 27: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

References

•INTRODUCTION TO MATLAB FOR ENGINEERING STUDENTS, David Houcque

Northwestern University

Page 28: Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.

Any Questions?


Recommended