Programming in MATLAB Part 2

Size: px
Start display at page:

Download "Programming in MATLAB Part 2"

Transcription

1 Programming in MATLAB Part 2 A computer program is a sequence of computer commands. In a simple program the commands are executed one after the other in the order they are typed. MATLAB provides several tools than can be used to control the flow of a program. Conditional statements make it possible to skip commands or to execute specific groups of commands in different situations. For loops and while loops make is possible to repeat a sequence of commands several times. CONTROL FLOW To build useful programs it is vital to have evaluation and re-direction facilities during execution. In evaluating conditions we use RELATIONAL and LOGICAL operators: RELATIONAL OPERATORS Operator Interpretation < less than > greater than < = less than or equal to > = greater than or equal to = = equal ~ = not equal The relational operators can be used to form logical expressions which are evaluated as true[1] or false[0]. MATLAB returns 1 for true, and 0 for false. e.g. If a = [2 ] and b = [3 ] then the logical expression a <b is true[1] COMPOUND EXPRESSIONS Logical expressions can be combined into compound expressions using connectors : Connector Interpretation & AND Operates on two operands (A and B). If both are true, the results is true(1); otherwise the result is false OR Operates on two operands (A and B). If either one or both are true the results in true (1); otherwise (both are false) the result is false (0)

2 ~ NOT Operates on one operand (A). Gives the opposite of the operand; true(1) if the operand is false and false (0) if the operand is true. First & Second Compound Example: a<b & a<c First Second Compound Example: a<b a<c NOT is the logical complement, returning 0 for non-zero elements, and 1 for zero elements. i.e If a<b is true then ~(a<b) is false.

3 The results of a relational operation with vectors, which are vectors with 0s and 1s are called logical vectors and can be used for addressing vectors. When a logical vector is used for addressing another vector, it extracts from that vector the elements in the position where the logical vector has 1s. Eg.

4

5

6 This inequality is correct mathematically. The answer however is false since MATLAB executes from left to right. -5<x is true (=1) and then 1<-1 is false (0) The mathematically correct statement is obtained by using the logical operator &. The inequalities are executed first. Since both are true(1), the answer is 1. y<7 is executed first, it is true(1), and ~1 is 0 ~y is executed first. y is true (1)(since y is nonzero), ~1 is 0, and 0<7 is true(1) y>=8 (false), and x<-1 (true) are executed first. OR is executed next(true).~is executed last, and gives false(0). y>=8 (false), and x<-1 (true) are executed first. NOT of (y>=8) is executed next (true). OR is executed last, and gives true (1). Sample Problem: Analysis of temperature data. The following were the daily maximum temperatures in deg. C in Washington D.C., during the month of April.: Use relational and logical operations to determine the following. (a) The number of days the temperature was above 24 deg. C (b) The number of days the temperature was between 18 and 27 (c) The days of the month when the temperature was between 10 and 16

7 Conditional Statements A conditional statement is a command that allows MATLAB to make a decision on whether to execute a group of commands that follow the conditional statement or to skip these commands. In a conditional statement a conditional expression is stated. If the expression is true, a group of commands that follow the statement are executed. If the expression is false the computer skips the group. Eg. if a<b if c>=5 if a==b if a~=0 if (d<h)&(x>7) if (x~=13) (y<0)

8 Conditional statements can be part of a program written in a script file or a user defined function. For every if statement there is an end statement The if statement is commonly used in three structures, if-end, if-else-end, and if-elseif-else-end. The if-end Structure The if-end conditional statement is shown schematically below. The figure shows how the commands are typed in the program and a flowchart that symbolically shows the flow or sequence in which the commands are executed. As the program executes, it reaches the if statement. If the conditional expression in the if statement is true(1), the program continues to execute the commands that follow the if statement all the way down to the end statement. If the conditional expression is false (0), the program skips the group of commands between the if and the end and continues with the commands that follow the end. The words if and end appear on the screen in blue and the commands between the if and end statement are automatically indented which makes the program easier to read. Eg. A worker is paid according to his hourly wage up to 40hours and 50% more for overtime. Write a program in a script file that calculates the pay to the worker. The program asks the user to enter the number of hours and the hourly wage. The program then displays the pay.

