37
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Embed Size (px)

Citation preview

Page 1: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

ECE 1304Introduction to Electrical and

Computer Engineering

Section 1.1

Introduction to MATLAB

Page 2: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

MATLAB

• MATLAB stands for “Matrix Laboratory.”• MATLAB was developed for applications

involving matrices, linear algebra and numerical analysis.

• MATLAB has built-in capacity for complex numbers.

Page 3: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Commands entered here are executed immediately.

This is called an Interactive Session.

The commands are recorded in the Command History.

Page 4: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Numbers and mathematical operations may be entered directly as if in a calculator.

The operation returns a value.

Page 5: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

MATLAB assigns the value to a built-in variable named ans.

Page 6: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Scalar Arithmetic OperatorsSymbol Operation MATLAB Form

^ exponentiation: ab a^b

* multiplication: ab a*b

/ right division: a/b = a b

a/b

\ left division: a\b = b a

a\b

+ addition: a + b a+b

- subtraction: a – b a-b

Page 7: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The value assigned to the variable named ans may be used in subsequent calculations.

Page 8: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The user may enter the variable name and use the assignment operator = to assign a value.

In this case the variable is named “r.”

Page 9: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Variable Names

• Variable names must begin with a letter and may contain up to 32 characters.

• The characters may include letters, numbers and the under score character.

• MATLAB is case sensitive. speed and Speed are two different variable names.

• Chose variable names that make sense for the problem you are solving. This makes programs easier to read.

Page 10: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The Assignment Operator

• The Assignment Operator = behaves differently than the mathematical equality symbol.

• MATLAB evaluates the expression on the right hand side of the = operator and then assigns that value to the variable on the left hand side of the operator.

Page 11: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The Assignment Operator

• The expression x + 2 = y is meaningless in MATLAB.

• The expression y = x + 2 is appropriate.• Provided x has been assigned a value.

MATLAB will find the value of x + 2 then assign that value to the variable y.

• If x does not have a value MATLAB will display an error message.

Page 12: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Numerical Precision

• MATLAB uses high precision for all of its calculations (14 decimal decimal places?).

• By default, results are displayed using 4 decimal places.

• This format may be changed if the user wishes.

• Numbers are displayed using exponential notation using e to represent a power of ten.

• 5.3164 102 is displayed as 5.3164e+2

Page 13: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

If the name of a variable is entered, MATLAB will display the value of the variable.

Page 14: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

After a variable has been assigned a value, that variable may be used in subsequent calculations.

The result is assigned to a new variable named s.

Page 15: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

MATLAB evaluates this expression as “the new value of s is assigned the old value of s plus 160.”

Page 16: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Expressions are evaluated according to a built-in order of precedence for the mathematical operators.

Page 17: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Order of PrecedencePrecedence Operation

First Parentheses ( ), evaluated starting with the innermost pair.

Second Unary plus (+), unary minus (-)

Third Exponentiation (^), evaluated from left to right.

Fourth Multiplication (*) and division ( / ) with equal precedence, evaluated from left to right

Fifth Addition (+) and subtraction (-) with equal precedence, evaluated from left to right.

Page 18: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB
Page 19: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The who operator causes MATLAB to display the names of the variables currently stored in the program.

The values of the variables are not displayed.

Page 20: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The whos operator causes MATLAB to display the names of the variables, the vector size of each variable, the number of bytes required by each variable, the class of variable (double precision in this case) an if the variable is complex.

Page 21: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The clear operator removes all variables from memory.

The memory space assigned to those variables is reclaimed.

Variables may be cleared by name:

clear r

clear s

Page 22: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The clc operator clears the command window.

The clc operator does not clears the memory.

Page 23: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The clc only changes the appearance of the Command Window.

Page 24: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

All variables in MATLAB are vectors (arrays) by default.

A scalar is a 11 vector (array).

The colon operator : is used to generate the elements of a vector (array).

The operator is evaluated as

first element : increment : final value

Page 25: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Think of the vector (array) as a list of numbers all assigned to the variable name t.

This vector has one row and nine columns.

Page 26: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The whos operator indicates the vector is 19 in size (one row, nine columns).

The total size is 72 bytes (9 numbers at 8 bytes each).

Page 27: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The semi-colon operator ; may be used to suppress MATLAB displaying values on the screen.

It serves as an end-of-line indicator.

Page 28: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

MATLAB has the built-in capacity for complex numbers.

A complex number has a real part and an imaginary part.

i = j = sqrt(–1) is the imaginary unit.

Complex number arithmetic must obey a specific set of rules.

Page 29: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Complex number addition.

Page 30: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Complex number subtraction.

Page 31: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Complex number multiplication.

Page 32: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

Complex number division.

Page 33: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB
Page 34: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The vector x starts at zero and goes to 10 with an increment of 0.01.

The brackets [ ] are used to enclose a vector.

The sine operation is performed on each element of the vector x and the results are assigned to a vector y.

y has the same number of elements as x.

Page 35: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB

The plot command creates a graph.

plot(horizontal axis vector, vertical axis vector)

The xlabel and ylabel commands print labels on the axes.

The axis names are inside the quotation marks.

Page 36: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB
Page 37: ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB