Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Introduction to Matlab
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
MatLab – Palm Chapter 4, Part 3 For and While Loops
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Introduction to MATLAB
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/2009.
Matlab Basics IEF 217a: Lecture 2 Fall 2002 Sigmon and Davis, chapters 1-7.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
INTRODUCTION TO MATLAB LAB# 01
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
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 for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Matlab for Engineers Logical Functions and Control Structures Chapter 8.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
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.,
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 3 - Matlab Introduction CVEN 302 June 7, 2002.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
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.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
1 Structured Programming EEN170 Programming in MATLAB.
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.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Computer Application in Engineering Design
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
JavaScript: Control Statements.
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab LAB 4
Computer Science Core Concepts
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.
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:

Bubble Sort Code – For Loop

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.