9 The if-else-end statement The if-else-end structure provides a means for choosing one group of commands out of a possible two groups for execution. The if-else-end structure is shown below. The figure shows how the commands are typed in the program and a flowchart illustrates the flow or sequence in which the commands are executed. The first line is an if statement with a conditional expression. If the conditional expression is true the program executes group 1 of commands between the if and the else statements and then skips to the end. If the conditional expression is false the program skips to the else and then executes group 2 of commands between the else and the end. Example: The systolic and diastolic blood pressure readings are found when the heart is pumping and the heart is at rest, respectively. A biomedical experiment is being conducted only on subjects whose blood pressure is optimal. This is defined as a systolic blood pressure less than 120 and a diastolic less than 80. Write a script file than will prompt for both blood pressure values and will print whether the person is a candidate for the experiment or not.

10 The if-elseif-else-end statement The if-elseif-else-end structure is shown below. The figure shows how the commands are typed in the program and gives a flowchart that illustrates the flow or the sequence in which the commands are executed. This structure includes two conditional statements (if and elseif) that make it possible to select one out of three groups of commands for execution. The first line is an if statement with a conditional expression. If the conditional expression is true the program executes group 1 commands between the if and the elseif statements and then skips to the end. If the conditional expression in the if statement is false the program skips to the elseif statement. If the conditional expression in the elseif statement is true the program executes group 2 of commands between the elseif and the else and then skips to the end.

11 If the conditional expression in the elseif statement is false the program skips to the else and executes group 3 commands between the else and the end. Note: Several elseif statements and associated groups can be added to create a file with >3 options. Sample Problem In fluid dynamics, the Reynolds number Re is a dimensionless number used to determine the nature of a fluid flow. For an internal flow (eg. water flow through a pipe) the flow can be categorized as follows Re 2300 Laminar Region 2300<Re 4000 Transition Region Re> 4000 Turbulent Region. Write a script file that will prompt the user for the Reynolds number of a flow and will print the region the flow is in.

12 Example1: Develop a program to categrise an item from 1-4 depending on its weight. If the weight of the item is below 50 kg, it is classified as category 1, between is category 2, between is category 3, and above 200 is category 4. Example 2: Assume that UL-Bank now offers 9% interest on balances of less than 5000, 12% for balances of 5000 or more but less than 10,000 and 15% for balances of 10,000 or more. Develop a program to calculate a customer s new balance after one year.

13 Sample Problem The continuity equation in fluid dynamics for steady fluid flow through a stream tube equates the product of the density, velocity, and area at two points that have varying cross sectional areas. For incompressible flow, the densities are constant so the equation is A 1 V 1 = A 2 V 2. If the areas and V 1 are known, V 2 can be found as (A 1 /A 2 )*V 1 Therefore, whether the velocity at the second point increases or decreases depends on the areas at the two points. Write a script file that will prompt the user for the two areas in square feet, and will print whether the velocity at the second point will increase, decrease, or remain the same as at the first point.

14 LOOPS A loop is another method to alter the flow of a computer program. In a loop the execution of a command or a group of commands is repeated several times consecutively. Repeat LOOPS There are two types of repeat loops that can be used. The for loop and the while loop. We have already covered the while loop. In a loop the execution of a command or a group of commands is repeated several times consecutively. Each round of execution is called a pass. The for loops is used for a fixed number of passes and the while loop is used for an unknown number of repeat. Both types of loops can be terminated using the break command. In for-end loops the execution of a command or a group of commands is repeated a predetermined number of times. The form of loop is shown below. Loop index variable Value of k in the first pass The increment in k after each pass for k = f:s:t The value of k in the last pass. A group of MATLAB Commands.. end.

