Matlab Basics.

Slides:



Advertisements
Similar presentations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Introduction to C Programming
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Sorting numbers Arrange a list of n numbers a1, a2, a3,..., an in ascending order. A solution: Using Insertion sort. What is Insertion sort? Insertion.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 4: Control Structures I (Selection)
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 – C Program Control
EEE 161 Applied Electromagnetics
ARITHMETIC IN C Operators.
Operators And Expressions
Control Statements in Matlab
Chapter 2 - Introduction to C Programming
Computing Fundamentals
2.5 Another Java Application: Adding Integers
Arithmetic operations & assignment statement
Chapter 2 - Introduction to C Programming
JavaScript: Control Statements.
MATLAB: Structures and File I/O
Control Structures – Selection
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS1100 Computational Engineering
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Chapter 4 - Program Control
C Operators, Operands, Expressions & Statements
3 Control Statements:.
Chapter 4 Selection.
Chapter 2 - Introduction to C Programming
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Boolean Expressions to Make Comparisons
Chapter 2 - Introduction to C Programming
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Introduction to C Programming
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Matlab Basics

Sequential

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.

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 + 5.6 / 2

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

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.

A couple of examples: A = [3 9 5]; B = [2 1 5]; C = A . / B .^ 2 C = 0.7500 9.0000 0.2000 C = (A . / B) .^ 2 2.2500 81.0000 1.0000 C = A + B . / B C = [4 10 6] C = ( A+B ) . / B ?

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

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

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: 8.000 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.

Logical Tests

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

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 = [ 3 1 5 2 ] ; b = [ 4 3 5 1 ] a <= b [ 1 1 1 0 ]

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

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 )

Selection Structures

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

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

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

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

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

Loop Structures

Suppose we want to find integer i such that 1 + 2 + 3 + … + 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 )

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.

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 )

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

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

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

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