A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CMPS 1371 Introduction to Computing for Engineers
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Lecture 7 Flow of Control: Branching Statements COMP1681 / SE15 Introduction to Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Al-Amer An Introduction to MATLAB Dr. Samir Al-Amer Term 062.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
UNIT II Decision Making And Branching Decision Making And Looping
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Essential MATLAB® for Engineers and Scientists
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Tutorial 1SEG7550 Introduction to MATLAB 18 th, SEP
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Algorithm Design.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Matlab Programming for Engineers
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
CPS120: Introduction to Computer Science Decision Making in Programs.
Fortran: Control Structures Session Three ICoCSIS.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
1 Structured Programming EEN170 Programming in MATLAB.
CSE123 - Lecture 4 Structured Programming- Loops.
Beginning Programming for Engineers Matlab Conditional Computation.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
ITEC 2600 Introduction to Analytical Programming
CNG 140 C Programming (Lecture set 3)
Computer Application in Engineering Design
JavaScript: Control Statements I
CS1371 Introduction to Computing for Engineers
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
Introduction To Matlab Class 2
Introduction to MATLAB
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Logical Operations In Matlab.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Matlab Basics.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
REPETITION Why Repetition?
Presentation transcript:

A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB

9. Flow Control MATLAB has four kinds of statements you can use to control the flow through your code:  If, else, elseif, end : execute statements based on a logical test  Switch, case, otherwise, end: execute groups of statements based on a logical test  While, end: execute statements an indefinite number of times, based on a logical test  For, end: execute statements a fixed number of times  If, else, elseif, end : execute statements based on a logical test  Switch, case, otherwise, end: execute groups of statements based on a logical test  While, end: execute statements an indefinite number of times, based on a logical test  For, end: execute statements a fixed number of times

I NTRODUCTION TO M ATLAB If Structure The basic form is if condition Statements else default statements end The general form is: if condition 1 Statements1 elseif condition 2 statements2 else default statements end The condition is an expression that is either 1 (true) or 0 (false). The statements between the if and end statements are executed if the test is true. If the condition is false the statements will be ignored and execution will resume at the line after the end statement. The condition expression can be a vector or matrix, in which case all the elements must be equal to 1 for the statements to be executed. Further conditions can be made using the elseif and else statements.

I NTRODUCTION TO M ATLAB Examples if (x>=0) z= x; else z = - x end if(x>0) sign =1 elseif (x==0) sign = 0 else sign = - 1 end

I NTRODUCTION TO M ATLAB The basic form of a switch statement is: switch test case result1 statements case result2 statements … otherwise statements end The respective statements are executed if the value of test is equal to the respective results. If none of the cases are true, the otherwise statements are done. Only the first matching case is carried out. If you want the same statements to be done for different cases, you can enclose the several results in curly brackets: Switch Structure

I NTRODUCTION TO M ATLAB switch x case 1 disp(’x is 1’) case {2,3,4} disp(’x is 2, 3 or 4’) case 5 disp(’x is 5’) otherwise disp(’x is not 1, 2, 3, 4 or 5’) end Examples

I NTRODUCTION TO M ATLAB The basic form of a while loop is while condition statements end To find the first integer n for which “1+2+…+n” is greater than 1000 we can do as follow n = 1; % starting value very important while sum(1:n)<=1000 n = n+1; end While Structure The statements are executed repeatedly while the condition is true (its value equal to 1). Examples

I NTRODUCTION TO M ATLAB For Structure The basic form of a for loop is: for index = start : increment : stop statements end You can omit the increment, in which case an increment of 1 is assumed. The increment can be positive or negative. During the first pass through the loop the index will have the value start. The index will be increased by increment during each successive pass until the index exceeds the value stop. Examples s=0 for i=1:3:11 s=s+i end

I NTRODUCTION TO M ATLAB % program 1 performs four iterations of % Newton’s Method X =.7 for i = 1 : 4 X = X – (2*cos(X)-1)/(-2*sin(X)) End Examples MATLAB program to find the roots of f (x) = 2cos(x)-1 Result X = X = X = X =

I NTRODUCTION TO M ATLAB The following example produces views of the peaks function from many angles: clf colormap(gray) plotnum = 1; z = peaks(20); for az = 0:10:350 subplot(6,6,plotnum) surfl(z),shading flat view(az,30) axis tight axis off plotnum = plotnum + 1; end

I NTRODUCTION TO M ATLAB Vectorised Code MATLAB is a matrix language, and many of its algorithms are optimised for matrices. MATLAB code can often be accelerated by replacing for and while loops with operations on matrices. In the following example, we calculate the factorial of the numbers from 1 to 500 using a for loop. Create a script m-file called factorialloop.m that contains the following code: for number = 1:500 fact = 1; for i = 2:number fact = fact*i; end y(number) = fact; end

I NTRODUCTION TO M ATLAB We can time how long this program takes to run by using the stopwatch functions tic and toc: >> tic;factorialloop;toc elapsed_time = Which is the time in seconds. The same calculation can be done in much less time by replacing the internal for loop by the prod function. Create an m-file called factorialvect.m: for number = 1:500 y(number) = prod(1:number); End This version takes about a tenth of the time: >> clear >> tic;factorialvect;toc elapsed_time =

I NTRODUCTION TO M ATLAB Further increases in speed can be achieved by pre-allocating the output matrix y. If we have an m-file called factorialpre.m: y = zeros (1,500); for number = 1:500 y(number) = prod(1:number); End the execution time is about 10% faster: >> clear >> tic;factorialpre;toc elapsed_time =