Transcript
Page 1: Matlab HTI summer training course_Lecture2

Lecture 2

Basics of MATLAB

Eng. Mohamed Awni

Electrical & Computer Engineering

Dept.

Page 2: Matlab HTI summer training course_Lecture2

Agenda

2

Useful Commands used frequently.

Matlab data type?

Arithmetic Operator & Their Precedence

Examples

The Use of (,) and (;) in matlab

Variables

Elementary math built-in functions.

Examples & Exercise & Homework

Page 3: Matlab HTI summer training course_Lecture2

3

Useful Commands

β€’ There are some commands used most by Matlab users are

>>help functionname

>>lookfor keyword

Page 4: Matlab HTI summer training course_Lecture2

Useful Commands

>>clc clears the Command Window.

>>clear removes all variables from the workspace.

>>Who

>>Whos

lists all of the variables in your matlab

list the variables and describes their matrix size

Page 5: Matlab HTI summer training course_Lecture2

5

Matlab data type

Class

Variable

Size Value

Page 6: Matlab HTI summer training course_Lecture2

Multidimensional Array [full or sparse]

logical

ab

char

{ } cell struct numeric

int8, uint8, int16, unit16, int32, unit32, int64, unit64

single double

Matlab data type

Page 7: Matlab HTI summer training course_Lecture2

7

Arithmetic Operator & Their Precedence

Operations Operators Examples

Addition Subtraction Multiplication Right Division Left Division Exponentiation

+ βˆ’ * / \ ^

>> πŸ“ + πŸ‘ >> πŸ“ βˆ’ πŸ‘ >> πŸ“ βˆ— πŸ‘ >> πŸ“/πŸ‘ >> πŸ“\πŸ‘ = πŸ‘/πŸ“ >> πŸ“^πŸ‘ (means πŸ“πŸ‘ = πŸπŸπŸ“)

Precedence Order Operators

1 2 3 4

Parentheses ( ). For nested parentheses, the innermost are executed first. Exponentiation, ^ Multiplication, *; Division, /,\ Addition, +; Subtraction, -

Page 8: Matlab HTI summer training course_Lecture2

8

Examples

Page 9: Matlab HTI summer training course_Lecture2

9

The use of the comma (,) and the semicolon (;)

Page 10: Matlab HTI summer training course_Lecture2

10

Variables

β€’ No need for types. i.e.,

int a; double b; float c;

>>x=6; >>y=7;

β€’ All variables are created with double precision unless specified and they are matrices.

β€’ When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of memory storage.

β€’ If the variable already exists, MATLAB changes its contents.

Page 11: Matlab HTI summer training course_Lecture2

How to choose a variable name β€’ Use meaningful names for variables

β€’ MATLAB variable names

– must begin with a letter

– can contain any combination of letters, numbers and underscore (_)

- The variables name should not contain spaces.

-The maximum number of characters of a variables name in MATLAB R2009a is 63 characters.

β€’ MATLAB is case sensitive: β€œname”, β€œName” and β€œNAME” are considered different variables.

β€’ Never use a variable with the same name as a MATLAB command.

Page 12: Matlab HTI summer training course_Lecture2

β€’ Using the assignment operator =

Variables assignment

Variable_name = A numerical value, or a computable expression

Page 13: Matlab HTI summer training course_Lecture2

Elementary math built-in functions

Matlab Functions

β€’ MATLAB program that performs a sequence of operations specified in a text file called an m-file

β€’ A function should have a name

β€’ accepts one or more MATLAB variables (called arguments) between parenthesis as inputs

β€’ operates on them in some way.

β€’ returns one or more MATLAB variables as

outputs

Page 14: Matlab HTI summer training course_Lecture2

Elementary math built-in functions

Page 15: Matlab HTI summer training course_Lecture2

Examples

Calculate : y= 2*sqrt(a^2-c^2), where a=5 and c= 4

Page 16: Matlab HTI summer training course_Lecture2

Exercises

Page 17: Matlab HTI summer training course_Lecture2

Homework