15 The loop index variable can have any variable name but is usually i,j,k,m,n, however i,j should not be used if MATLAB is used with complex numbers. In the first pass k=f and the computer executed the commands between the for and end commands. Then the program goes back to the for command for the second pass. K obtains a new value equal to k=f+s and the commands between the for and end commands are executed with the new value. The process repeats itself until the last pass where k=t. Then the program does not go back to the for but continues with the commands that follow the end commands. For example 1:2:9, there are five loops and the corresponding values of k are 1,3,5,7 and 9. The increment s can be negative k=25:-5:10 gives four passes with k=25,20,15,10. If the increment s is omitted the default is 1. K=3:7 give k=3,4,5,6,7 If f=t the loop is executed once If f>t and s>0 or if f<t and s<0 the loop is not executed. If the values of of k,s,and t are such that k cannot be equal to t, then if s is positive, the last pass is the one where k has the largest value that is smaller than t (i.e. k=8:10:50 produces five passes with k=8:10:50 produces five passes with k=8,18,28,38,48. If s is negative, the last pass is the one where k has the smallest value that is larger than t. In the for command k can also be assigned a specific value. Example: for k=[ ] The value of k should not be redefined within the loop. Each for command in a program must have an end command. The value of the loop index variable(k) is not displayed automatically. It is possible to display the value in each pass by typing k as one of the command is the loop. When the loop ends, the loop index variable(k) has the value that was last assigned to it. An example of a for-end loop is shown below. When this program is executed the loop is executed four times. The value of k in the four passes is k=1,4,7 and 10 which means that the values that are assigned to x in the passes are 1,16,49 and 100 respectively. Since a semicolon is not typed at the end of the second line, the value of x is displayed in the command window at each pass.

16 Colon operator: n=1:1:50; n goes from 1 to 50 in increments if 1 n=1:50; if the increment is omitted, 1 is assumed. n goes from 1 to 50 in steps of 1. n=1:2:50, no goes from 1 to 5 in steps of 2. Example: % This program calculates the sum of first fifty integers. tot=0; for number=1:50 tot=tot+number; end disp(tot) FOR Loop Rules: 1. The index must be a variable 2. If the expression is a scalar, the loop will be executed once. 3. If the expression is a row vector, the index will contain the next value in the vector in each loop. 4. When the loop is completed, the index contains the last value used. 5. If the colon operator is used to define the expression matrix using the following format: for k =initial : increment : limit then the no. of loops executed can be calculated from: limit-increment floor +1 increment Note: If the value is negative, the loop will not be executed.

17 Q. Write a for loop that will print the column of real numbers from 1.5 to 3.1 in steps of 0.2. for i = 1.5:0.2:3.1 disp(i) end Q Use a for-end loop in a script file to calculate the sum of the first n terms of the series: n k k k k. Execute the script file for n=4 and n=20. n=input( Enter the number of terms ); S=0; for k=1:n S=S+(-1)^k*k/2^); end fprintf( The sum of the series is: %f,s) Q In the Command Window, write a for loop that will print the elements from a vector variable in sentence format. For example, if this is the vector: >> vec = [ ]; this would be the result: Element 1 is Element 2 is Element 3 is The for loop should work regardless of how many elements are in the vector. >> vec = [ ]; >> for i = 1:length(vec) fprintf('element %d is %.2f\n',i,vec(i)) end Element 1 is Element 2 is Element 3 is 2.00 Element 4 is 9.00 Element 5 is 6.00 Note:conversion characters used in fprintf command. %d integer %f float %c single character %s string

18 Note on Matrices Q A vector is given by V=[5,17,-3,8,0,-7,12,15,20,-6,6,4,-7,16]. Write a script file that doubles the elements that are positive and are divisible by 3 or 5 and raises to the power of 3 the elements that are negative but greater than -5. This problem is solved by using a for-end loop that has an if-elseif-end conditional statement inside. The number of passes is equal to the number of elements in the vector. In each pass one element is checked by the conditional statement. The element is changed if it satisfies the conditions in the problem statement. A program in a script file that carries out the required operations is:

19 BREAK STATEMENT The BREAK statement can be used to exit a loop before it has been completed. Example: % This program shows use of break statement. If the denominator is zero % the program will exit the loop before it is completed. for i=1:5 den=input( Enter a value for denominator ) if den==0 disp( denominator is zero ) break end end WHILE LOOPS The while loop is a structure for repeating statements as long as a condition is true. while logical expression statements end

