17
Lecture 2 Basics of MATLAB Eng. Mohamed Awni Electrical & Computer Engineering Dept.

Matlab HTI summer training course_Lecture2

Embed Size (px)

Citation preview

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