Introduction to MATLAB

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Digital Image Processing Introduction to M-function Programming.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
ITEC 2600 Introduction to Analytical Programming
Chapter 4 – C Program Control
Operators and Expressions
The switch Statement, and Introduction to Looping
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
ECS10 10/10
Chapter 5: Loops and Files.
Scripts & Functions Scripts and functions are contained in .m-files
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
MATLAB: Structures and File I/O
Control Structures – Selection
11/10/2018.
Conditional Statements
Outline Altering flow of control Boolean expressions
During the last lecture we had a discussion on Data Types, Variables & Operators
Patterns to KNOW.
Logical Operations In Matlab.
Introduction to Programming Using Python PART 2
Selection Statements.
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
CHAPTER 21 LOOPS 1.
Matlab Basics.
Chap 7. Advanced Control Statements in Java
REPETITION Why Repetition?
Presentation transcript:

Introduction to MATLAB Session 2 Flow control String evaluation Mihaela Duta mihaela.duta@maths.ox.ac.uk MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

Today’s topics Flow control Logical operators Relational operators Logical indexing Flow control statements String evaluation MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

Logical operators and, or, negation, xor (exclusive or) Two types b a xor b 1 and, or, negation, xor (exclusive or) Two types Element-wise Bit-wise and or negation xor Element-wise & | ~ Bit-wise bitand bitor bitcmp bitxor MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

Relational operators Perform comparisons Output variables of logical type: true = logical(1) – if the relation holds false = logical(0) – if the relation doesn’t hold They are: a < b a less than b a <= b a less than or equal to b a > b a greater than b a >= b a greater than or equal to b a == b a equal to b a ~= b a not equal to b In the previous session we have worked with arithmetical operators. quantitative operations on the operands MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

How to apply relational operators Two matrices of equal dimensions: to each corresponding element A matrix and a single value: to each matrix element in turn compared with the value MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

Logical indexing positional indexing use logical arrays >> b = a( logical([0 1 0 0 0]) ); equivalent to >> b = a(2); example: output of relational operators >> cmp = a > 30; >> a(cmp) >> a = [1 2 3 4 5]; >> a>2 ans = 0 0 1 1 1 >> a(a>2) 3 4 5 MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

Flow control a series of MATLAB commands are executed in the order they appear to alter the order and allow for branching we have to use flow control statements: if-else switch for while break & return a = 2; b=4; b=5; a = 5; disp(‘a is greater than 3’); disp(‘a is less than 3’) MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

if-else Syntax Steps: evaluate <logical-expression> if <logical-expression> <statement series 1> else <statement series 2> end Steps: evaluate <logical-expression> if true, then execute <statement series 1> if false, then execute <statement series 2> MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

switch Syntax Steps: evaluate <variable/expression> switch <variable/expression> case <value 1> <statement series 1> etc case <value n> <statement series n> otherwise <statement series n+1> end Steps: evaluate <variable/expression> check the result against several options <value i> - labels for the case branches execute the series of statements corresponding to one case label if none of the options is valid, then the set of statements corresponding to label otherwise is executed Equivalent to a series of if statements MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

for Syntax for <variable> = <start>:<step>:<stop> <series of statements> end Steps assign to <variable> the value <start> and execute the <series of statements> once add <step> to <variable> and execute <series of statement> again repeat the previous step till <variable> reaches the value <stop> MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

while Syntax while <expression> <series of statements> end Steps Evaluate <expression> and if true execute the <series of statements> once Repeat the previous step till <expression> becomes false If the statements inside the loop do not modify <expression>, we’ll have infinite looping because <expression> will never invalidate MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

break and return break is used inside a for or while loop to request the immediate and unconditional exit from the loop return causes the immediate exit from a function MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2

String evaluation [out1, ...]= eval(<str_with_statement>) where str is a string containing a statement the MATLAB interpreter executes the statement stored [out1, …] = feval(<string_fctname>, in1, …) the MATLAB interpreter executes function given by <string_fctname> with input arguments in1, … and output arguments out1, … MSc in Mathematical and Computational Finance Introduction to MATLAB Session 2