20 Example: We are making an investment of 100, which draws 10% in compound interest per year. This program will display the years and balance up to the point where initial investment is doubled.

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering 1 Introduction A computer program is a sequence of computer

More information

Chapter 7: Programming in MATLAB

Chapter 7: Programming in MATLAB The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 7: Programming in MATLAB 1 7.1 Relational and Logical Operators == Equal to ~=

More information

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif

Lecture 3 Programming in MATLAB. Dr. Bedir Yousif Lecture 3 Programming in MATLAB Dr. Bedir Yousif RELATIONAL AND LOGICAL OPERATORS Relational operator Description < Less than > Greater than = Greater than or equal to = = Equal

More information

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk Control Statements ELEC 206 Prof. Siripong Potisuk 1 Objectives Learn how to change the flow of execution of a MATLAB program through some kind of a decision-making process within that program The program

More information

Programming for Experimental Research. Flow Control

Programming for Experimental Research. Flow Control Programming for Experimental Research Flow Control FLOW CONTROL In a simple program, the commands are executed one after the other in the order they are typed. Many situations require more sophisticated

More information

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control COGS 119/219 MATLAB for Experimental Research Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control .m files We can write the MATLAB commands that we type at the command window

More information

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28 Control Structures Dr. Mihail March 1, 2015 (Dr. Mihail) Control March 1, 2015 1 / 28 Overview So far in this course, MATLAB programs consisted of a ordered sequence of mathematical operations, functions,

More information

Thinking Like an Engineer. Instructor Slides

Thinking Like an Engineer. Instructor Slides Instructor Slides Thinking Like an Engineer An Active Learning Approach Stephan, Bowman, Park, Sill, Ohland Third Edition Copyright 2015 Pearson Prentice-Hall, Inc. Create relational expressions using

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

Short Version of Matlab Manual

Short Version of Matlab Manual Short Version of Matlab Manual This is an extract from the manual which was used in MA10126 in first year. Its purpose is to refamiliarise you with the matlab programming concepts. 1 Starting MATLAB 1.1.1.

More information

ECE 102 Engineering Computation

ECE 102 Engineering Computation ECE 102 Engineering Computation Phillip Wong MATLAB Loops for while break / continue Loops A loop changes the execution flow in a program. What happens in a loop? For each iteration of the loop, statements

More information

Branches, Conditional Statements

Branches, Conditional Statements Branches, Conditional Statements Branches, Conditional Statements A conditional statement lets you execute lines of code if some condition is met. There are 3 general forms in MATLAB: if if/else if/elseif/else

More information

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

ENGR 1181 MATLAB 09: For Loops 2

ENGR 1181 MATLAB 09: For Loops 2 ENGR 1181 MATLAB 09: For Loops Learning Objectives 1. Use more complex ways of setting the loop index. Construct nested loops in the following situations: a. For use with two dimensional arrays b. For

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999 Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999 1999 by CRC PRESS LLC CHAPTER THREE CONTROL STATEMENTS 3.1 FOR

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators Flow Control We can control when how and the number of times calculations are made based on values of input data and/or data calculations in the program. Statements We Will Use in Flow Control for loops

More information

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Matlab. By: Hossein Hamooni Fall 2014 Introduction to Matlab By: Hossein Hamooni Fall 2014 Why Matlab? Data analytics task Large data processing Multi-platform, Multi Format data importing Graphing Modeling Lots of built-in functions for rapid

More information

MATLAB for Chemical engineer

MATLAB for Chemical engineer University of Baghdad College of Engineering Department of Chemical Engineering MATLAB for Chemical engineer Basic and Applications Lecture No. 9 Dr. Mahmood Khazzal Hummadi Samar Kareem 2016 Lecture No.

More information

function [s p] = sumprod (f, g)

function [s p] = sumprod (f, g) Outline of the Lecture Introduction to M-function programming Matlab Programming Example Relational operators Logical Operators Matlab Flow control structures Introduction to M-function programming M-files:

More information

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights

More information

Programming 1. Script files. help cd Example:

