Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Published byModified over 4 years ago
Presentation on theme: "Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort."— Presentation transcript:
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort
Review Matrices – Two dimensional Arrays Plotting – Powerful Matlab commands used to plot various functions over a specified range. >>plot(x,y) % Plot y as a function of x xlabel(‘axis name’), ylabel(‘yaxis name’), title(‘Plot Title’) Importing Data xlsread(‘file name’) % reads the data from an excel spreadsheet and stores them in a two dimensional array
Programming with Script Files Programs are contained in script files also known as M-files. A program can be ran by typing its name at the command window. Example: Go to “File” -> “New” -> M-file The Editor window will appear (next slide) Type the following commands: % Program example1. m % This program computers computes the sine of the square root, % and displays the result. x= sqrt ([3:2 :11]) ; y= sin(x) xlabel (‘x’) ; ylabel (‘f (x)’) ; grid Save as “example1” To run program type >>example1 on the command window NOTE: The m-file location path should be the same as the path shown in the current directory window.
Programming with Script Files To create a new script file: 1. Go to the Matlab Desktop Menu Toolbar. 2. File Menu 3. New 4. M-File To open and existing script file: 1. Go to the Matlab Desktop Menu Toolbar. 2. File Menu 3. New 4. M-File To save a script file: 1. Go to the Editor Window Menu Toolbar 2. File Menu 3. Save As Matlab code should be typed in the editor window
Programming with Script Files NOTE: The m-file location path should be the same as the path shown in the current directory window. Saving an M-fileSetting the Current Directory Path
Program Structures To enable the implementation of computer algorithms, a computer language needs control structures for Comparison Conditional execution: branching Repetition: looping or iteration
Comparison Comparison is achieved with relational operators. The result of a comparison may also be modified by logical operators. Logical operators are used to combine logical expressions (with “and” or “or”), or to change a logical value with “not”. OperatorMeaning &And |Or ~Not
Example – Logical Operators >> a = 2; b = 4; >> aIsSmaller = a < b; >> bIsSmaller = b < a; >> bothTrue = aIsSmaller & bIsSmaller bothTrue = 0 >> eitherTrue = aIsSmaller | bIsSmaller eitherTrue = 1 >> ~eitherTrue ans = 0
Conditional Execution or Branching: As the result of a comparison, or another logical (true/false) test, selected blocks of program code are executed or skipped. Conditional execution is implemented with if, if...else, andif...elseif constructs. There are three types of if constructs 1. Plain if 2. if...else 3. if...elseif Syntax: if expression block of statements end
Examples 1. >>if a < 0, disp(’a is negative’); end 2. if x < 0 error(’x is negative; sqrt(x) is imaginary’); else r = sqrt(x); end 3. if x > 0 disp(’x is positive’); elseif x < 0 disp(’x is negative’); else disp(’x is exactly zero’); end
Repetition or Looping A sequence of calculations is repeated until either 1. All elements in a vector or matrix have been processed or 2. The calculations have produced a result that meets a predetermined termination criterion Looping is achieved with for loops and while loops. Syntax: for index = expression block of statements end Syntax: while expression block of statements end
For Loop Example: Sum of elements in a vector x = 1:5; % create a row vector sumx = 0; % initialize the sum for k = 1:length(x) sumx = sumx + x(k); end Example: A loop with an index incremented by two for k = 1:2:n... end Example: A loop with an index that counts down for k = n:-1:1... end
While Loop while loops are most often used when an iteration is repeated until some termination criterion is met. Example >> while S+ (n+1)^2 < 100 n = n+1; S = S + n^2; end >> [n, S] ans = 6 91
Bubble Sort The bubble sort works by iterating down an array to be sorted from the first element to the last, comparing each pair of elements and switching their positions if necessary. This process is repeated as many times as necessary, until the array is sorted. Since the worst case scenario is that the array is in reverse order, and that the first element in sorted array is the last element in the starting array, the most exchanges that will be necessary is equal to the length of the array. Here is a simple example:
Assignment 5 1. Determine how long it will take to accumulate at least $10,000 in a bank account if you deposit $500 initially and $500 at the end of each year, if the account pays 5 percent annual interest. 2. Use while loops to perform bubble sort.