Structure Array 1 / 50

Size: px
Start display at page:

Download "Structure Array 1 / 50"

Transcription

1 Structure Array A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a structure using dot notation of the form structname.fieldname. For example, 1 >> student.name='arthur'; 2 >> student.id='d '; 3 >> student.scores=[80, 90, 100]; 4 >> student % show the content of student 5 >> 6 7 student = 8 9 name: 'Arthur' 1 / 50

2 Title 10 id: 'd ' 11 scores: [ ] fieldnames returns the names of the fields contained in a structure variable. rmfield removes a field from a structure. You can pass structures to functions. More details can be found here. They are extremely useful in applications such as MATLAB GUI and database management. 2 / 50

3 In Workspace 3 / 50

4 Basic Math Functions 1 1 See Table in Palm, p / 50

5 Trigonometric Functions 2 Recall that 1 rad = 180 π. 2 See Table in Palm, p / 50

6 括弧種類整理 3 Parentheses ( ) Arithmetic, e.g. (x + y)/z. Input arguments of a function, e.g. sin(1), exp(1). Array addressing, e.g. A(1) refers to the first element in array A. Square brackets [ ]: only used in array operations e.g. x = [ ]. Curly brackets { }: only used to declare a cell array e.g. A = { This is MATLAB class., x}. 3 Thanks to a lively class discussion (MATLAB-237) on April 16, / 50

7 1 >> Lecture 2 2 >> -- Programming Basics 7 / 50

8 Introduction of MATLAB Programming The MATLAB command mode is very useful for simple problems, but more complex problems require a script. The usefulness of MATLAB is greatly increased by the use of decision making functions in its programs. These functions enable you to write programs whose operations depend on the results of calculations made by the program. MATLAB can also repeat calculations a specified number of times or until some condition is satisfied. This feature enables engineers to solve problems of great complexity or requiring numerous calculations. 8 / 50

9 Program Design and Development Design of programs to solve complex problems needs to be done in a systematic manner from the start to avoid time-consuming and frustrating difficulties later in the process. An algorithm is an ordered sequence of precisely defined instructions that performs specific task in a finite amount of time and space. 9 / 50

10 Elements in Algorithms There are three categories of algorithmic operations: Sequential operations: These instructions are executed in order. selection/conditional operations: These control structures first ask a question to be answered with a true/false answer and then select the next instruction based on the answer. Iterative operations: These control structures repeat the execution of a block of instructions. Note that not every problem can be solved with an algorithm, so-called undecidable problems 4. Besides, some potential algorithmic solutions can fail because they take too long to find a solution. 5 4 See halting problem. 5 Recall that the finiteness is one property of algorithms. 10 / 50

11 Programming Structures 6 6 See Figure 8.1 in Moore, p / 50

12 Structured Programming An algorithm often must have the ability to alter the order of its instructions using what is called a control structure. Structured programming is a technique for designing programs in which a hierarchy of modules/functions is used. Core concept: divide and conquer. In MATLAB, these modules can be built-in or user-defined functions. Structured programming, if used properly, results in programs that are easy to write, understand, modify, and debug. 12 / 50

13 Steps of Developing A Computer Program 1. (Problem formulation) State the problem concisely. (Input) Specify the data to be used by the program. (Output) Specify the information to be generated by the program. 2. (Algorithm) Work through the solution steps by hand or with a calculator; use a simpler set of data if necessary. 3. (Programming) Write and debug the program. 4. (Verification) Check the output of the program with your hand solution. Make sense? 5. (Generalization) If you will use the program as a general tool in the future, test it by running it for a range of reasonable data values. 13 / 50

14 If debugging is the process of removing software bugs, then programming must be the process of putting them in. Edsger W. Dijkstra ( ) 14 / 50

15 Flowcharts Flowcharts make it easy to visualize the structure of a program. It can display the various paths (called branches) that a program can take, depending on how the conditional statements are executed. Why we need flowcharts? Help developing the algorithm and program. Documenting programs properly is very important, even if you never give your programs to other people. 流程圖 (Flow Chart) 常用符號教學 15 / 50

16 Design Elements in Flowchart See Table 8.3 Flowcharting for Designing Computer Programs in Moore, p. 16 / 50

17 Example 8 8 See Figure 8.2 in Moore, p / 50