Programming 1. Script files. help cd Example: Programming Until now we worked with Matlab interactively, executing simple statements line by line, often reentering the same sequences of commands. Alternatively, we can store the Matlab input commands

More information

Unit 2: Accentuate the Negative Name:

Unit 2: Accentuate the Negative Name: Unit 2: Accentuate the Negative Name: 1.1 Using Positive & Negative Numbers Number Sentence A mathematical statement that gives the relationship between two expressions that are composed of numbers and

More information

CEMTool Tutorial. Control statements

CEMTool Tutorial. Control statements CEMTool Tutorial Control statements Overview This tutorial is part of the CEMWARE series. Each tutorial in this series will teach you a specific topic of common applications by explaining theoretical concepts

More information

CITS2401 Computer Analysis & Visualisation

CITS2401 Computer Analysis & Visualisation FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Topic 3 Introduction to Matlab Material from MATLAB for

More information

Scientific Computing with MATLAB

Scientific Computing with MATLAB Scientific Computing with MATLAB Dra. K.-Y. Daisy Fan Department of Computer Science Cornell University Ithaca, NY, USA UNAM IIM 2012 2 Focus on computing using MATLAB Computer Science Computational Science

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Boolean Logic & Branching Lab Conditional Tests

Boolean Logic & Branching Lab Conditional Tests I. Boolean (Logical) Operations Boolean Logic & Branching Lab Conditional Tests 1. Review of Binary logic Three basic logical operations are commonly used in binary logic: and, or, and not. Table 1 lists

More information

ECE 202 LAB 3 ADVANCED MATLAB

ECE 202 LAB 3 ADVANCED MATLAB Version 1.2 1 of 13 BEFORE YOU BEGIN PREREQUISITE LABS ECE 201 Labs EXPECTED KNOWLEDGE ECE 202 LAB 3 ADVANCED MATLAB Understanding of the Laplace transform and transfer functions EQUIPMENT Intel PC with

More information

Mathcad Lecture #3 In-class Worksheet Functions

Mathcad Lecture #3 In-class Worksheet Functions Mathcad Lecture #3 In-class Worksheet Functions At the end of this lecture, you should be able to: find and use intrinsic Mathcad Functions for fundamental, trigonometric, and if statements construct compound

More information

Chapter 1 Introduction to MATLAB

Chapter 1 Introduction to MATLAB Chapter 1 Introduction to MATLAB 1.1 What is MATLAB? MATLAB = MATrix LABoratory, the language of technical computing, modeling and simulation, data analysis and processing, visualization and graphics,

More information

REPETITIVE EXECUTION: LOOPS

REPETITIVE EXECUTION: LOOPS Contents REPETITIVE EXECUTION: LOOPS... 1 for Loops... 1 while Loops... 6 The break and continue Commands... 8 Nested Loops... 10 Distinguishing Characteristics of for and while Loops Things to Remember...

More information

Lecture 15 MATLAB II: Conditional Statements and Arrays

Lecture 15 MATLAB II: Conditional Statements and Arrays Lecture 15 MATLAB II: Conditional Statements and Arrays 1 Conditional Statements 2 The boolean operators in MATLAB are: > greater than < less than >= greater than or equals

More information

MATLAB Operators, control flow and scripting. Edited by Péter Vass

MATLAB Operators, control flow and scripting. Edited by Péter Vass MATLAB Operators, control flow and scripting Edited by Péter Vass Operators An operator is a symbol which is used for specifying some kind of operation to be executed. An operator is always the member

More information

2.0 MATLAB Fundamentals

2.0 MATLAB Fundamentals 2.0 MATLAB Fundamentals 2.1 INTRODUCTION MATLAB is a computer program for computing scientific and engineering problems that can be expressed in mathematical form. The name MATLAB stands for MATrix LABoratory,

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

Introduction to programming in MATLAB

Introduction to programming in MATLAB Master Degree Course in ELECTRONICS ENGINEERING http://www.dii.unimore.it/~lbiagiotti/systemscontroltheory.html Introduction to programming in MATLAB e-mail: luigi.biagiotti@unimore.it http://www.dii.unimore.it/~lbiagiotti

