Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Basics.

Similar presentations


Presentation on theme: "Matlab Basics."— Presentation transcript:

1 Matlab Basics

2 Sequential

3 Matlab has the usual programming operators for forming expressions:
+ Addition Subtraction * Multiplication / Division \ Left division (Solves Linear Equations) ^ Power ' Matrix-Vector Transpose ( ) Specify Evaluation Order The multiplication, division and power operators can be modified to perform “element-by-element” arithmetic on vectors and matrices of matching size.

4 Operators and Expressions
With the exception of the unary minus sign, operators are binary operators. That is they act on two operands. The unary minus changes the sign of the the operand immediately following it. Unary Operation: -5 Binary Operation: 5+3 Expressions are made up of series of operators and operands. A + b / 2

5 When used in an extended sequence, operations are executed in a certain order controlled by “order of precedence” rules. Operations of equal precedence are evaluated from left to right. Parentheses () Transpose(') Power(^, .^) Unary plus (+), unary minus (-), logical negation (~) Multiplication (*, .*), right division (/, . /), left division (\, .\) Addition (+), subtraction (-) Colon operator (:) Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=) Logical AND (&) Logical OR (|)

6 Assignment Statements
The “workhorse” operation in sequential processing is the assignment statement in which a right-hand side expression is equated to a left-hand side variable. A = b + 5 All quantities on the right-hand side must be defined before execution of the expression. If the left-hand side variable has previously been defined it will acquire the new value.

7 A couple of examples: A = [3 9 5]; B = [2 1 5]; C = A . / B .^ 2 C = C = (A . / B) .^ 2 C = A + B . / B C = [4 10 6] C = ( A+B ) . / B ?

8 Input/Output User input can be obtained via the input function:
>> A = input('Give me a number: '); Give me a number: 5 Input can also process string responseses: >> A = input('Give me a string: ','s'); Give me a string: Hello

9 Simple output can be produced in one of two ways:
Missing semi-colon >> A = 5 + 3; Produces no output >> A = 5 + 3 A = 8 disp function >> disp(‘A is: ‘); disp( A ); 8

10 The output methods on the previous slides are not able to produce nicely formatted output.
>>fprintf(‘\nThe value of A is: %7.3f\n’, A); The value of A is: The first argument is called the template. It contains a mixture of text to be printed, formatting characters, and output specifiers. \n is a newline character. %7.3f tells fprintf to save seven columns to print a floating point number (f) with three decimal place precision. For every % specifier in the template, an extra argument is normally required. These arguments are the values to be printed.

11 Logical Tests

12 Matlab has a group of operators that are used to for logical tests
Matlab has a group of operators that are used to for logical tests. They are called relational operators. These operators are used to construct logical expressions which evaluate to either true or false values. Operator Description < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not Equal to

13 Examples a = 5.5 ; b = 1.5 ; c = -3 a + b >= 6.5 true ( 1 )
false ( 0 ) true ( 1 ) Note how vectors and matrices are compared (element-by-element) a = [ ] ; b = [ ] a <= b [ ]

14 Logical operators are used to construct compound logical expressions that are used to test the state of several conditions at once. Inputs and or xor not A B A&B A|B xor(A,B) ~A 1 The precedence for the logical operators with respect to each other is: not (~) has the highest precedence. and (&) or (|)

15 Examples a = 5.5 ; b = 1.5 ; c = -3 a < 10 & a > 5 true ( 1 )
abs( k ) > 3 | k < b – a b < c | b == 1.5 b < c | ~(b == 1.5) true ( 1 ) false ( 0 ) true ( 1 ) false ( 0 )

16 Selection Structures

17 A B If both A and B must be full before valve C is open: 1 1 if ( A >= 1 & B >= 1 ) C = 1; else C = 0; end ← Remember Matlab considers: 0 = false 1 = true If either A or B must be full before valve C is open: if ( A >= 1 | B >= 1 ) C = 1; else C = 0; end C

18 The if Structure function y = if_simple( x ) y = 0; if ( x > 0 )
y = x^2; end

19 The if-else Structure function y = if_else( x ) if ( x > 0 )
y = x^2; else y = -x; end

20 Nested if Structures function y = if_else_if( x ) if ( x < 0 )
y = -x; else if ( x < 1 ) y = x^2; y = 1; end

21 The if-elseif-else Structure
function y = if_elseif( x ) if ( x < 0 ) y = -x; elseif ( x < 1 ) y = x^2; else y = 1; end

22 Loop Structures

23 Suppose we want to find integer i such that … + i is just greater than or equal to some input value n. Condition based repetition is necessary. Matlab use the while structure for this. n = input( ‘Enter n’ ); sum = 0; i = 0; while ( sum < n ) i = i + 1; sum = sum + i; end fprintf( ‘/n the integer is %d\n’ , i )

24 WHILE Repeat statements an indefinite number of times.
The general form of a WHILE statement is: WHILE expression statements END The statements are executed while the expression has all non-zero elements. The expression is usually the result of expr rop expr where rop is ==, <, >, <=, >=, or ~=. The BREAK statement can be used to terminate the loop prematurely.

25 Suppose that we wish to sum the elements of a vector a
Suppose that we wish to sum the elements of a vector a. Repetition is also required, but we know how many iterations are required (one for each element in the vector). Matlab uses the for statement for this. n = input( ‘Enter vector a’ ); sum = 0; n = length( a ); for i = 1:n sum = sum + a(i); end fprintf( ‘/n sum is %f\n’ , sum )

26 FOR Repeat statements a specific number of times.
The general form of a FOR statement is: for variable = expr statement ... end The columns of the expression are stored one at a time in the variable and then the following statements, up to the end, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars

27 Examples for i = 1:10 i end for i = [ 1 2 3 4 ; 5 6 7 8 ] i end n=4
A=eye(n,n); for i = 1:n, for j = 1:n, A(i,j) = 1/(i+j−1); end

28 Matlab is optimized for vectorization
Note that when operating on vectors and matrices and vectors, simple matrix arithmetic can sometimes be used to replace repetition loops. Multiply each element in a 10 x 10 matrix by a constant: Old way: for x = 1:10 for y = 1:10 foo(x,y) = 2 * bar(x,y) end Better (MATLAB) way: foo = 2 * bar; Matlab is optimized for vectorization

29 Suppose we wish to simulate flipping a coin to determine the number of heads and tails that occur after n flips. Using a random number generator we can generate a series of 0’s (heads) and 1’s (tails) and keep a tally. The algorithm can be expressed as either a function or a script. function y = flips( n ) heads = 0 ; tails = 0 ; for i = 1:n flip = floor(2*rand); if (flip == 0) heads = heads + 1; elseif (flip == 1) tails = tails + 1; else error( 'bad flip' ); end y = [ heads tails ]; n = input('Number of flips? '); heads = 0 ; tails = 0 ; for i = 1:n flip = floor(2*rand); if (flip == 0) heads = heads + 1; elseif (flip == 1) tails = tails + 1; else error( 'bad flip' ); end fprintf('\nHeads: %d\nTails: %d', heads, tails);


Download ppt "Matlab Basics."

Similar presentations


Ads by Google