Upload
mohamed-awni
View
450
Download
6
Tags:
Embed Size (px)
Citation preview
Lecture 2
Basics of MATLAB
Eng. Mohamed Awni
Electrical & Computer Engineering
Dept.
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
3
Useful Commands
• There are some commands used most by Matlab users are
>>help functionname
>>lookfor keyword
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
5
Matlab data type
Class
Variable
Size Value
Multidimensional Array [full or sparse]
logical
ab
char
{ } cell struct numeric
int8, uint8, int16, unit16, int32, unit32, int64, unit64
single double
Matlab data type
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, -
8
Examples
9
The use of the comma (,) and the semicolon (;)
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.
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.
• Using the assignment operator =
Variables assignment
Variable_name = A numerical value, or a computable expression
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
Elementary math built-in functions
Examples
Calculate : y= 2*sqrt(a^2-c^2), where a=5 and c= 4
Exercises
Homework