More information

PROGRAMMING IN C AND C++:

PROGRAMMING IN C AND C++: PROGRAMMING IN C AND C++: Week 1 1. Introductions 2. Using Dos commands, make a directory: C:\users\YearOfJoining\Sectionx\USERNAME\CS101 3. Getting started with Visual C++. 4. Write a program to print

More information

The Mathematics of Big Data

The Mathematics of Big Data The Mathematics of Big Data Linear Algebra and MATLAB Philippe B. Laval KSU Fall 2015 Philippe B. Laval (KSU) Linear Algebra and MATLAB Fall 2015 1 / 23 Introduction We introduce the features of MATLAB

More information

Mth 60 Module 2 Section Signed Numbers All numbers,, and

Mth 60 Module 2 Section Signed Numbers All numbers,, and Section 2.1 - Adding Signed Numbers Signed Numbers All numbers,, and The Number Line is used to display positive and negative numbers. Graph -7, 5, -3/4, and 1.5. Where are the positive numbers always

More information

Tribhuvan University Institute of Science and Technology 2065

Tribhuvan University Institute of Science and Technology 2065 1CSc.102-2065 2065 Candidates are required to give their answers in their own words as for as practicable. 1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

AN INTRODUCTION TO MATLAB

AN INTRODUCTION TO MATLAB AN INTRODUCTION TO MATLAB 1 Introduction MATLAB is a powerful mathematical tool used for a number of engineering applications such as communication engineering, digital signal processing, control engineering,

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

An Introduction to MATLAB

An Introduction to MATLAB An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu High level language Programing language and development environment Built-in development tools Numerical manipulation Plotting of

More information

MATLAB. Devon Cormack and James Staley

MATLAB. Devon Cormack and James Staley MATLAB Devon Cormack and James Staley MATrix LABoratory Originally developed in 1970s as a FORTRAN wrapper, later rewritten in C Designed for the purpose of high-level numerical computation, visualization,

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Introduction MATLAB is an interactive package for numerical analysis, matrix computation, control system design, and linear system analysis and design available on most CAEN platforms

More information

CS 221 Lecture. Tuesday, 11 October 2011

CS 221 Lecture. Tuesday, 11 October 2011 CS 221 Lecture Tuesday, 11 October 2011 "Computers in the future may weigh no more than 1.5 tons." - Popular Mechanics, forecasting the relentless march of science, 1949. Today s Topics 1. Announcements

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Dr./ Ahmed Nagib Mechanical Engineering department, Alexandria university, Egypt Spring 2017 Chapter 4 Decision making and looping functions (If, for and while functions) 4-1 Flowcharts

More information

Digital Image Analysis and Processing CPE

Digital Image Analysis and Processing CPE Digital Image Analysis and Processing CPE 0907544 Matlab Tutorial Dr. Iyad Jafar Outline Matlab Environment Matlab as Calculator Common Mathematical Functions Defining Vectors and Arrays Addressing Vectors

More information

Big Mathematical Ideas and Understandings

Big Mathematical Ideas and Understandings Big Mathematical Ideas and Understandings A Big Idea is a statement of an idea that is central to the learning of mathematics, one that links numerous mathematical understandings into a coherent whole.

More information

Matlab and Octave: Quick Introduction and Examples 1 Basics

Matlab and Octave: Quick Introduction and Examples 1 Basics Matlab and Octave: Quick Introduction and Examples 1 Basics 1.1 Syntax and m-files There is a shell where commands can be written in. All commands must either be built-in commands, functions, names of

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Math 24 - Study Guide - Chapter 1 Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Give one number between -8 and 8 that is a negative real

More information

Dr. Iyad Jafar. Adapted from the publisher slides

Dr. Iyad Jafar. Adapted from the publisher slides Computer Applications Lab Lab 5 Programming in Matlab Chapter 4 Sections 1,2,3,4 Dr. Iyad Jafar Adapted from the publisher slides Outline Program design and development Relational operators and logical

More information

EP375 Computational Physics