18 Pseudocode We use pseudocode in which natural language and mathematical expressions are used to construct statements that look like computer statements but without detailed syntax. For example, 1 if (student's grade >= 60) 2 Print pass 3 else 4 Print failed 5 end 18 / 50

19 Relational Operators 9 MATLAB has six relational operators to make comparisons between arrays of equal size. Note that all of them are binary operators, which need two operands. 9 See Table 8.1 in Moore, p / 50

20 Boolean Variables Boolean variables contain only 0 and 1 for false and true, respectively. For example, 1 >> x=2; y=5; 2 >> z=x<y % Note that z is a logical variable. 3 4 z = >> w = (y > x) ~= 1 % Note that w is a logical... variable w = / 50

21 Equivalently, w = x([ ]). (Try.) 21 / 50 More Examples 1 >> x=0:1:10; 2 >> y=10:-1:0; 3 >> z=x<y % Note that z is a logical variable. 4 5 z = >> w=x((x-y>0)) 11 w = In the second example, (x y) > 0 return a logical vector. So, w((x y) > 0) returns a partial vector of vector x when the corresponding element in (x y) > 0 is 1.

22 Logical Operators 22 / 50

23 Precedence of Operators See Table 1.2 Operator Precedence Rules in Attaway, p / 50

24 & vs. && 1 clear all; 2 clc 3 % main 4 x=[ ]; % x is a numeric array 5 y=[ ]; 6 7 x>0 & y>0 % boolean array 8 sum(x-y)>0 && sum(y-x)>0 % boolean scalar The difference between and is similar except that and do or operation. 24 / 50

25 Exercise 11 : & vs. == 1 >> x=[ ]; 2 >> y=[ ]; 3 >> x==y 4 5 ans = >> x&y 11 ans = Thanks to a lively class discussion (MATLAB-237) on April 16, / 50

26 Example: Exclusive OR (XOR) The exclusive OR function, denoted by xor(x, y) returns 0s where x and y are either both nonzero or both 0, and 1s where either x or x is nonzero, but not both. We can use the truth table 12 to find the equivalent boolean expression. More interesting details of XOR can be found here. Please write a program to do XOR operation on two boolean variable x and y. Input: boolean variables x, y Output: xor(x,y) / 50

27 Truth Tables and Basic Logic Gates 27 / 50

28 1 >> x=[3 0 6]; 2 >> y=[5 0 0]; 3 >> z= (x y)& ~(x&y) 4 5 z = Note that the boolean expression of one specific statement is not unique. 28 / 50

29 Logical Functions NaN: Not A Number, caused by the math operation like and See NaN. 29 / 50

30 Conditional Statements MATLAB conditional statements enable us to write programs that make decisions. Conditional statements contain one or more of the if, else, and elseif statements. The end statement denotes the end of a conditional statement. 30 / 50

31 Example: if-else If x is a nonnegative real number, then y = x. input(prompt) gives the user the prompt (in string) and then waits for numeric input from the keyboard. 1 clear all; 2 clc 3 % main 4 x=input('enter a number? '); % input from keyboard 5 if x>=0 6 y=sqrt(x) % show y 7 else 8 disp([num2str(x),' is a negative number.']); 9 end Note that one if should be paired with one end. input(prompt, s ) waits for a string input. 31 / 50

32 Example: if-elseif-else 1 clear all; 2 clc 3 % main 4 x=input('enter any real number: '); 5 if isempty(x) 6 disp('no input.') 7 elseif x>=0 8 disp('it is a positive number.') 9 else 10 disp('it is a negative number.') 11 end 32 / 50

33 Exercise: Do you want to continue? Write a program which allows the user to answer Yes by typing either Y or y or by pressing the Enter key. Any other response is treated as a No answer. 33 / 50

34 1 clear all; 2 clc 3 % main 4 response = input('do you want to continue? Y/N... [Y]:','s'); 5 if (isempty(response)) ( response ==... 'Y') ( response == 'y') 6 response = 'Y' 7 else 8 response = 'N' 9 end 14 Note that logical operators cannot be applied to empty sets, so putting isempty(response) in the first place is a better way. 14 Contribution by Mr. Grant Huang (MAT24217) on July 23, / 50

35 Selection Structure: switch/case The switch/case structure is often used when a series of programming path options exists for a given variable, depending on its value. The code is a bit easier to read with switch/case, a structure that allows you to choose between multiple outcomes, based on specific criterion. 35 / 50

36 Example 1 clear all; 2 clc 3 % main 4 disp('welcome to Taiwan. I can show you the... ticket price.') 5 disp('you can choose Taipei, Taichung, or Tainan.') 6 city = input('enter the name of a city: ','s'); 7 switch city 8 case 'Taipei' 9 disp('price: $100') 10 case 'Taichung' 11 disp('price: $200') 12 case 'Tainan' 13 disp('price: $300') 14 otherwise 15 disp('not an option.') % default 16 end 36 / 50

37 Error Handling You can use a try-catch statement to execute code after your program encounters an error. try-catch can be useful if you: want to finish the program in another way that avoids errors; need to clean up unwanted side effects of the error; 1 try 2 try block; % normal execution code 3 catch 4 catch block; % error handling section 5 end lasterr returns the last error message. 37 / 50

38 Example: Combinations For all nonnegative integers n k, ( n k) is given by ( ) n n! = k k!(n k)!. Note that factorial(n) returns n!. 1 clear all; 2 clc; 3 % main 4 n=input('n=?'); 5 k=input('k=?'); 6 if n>=0 && k>=0 7 y=factorial(n)/(factorial(k)*factorial(n-k)) 8 else 9 disp('invalid inputs.') 10 end 38 / 50

39 Try n = 2, k = 5. factorial( 3) is not allowed! 1 clear all; 2 clc; 3 % main 4 n=input('n=?'); 5 k=input('k=?'); 6 if n>=0 && k>=0 7 try 8 y=factorial(n)/(factorial(k)*factorial(n-k)) 9 catch 10 lasterr 11 y=factorial(k)/(factorial(n)*factorial(k-n)) 12 end 13 else 14 disp('invalid inputs.') 15 end 39 / 50

40 Repetition Structures As a rule of thumb, if a section of code is repeated more than three times, it is a good candidate for a repetition structure. Repetition structures are often called loops. All loops consist of 5 basic parts: A parameter to be used in determining whether or not to end the loop. Initialization of this parameter. A way to change the parameter each time through the loop. 15 A comparison, using the parameter, to a criterion used to decide when to end the loop. Calculations to do inside the loop. 15 If not, then an infinite loop, that is, a loop never stops. 40 / 50

41 MATLAB supports two different types of loops: the for loop and the while loop. break, continue, and return can be used in the loop if some condition meets. break: to end the loop even when the loop does not finish. continue: to pass the loop once. return: to end the subroutine (function). 41 / 50

42 for Loops for loop is the easiest choice when you know how many times you need to repeat the loop. 1 for loop variable 2 statements 3 end 42 / 50

43 43 / 50

44 Example: for Loop 1 clear all; 2 clc 3 % main 4 for i=[1 3 5] % Can be a string. (Try.) 5 i 6 end 1 clear all; 2 clc 3 % main 4 for i=0:1:10 5 i 6 end 44 / 50

45 while Loops While loops are the easiest choice when you need to keep repeating the instructions until a criterion which is written in logical expression is met. 1 while (criterion) 2 statements 3 end Note that before while, the criterion will be checked. Can you replace a while loop with a for loop? (Try.) 45 / 50

46 46 / 50

47 Example: while Loop Write a program to determine the number of terms required for the sum of the series 5k 2 2k, k N to exceed What is the sum for this many terms? Note that N = {1, 2, }. 47 / 50

48 Solution 1 clear all; 2 clc 3 % main 4 sol=0; 5 k=0; 6 while sol < 1e4 7 k=k+1; 8 sol=sol+5*kˆ2-2*k; 9 end 10 sol 11 k 48 / 50

49 Example: A Simple Infinite Loop 1 while 1 2 fprintf('press ctrl+c to stop me!!!!\n'); 3 end In line 2, fprintf shows a string on the screen. Besides, \n is used to create a new line. Note that your can stop your program by pressing ctrl+c. 49 / 50

50 Example: Infinite Loop by for? 1 clear all; 2 clc 3 % main 4 for i=0:inf % inf is a special word for infinite... large number. 5 end 1 Warning: FOR loop index is too large. Truncating... to > In test at 5 3 Elapsed time is seconds. I cannot run an infinite loop using a for loop on my desktop MATLAB R2010a (32-bit) on Windows 7 (64-bit), i7 920 with 14G memory. 50 / 50

Variables and Assignments

Variables and Assignments Variables and Assignments ˆ A variable is used to keep a value or values. ˆ A box which contains something. ˆ In most languages, a statement looks like var = expression, where var is a variable and expression

More information

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be

Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be Arrays ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be ˆ row vectors: u R 1 n for any positive integer n ˆ

More information

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

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

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

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

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 256 Summer 2015 Class Information Official

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block Chapter 4 Branching Statements and Program Design Programming 1: Logical Operators, Logical Functions, and the IF-block Learning objectives: 1. Write simple program modules to implement single numerical

More information

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB SECTION 2: PROGRAMMING WITH MATLAB MAE 4020/5020 Numerical Methods with MATLAB 2 Functions and M Files M Files 3 Script file so called due to.m filename extension Contains a series of MATLAB commands The

More information

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Program Development Cycle Program development

More information

Outline. Program development cycle. Algorithms development and representation. Examples.

Outline. Program development cycle. Algorithms development and representation. Examples. Outline Program development cycle. Algorithms development and representation. Examples. 1 Program Development Cycle Program development cycle steps: Problem definition. Problem analysis (understanding).

More information

Introduction to Control Structures

Introduction to Control Structures Introduction to Control Structures Aniel Nieves-González Institute of Statistics Spring 2014 Logical and relational operators Let x be a boolean variable, i.e., it can only be {0, 1} ({TRUE, FALSE}). For

More information

Selection Statements

Selection Statements Selection Statements by Ahmet Sacan selection statements, branching statements, condition, relational expression, Boolean expression, logical expression, relational operators, logical operators, truth

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

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

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

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

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 289 Summer 2017 Class Information ˆ The

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

Relational & Logical Operators, Selection Statements

Relational & Logical Operators, Selection Statements Relational & Logical Operators, Selection Statements by Ahmet Sacan selection statements, branching statements, condition, relational expression, Boolean expression, logical expression, relational operators,

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

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Files and File Management Scripts Logical Operations Conditional Statements

Files and File Management Scripts Logical Operations Conditional Statements Files and File Management Scripts Logical Operations Conditional Statements Files and File Management Matlab provides a group of commands to manage user files pwd: Print working directory displays the

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

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

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

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

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

Dr. Khaled Al-Qawasmi

Dr. Khaled Al-Qawasmi Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351 Dr. Khaled Al-Qawasmi ١ Dr. Kahled Al-Qawasmi 2010-2011 Chapter 3 Selection Statements

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming Structured Programming & an Introduction to Error Lecture Objectives Review the basic good habits of programming To understand basic concepts of error and error estimation as it applies to Numerical Methods

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

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

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu

MATLAB provides several built-in statements that allow for conditional behavior if/elseif/else switch menu Chapter 3 What we have done so far: Scripts/Functions have executed all commands in order, not matter what What we often need: A piece of code that executes a series of commands, if and only if some condition

More information

Relational and Logical Operators

Relational and Logical Operators Relational and Logical Operators Relational Operators Relational operators are used to represent conditions (such as space 0 in the water tank example) Result of the condition is either true or false In

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

Programming in MATLAB

Programming in MATLAB Programming in MATLAB Scripts, functions, and control structures Some code examples from: Introduction to Numerical Methods and MATLAB Programming for Engineers by Young & Mohlenkamp Script Collection

More information

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 10 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Chapter Two: Program Design Process and Logic

Chapter Two: Program Design Process and Logic Chapter Two: Program Design Process and Logic 2.1 Chapter objectives Describe the steps involved in the programming process Understand how to use flowchart symbols and pseudocode statements Use a sentinel,

More information

Numerical Methods in Scientific Computation

Numerical Methods in Scientific Computation Numerical Methods in Scientific Computation Programming and Software Introduction to error analysis 1 Packages vs. Programming Packages MATLAB Excel Mathematica Maple Packages do the work for you Most

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 141 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB: Contents VARIABLES... 1 Storing Numerical Data... 2 Limits on Numerical Data... 6 Storing Character Strings... 8 Logical Variables... 9 MATLAB S BUILT-IN VARIABLES AND FUNCTIONS... 9 GETTING HELP IN MATLAB...

More information

++x vs. x++ We will use these notations very often.

++x vs. x++ We will use these notations very often. ++x vs. x++ The expression ++x first increments the value of x and then returns x. Instead, the expression x++ first returns the value of x and then increments itself. For example, 1... 2 int x = 1; 3

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Logic is the anatomy of thought. John Locke ( ) This sentence is false. Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

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

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I ENGI 1020 - Introduction to Computer Programming M A Y 2 8, 2 0 1 0 R E Z A S H A H I D I Last class Last class we talked about the following topics: Constants Assignment statements Parameters and calling

More information

Lab 8: Conditional Statements with Multiple Branches (please develop programs individually)

Lab 8: Conditional Statements with Multiple Branches (please develop programs individually) ENGR 128 Computer Lab Lab 8: Conditional Statements with Multiple Branches (please develop programs individually) I. Background: Multiple Branching and the if/if/ structure Many times we need more than

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

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

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d. Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

Scheme of work Cambridge International AS & A Level Computing (9691)

Scheme of work Cambridge International AS & A Level Computing (9691) Scheme of work Cambridge International AS & A Level Computing (9691) Unit 2: Practical programming techniques Recommended prior knowledge Students beginning this course are not expected to have studied

More information

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics: Computers and FORTRAN Language Fortran 95/2003 Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes Topics: - Program Design - Logical Operators - Logical Variables - Control Statements Any FORTRAN program

More information

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up More on Scripts and Functions Basic Programming Lecture 2 Lecture 3 Lecture 4 Wrap Up Last time Loading data from file: load( filename ) Graphical input and

More information

Chapter 4: Programming with MATLAB

Chapter 4: Programming with MATLAB Chapter 4: Programming with MATLAB Topics Covered: Programming Overview Relational Operators and Logical Variables Logical Operators and Functions Conditional Statements For Loops While Loops Debugging

More information

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards Language Reference Manual Introduction The purpose of

More information

Standard Boolean Forms

Standard Boolean Forms Standard Boolean Forms In this section, we develop the idea of standard forms of Boolean expressions. In part, these forms are based on some standard Boolean simplification rules. Standard forms are either

More information

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++ 162 Our Goal 163 4. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation int a; std::cin >> a; if (a % 2 == 0) std::cout

More information

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Copyright 2011 Pearson Addison-Wesley. All rights reserved.

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4. Introduction to Visual Basic and Visual C++ Arithmetic Expression Lesson 4 Calculation I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Arithmetic Expression Using Arithmetic Expression Calculations

More information

LECTURE 1. What Is Matlab? Matlab Windows. Help

LECTURE 1. What Is Matlab? Matlab Windows. Help LECTURE 1 What Is Matlab? Matlab ("MATrix LABoratory") is a software package (and accompanying programming language) that simplifies many operations in numerical methods, matrix manipulation/linear algebra,

More information

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

Selection Statements

Selection Statements Selection Statements 1 Introduction Matlab has two basic selection statements: the if statement and the switch statement. The if statement has optional else and elseif. The relational/comparison and logical

More information

Final Examination Semester 3 / Year 2010

Final Examination Semester 3 / Year 2010 Southern College Kolej Selatan 南方学院 Final Examination Semester 3 / Year 2010 COURSE : PROGRAMMING LOGIC AND DESIGN COURSE CODE : CCIS1003 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM

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

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 88 / 133 Flow Controls The basic algorithm (and program) is constituted

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

Lecture 1: Hello, MATLAB!

Lecture 1: Hello, MATLAB! Lecture 1: Hello, MATLAB! Math 98, Spring 2018 Math 98, Spring 2018 Lecture 1: Hello, MATLAB! 1 / 21 Syllabus Instructor: Eric Hallman Class Website: https://math.berkeley.edu/~ehallman/98-fa18/ Login:!cmfmath98

More information

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples

More information

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

Lecture 3 MATLAB programming (1) Dr.Qi Ying

Lecture 3 MATLAB programming (1) Dr.Qi Ying Lecture 3 MATLAB programming (1) Dr.Qi Ying Objectives Data types Logical operators/functions Branching Debugging of a program Data types in MATLAB Basic: Numeric (integer, floating-point, complex) Logical:

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals INTERNET PROTOCOLS AND CLIENT-SERVER PROGRAMMING Client SWE344 request Internet response Fall Semester 2008-2009 (081) Server Module 2.1: C# Programming Essentials (Part 1) Dr. El-Sayed El-Alfy Computer

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

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation 140 3. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation Our Goal 141 int a; std::cin >> a; if (a % 2 == 0) std::cout

More information

Page 1 of 14 Version A Midterm Review 1. The sign means greater than. > =

More information

IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

More information

Chapters 6-7. User-Defined Functions

Chapters 6-7. User-Defined Functions Chapters 6-7 User-Defined Functions User-Defined Functions, Iteration, and Debugging Strategies Learning objectives: 1. Write simple program modules to implement single numerical methods and algorithms

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection) Control Structures A computer can proceed: In sequence Selectively (branch) - making

More information

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have been covered in the first 5 weeks of the course. The exam

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14 Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to

More information

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions Chapter 3 Flow of Control Branching Loops exit(n) method Boolean data type and expressions Chapter 3 Java: an Introduction to Computer Science & Programming - Walter Savitch 1 What is Flow of Control?

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement

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

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Programming in MATLAB

Programming in MATLAB 2. Scripts, Input/Output and if Faculty of mathematics, physics and informatics Comenius University in Bratislava October 7th, 2015 Scripts Scripts script is basically just a sequence of commands the same

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information