ece 1304 introduction to electrical and computer engineering section 1.1 introduction to matlab

Post on 12-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

ECE 1304Introduction 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.

Commands entered here are executed immediately.

This is called an Interactive Session.

The commands are recorded in the Command History.

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

The operation returns a value.

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

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

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

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

In this case the variable is named “r.”

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.

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.

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.

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

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

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.

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

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

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.

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.

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.

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

The clc operator clears the command window.

The clc operator does not clears the memory.

The clc only changes the appearance of the Command Window.

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

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.

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).

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

It serves as an end-of-line indicator.

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.

Complex number addition.

Complex number subtraction.

Complex number multiplication.

Complex number division.

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.

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.

top related