EP375 Computational Physics EP375 Computational Physics Topic 1 MATLAB TUTORIAL BASICS Department of Engineering Physics University of Gaziantep Feb 2014 Sayfa 1 Basic Commands help command get help for a command clear all clears

More information

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis Introduction to Matlab 1 Outline What is Matlab? Matlab desktop & interface Scalar variables Vectors and matrices Exercise 1 Booleans Control structures File organization User defined functions Exercise

More information

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics. Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics. Starting MATLAB - On a PC, double click the MATLAB icon - On a LINUX/UNIX machine, enter the command:

More information

1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order).

1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order). Exercises Exercises 1. Represent each of these relations on {1, 2, 3} with a matrix (with the elements of this set listed in increasing order). a) {(1, 1), (1, 2), (1, 3)} b) {(1, 2), (2, 1), (2, 2), (3,

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

ENGR 1181c Midterm Exam 2: Study Guide and Practice Problems

ENGR 1181c Midterm Exam 2: Study Guide and Practice Problems ENGR 1181c Midterm Exam 2: Study Guide and Practice Problems Disclaimer Problems seen in this study guide may resemble problems relating mainly to the pertinent homework assignments. Reading this study

More information

Taking Apart Numbers and Shapes

Taking Apart Numbers and Shapes Taking Apart Numbers and Shapes Writing Equivalent Expressions Using the Distributive Property 1 WARM UP Calculate the area of each rectangle. Show your work. 1. 6 in. 2. 15 in. 12 yd 9 yd LEARNING GOALS

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Precalculus Notes: Unit 7 Systems of Equations and Matrices

Precalculus Notes: Unit 7 Systems of Equations and Matrices Date: 7.1, 7. Solving Systems of Equations: Graphing, Substitution, Elimination Syllabus Objectives: 8.1 The student will solve a given system of equations or system of inequalities. Solution of a System

More information

Computer Vision. Matlab

Computer Vision. Matlab Computer Vision Matlab A good choice for vision program development because Easy to do very rapid prototyping Quick to learn, and good documentation A good library of image processing functions Excellent

More information

MATH LEVEL 2 LESSON PLAN 1 NUMBER TO INTEGER Copyright Vinay Agarwala, Checked: 06/02/18

MATH LEVEL 2 LESSON PLAN 1 NUMBER TO INTEGER Copyright Vinay Agarwala, Checked: 06/02/18 Section 1: Natural Numbers MATH LEVEL 2 LESSON PLAN 1 NUMBER TO INTEGER 2018 Copyright Vinay Agarwala, Checked: 06/02/18 1. Natural numbers are counting numbers. Counting starts from one. We use these

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab Andreas C. Kapourani (Credit: Steve Renals & Iain Murray) 9 January 08 Introduction MATLAB is a programming language that grew out of the need to process matrices. It is used extensively

More information

EE 301 Signals & Systems I MATLAB Tutorial with Questions

EE 301 Signals & Systems I MATLAB Tutorial with Questions EE 301 Signals & Systems I MATLAB Tutorial with Questions Under the content of the course EE-301, this semester, some MATLAB questions will be assigned in addition to the usual theoretical questions. This

More information

Introduction to Matlab

Introduction to Matlab Davies: Computer Vision, 5 th edition, online materials Matlab Tutorial 1 1 Introduction to Matlab 1. The nature of Matlab Matlab is a computer language developed for the specific purpose of matrix manipulation.

More information

ALGORITHMS AND FLOWCHARTS

ALGORITHMS AND FLOWCHARTS ALGORITHMS AND FLOWCHARTS ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem

More information

MatLab Just a beginning

MatLab Just a beginning MatLab Just a beginning P.Kanungo Dept. of E & TC, C.V. Raman College of Engineering, Bhubaneswar Introduction MATLAB is a high-performance language for technical computing. MATLAB is an acronym for MATrix

More information

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators. Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE

More information

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu L. Oberbroeckling (Loyola University) MATLAB 10/07/10

More information

This is the basis for the programming concept called a loop statement

This is the basis for the programming concept called a loop statement Chapter 4 Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times did you solve it? What if you had millions of data points

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

Computer Programming in MATLAB

Computer Programming in MATLAB Computer Programming in MATLAB Prof. Dr. İrfan KAYMAZ Atatürk University Engineering Faculty Department of Mechanical Engineering What is a computer??? Computer is a device that computes, especially a

More information

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times. EGR 111 Loops This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times. New MATLAB commands: for, while,, length 1. The For Loop Suppose we want print a statement

More information

Physics 326G Winter Class 6

Physics 326G Winter Class 6 Physics 36G Winter 008 Class 6 Today we will learn about functions, and also about some basic programming that allows you to control the execution of commands in the programs you write. You have already

More information

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS

UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS All programs need to be submitted on 7th Oct 206 by writing in hand written format in A4 sheet. Flowcharts, algorithms, source codes and outputs

More information

Chapter 4 Branching Statements & Program Design

Chapter 4 Branching Statements & Program Design EGR115 Introduction to Computing for Engineers Branching Statements & Program Design from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Program Design

More information

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices Introduction to Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> V = 50 >> V + 2 >> V Ans = 52 >> a=5e-3; b=1; a+b Most elementary functions and constants are

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Chapter 1 Section 1 Solving Linear Equations in One Variable

Chapter 1 Section 1 Solving Linear Equations in One Variable Chapter Section Solving Linear Equations in One Variable A linear equation in one variable is an equation which can be written in the form: ax + b = c for a, b, and c real numbers with a 0. Linear equations

More information

Summary of the Lecture

Summary of the Lecture Summary of the Lecture 1 Introduction 2 MATLAB env., Variables, and format 3 4 5 MATLAB function, arrays and operations Algorithm and flowchart M-files: Script and Function Files 6 Structured Programming

More information

McTutorial: A MATLAB Tutorial

McTutorial: A MATLAB Tutorial McGill University School of Computer Science Sable Research Group McTutorial: A MATLAB Tutorial Lei Lopez Last updated: August 2014 w w w. s a b l e. m c g i l l. c a Contents 1 MATLAB BASICS 3 1.1 MATLAB

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

C/C++ Programming for Engineers: Matlab Branches and Loops

C/C++ Programming for Engineers: Matlab Branches and Loops C/C++ Programming for Engineers: Matlab Branches and Loops John T. Bell Department of Computer Science University of Illinois, Chicago Review What is the difference between a script and a function in Matlab?

More information

Introduction to MATLAB LAB 1

Introduction to MATLAB LAB 1 Introduction to MATLAB LAB 1 1 Basics of MATLAB MATrix LABoratory A super-powerful graphing calculator Matrix based numeric computation Embedded Functions Also a programming language User defined functions

More information

2 T. x + 2 T. , T( x, y = 0) = T 1

2 T. x + 2 T. , T( x, y = 0) = T 1 LAB 2: Conduction with Finite Difference Method Objective: The objective of this laboratory is to introduce the basic steps needed to numerically solve a steady state two-dimensional conduction problem

More information

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you

More information

Matlab- Command Window Operations, Scalars and Arrays

Matlab- Command Window Operations, Scalars and Arrays 1 ME313 Homework #1 Matlab- Command Window Operations, Scalars and Arrays Last Updated August 17 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents

More information

Section Learning Objective Media You Examples Try

Section Learning Objective Media You Examples Try UNIT 2 INTEGERS INTRODUCTION Now that we have discussed the Base-10 number system including whole numbers and place value, we can extend our knowledge of numbers to include integers. The first known reference

More information

Chapter 1: Number and Operations

Chapter 1: Number and Operations Chapter 1: Number and Operations 1.1 Order of operations When simplifying algebraic expressions we use the following order: 1. Perform operations within a parenthesis. 2. Evaluate exponents. 3. Multiply

More information

Conditionals and Recursion. Python Part 4

Conditionals and Recursion. Python Part 4 Conditionals and Recursion Python Part 4 Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print (remainder) 1 Boolean expressions An expression that

More information

Lecture 10 Online Portal http://structuredprogramming.wikispaces.com/ Menus A menu is a list of functions that a program can perform; the user chooses the desired function from the list. E.g., 1 convert

More information