Sunteți pe pagina 1din 7

GENN004: Introduction to Computers

09-Feb-13

Arithmetic Operations
Variables, Assignment, Expressions

Outline
Variables Assignment Statements Arithmetic Operators Built-in MATLAB Functions

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

MATLAB Desktop
Current Directory Workspace Browser The prompt Function Browser

Help Browser

The Command Window Path Browser

The Command History Window

The Start Button

What are the usages of these items? Try it and take notes

Command Window

User Input (What you typed) Calculation Results (What MATLAB computed from your statement)

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

Variables
Variable: a named space for storing a value radius area Valid names start with a letter, can contain digits. Use meaningful variable names. MATLAB is case sensitive (area and Area are different)

Assignment Statement
Variable: a named space for storing a value sum Assignment: putting a value into a variable Assignment operator: = An assignment statement: r= 2*4.5 Expression on Right Hand Side is evaluated before the assignment operation

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

Assignment Statement
Expression on rhs is evaluated before the assignment operation Examples:
x= 2*3.14 y= 1+x z= 4^2 cos(y)

Any variable on the rhs must be initialized

Scalar vs Array
Scalar: indicates a variable that holds only a single value at a time.
This is compared to an array, which holds many values at once

Array: is an indexed grouping of values, addressed with subscripts


A = [10, 20, 30, 40, 50] A(1)=10 A(2)=20 A(3)=30

B = [1.0 2.0 3.0] creates a 1 x 3 row array/vector length(vec): Returns length (# of elements) of vector length(B) returns 3

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

Operators
+ * / \ ^ Addition Subtraction Multiplication Division (divided by e.g., 10/5 is 2) Division (divided into e.g., 5\10 is 2) Exponentiation (e.g., 5^2 is 25)

Operators Precedence
() ^ *,/,\ +, parentheses exponentiation negation multiplication and division addition and subtraction

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

Arithmetic Operation Examples


What the results would be for the following expressions (do it by hand then test it on MATLAB): 1. a = 3 + 5 / 10 2. a = ( 3 + 5 ) / 10 3. a = 3 + 5 / 10 * 3 4. a = 3/2 5 / 10 5. a = 3 5/2 6. a = ((3+5)/((3+2)*2))
Etter/Ingber

Arithmetic Operations Examples


Write the MATLAB expression to evaluate the following equations: Assume x=3, y=5, z=10 (do it by hand then test it on MATLAB) x2 + y 2 a= ; 3z 3 x + 4 yz + 5 a= ; x+ y

(2 x + 3 y ) 2 a= ; 3z + 5
Etter/Ingber

Arithmetic Operations

GENN004: Introduction to Computers

09-Feb-13

MATLAB built-in functions


x= 25; y=cos(x); a=sqrt(x); z=rem(13,5); Function name Arguments (values) passed to the function - For more functions, check the Function Browser - Or type help elfun to show a list of the elementary math functions - Practice: Find out what the rounding functions floor, ceil, and round do.

Functions Examples
Evaluate the following expressions (do it by hand then test it on MATLAB): 1. 2. 3. 4. 5. 6. floor(-2.6) abs(-10*2.5) floor(ceil(10.8)) rem(10,3) round(10.4) fix(10.4) ceil(-2.6) abs(-2^5) ceil(floor(10.8)) rem(3,10) round(10.6) fix(10.6)

Arithmetic Operations

S-ar putea să vă placă și