Chapter 4 MATLAB Programming

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Computer Science 1620 Programming & Problem Solving.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Linear Simultaneous Equations
Non-Linear Simultaneous Equations
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Working with Arrays in MATLAB
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.
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:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 10 Section 8.1 Recursive Thinking Page 409
Time Value of Money 1: Analyzing Single Cash Flows
ECE 1304 Introduction to Electrical and Computer Engineering
REPETITION CONTROL STRUCTURE
Chapter 7 Matrix Mathematics
Chapter 2 Excel Fundamentals
Repetition Structures Chapter 9
© 2016 Pearson Education, Ltd. All rights reserved.
Topics Introduction to Repetition Structures
Accuracy and Precision
Chapter 4 MATLAB Programming
Chapter 5: Control Structure
Basic operations in Matlab
Computer Science 101 While Statement.
Copyright © The McGraw-Hill Companies, Inc
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 3 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Please use speaker notes for additional information!
MATH 493 Introduction to MATLAB
Alternate Version of STARTING OUT WITH C++ 4th Edition
MATLAB – Basic For Loops
Plotting Data with MATLAB
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter R A Review of Basic Concepts and Skills
Communication and Coding Theory Lab(CS491)
Chapter 6: Repetition Statements
Chapter 6 Lesson 4 PMT Function.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 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.
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
Topics Introduction to Repetition Structures
Chapter 4: Repetition Structures: Looping
Copyright © The McGraw-Hill Companies, Inc
Matlab Basics Tutorial
Working with Arrays in MATLAB
Chapter 3 Introduction to Physical Design of Transportation Facilities.
Presentation transcript:

Chapter 4 MATLAB Programming Loops (for while) Chapter 4 MATLAB Programming Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1

The for Loop in MATLAB In MATLAB, a for loop is used to repeat statements multiple times: number = 3; count = 0; for i = 1:number; count = count + i; end; count Displayed in command window: count = 6 Engineering Computation: An Introduction Using MATLAB and Excel 2

The for Loop in MATLAB In MATLAB, a for loop begins with the statement indicating how many times the statements in the loop will be executed A counter is defined within this statement Examples: for k = 1:100 (counter = k, the loop will be executed 100 times) for i = 1:2:7 (counter = i, the counter will be incremented by a value of 2 each time until its value reaches 7. Therefore, the loop will be executed 4 times (i = 1,3,5, and 7) Engineering Computation: An Introduction Using MATLAB and Excel 3

The for Loop in MATLAB The loop ends with an end statement In M-files, the MATLAB editor will automatically indent text between the for and end statements: Can you determine what the variable x will be after running this M-file? Engineering Computation: An Introduction Using MATLAB and Excel 4

for Loop Example The first time through the loop, j = 1 Because of the single value in parentheses, x will be a one-dimensional array x(1) will be set equal to 5*1 = 5 The second time through the loop, j = 2 x(2) will be set equal to 5*2 = 10 This will be repeated until j = 10 and x(10) = 50 Engineering Computation: An Introduction Using MATLAB and Excel 5

for Loop Example x will be a one-dimensional array (a row matrix) with 10 elements: Engineering Computation: An Introduction Using MATLAB and Excel 6

for Loop in Interactive Mode Loop commands can be entered directly from the command prompt The calculations are not performed until the end statement is entered Engineering Computation: An Introduction Using MATLAB and Excel 7

for Loop in Interactive Mode Remember that if you leave off the semi-colon, the results of the calculations will be written to the screen in every loop: Engineering Computation: An Introduction Using MATLAB and Excel 8

for Loop Examples What result will be output to the screen in each of the following examples? y = 0; for k = 1:5 y = y + k; end y Engineering Computation: An Introduction Using MATLAB and Excel 9

for Loop Examples y = 0; for k = 2:2:8 y = y + k; end y Engineering Computation: An Introduction Using MATLAB and Excel 10

for Loop Examples for k = 1:5 y(k)=k^2; end y Engineering Computation: An Introduction Using MATLAB and Excel 11

for Loop Examples for j = 1:3 for k = 1:3 T(j,k) = j*k; end T Engineering Computation: An Introduction Using MATLAB and Excel 12

for Loop Example Consider this equation: Plot this equation for values of x from -10 to 10 We will use a for loop to calculate and store x and y values in one-dimensional arrays Engineering Computation: An Introduction Using MATLAB and Excel 13

for Loop Example for i = 1:21 x(i) = -10 +(i-1); y(i) = 2^(0.4*x(i)) + 5; end After running these lines of code, two one- dimensional arrays, x and y, have been created, each with 21 elements Engineering Computation: An Introduction Using MATLAB and Excel 14

Plot Command The stored arrays can be plotted with the command: plot(x,y) Any two one-dimensional arrays can be plotted, as long as they are exactly the same size The plot will be created in a new window We will learn how to format MATLAB plots later Engineering Computation: An Introduction Using MATLAB and Excel 15

for and while Loops in MATLAB A for loop will be executed a fixed number of times A while loop will continue to be repeated until some condition is satisfied Examples: Consider an amount of money deposited in an interest-bearing account If we want to calculate the value in the account after 10 years, then we would use a for loop If we want to determine how long it will take until the account reaches $100,000, then we would use a while loop Engineering Computation: An Introduction Using MATLAB and Excel 16

Example Consider this loop: How many times will the loop be executed? k = 0; while k < 10 k = k + 2 end How many times will the loop be executed? Initially, k = 0, so the loop is entered Pass #1: k = 2, so execution continues Pass #2: k = 4, so execution continues Pass #3: k = 6, so execution continues Pass #4: k = 8, so execution continues Pass #5, k = 10, so k is not less than 10 and execution ends Engineering Computation: An Introduction Using MATLAB and Excel 17

while Example Suppose you borrow $10,000 at an interest rate of 6%, compounded monthly. Therefore, each month, the amount owed increases by 0.5% (6% divided by 12) and decreases by the amount of the monthly payment that you make How many months will it take to completely pay back the loan, if the monthly payment is $500 $200 $100 Engineering Computation: An Introduction Using MATLAB and Excel 18

m-File Note the use of the input command: P is assigned the value entered at the prompt given in single quotes Engineering Computation: An Introduction Using MATLAB and Excel 19

Results Payment = $500: The loan would be repaid in 22 months After the 22nd payment, the balance of $437 would be returned to the borrower Engineering Computation: An Introduction Using MATLAB and Excel 20

Results Payment = $200 Payment = $100 Engineering Computation: An Introduction Using MATLAB and Excel 21

Results Try Payment = $45: The calculations will continue until you press ctrl+C to stop execution Engineering Computation: An Introduction Using MATLAB and Excel 22

Results Check m and B after stopping the calculations: After making payments for more than 1,000,000 years, you owe more money than MATLAB can calculate. This won’t look good on your credit report! Engineering Computation: An Introduction Using MATLAB and Excel 23

Infinite Loops When using a while loop, there is a danger of encountering an infinite loop Since termination of the loop is dependent upon achieving some condition (in this case, a balance of less than or equal to zero), it is possible that the condition will never be reached, and therefore the looping will continue endlessly In our example, the interest after the first month was $50 (1/2% of $10,000). If the payment was less than $50, then the balance increased every month Engineering Computation: An Introduction Using MATLAB and Excel 24

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 20; while A <= 50 A = A + 5; m = m + 1; end A m Engineering Computation: An Introduction Using MATLAB and Excel 25

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 100; while A > 15 A = A/2; m = m + 1; end A m Engineering Computation: An Introduction Using MATLAB and Excel 26

Practice What are the values of A and m after execution of these MATLAB commands: m = 0; A = 10; while A > 0 A = sqrt(A); m = m + 1; end A m Infinite Loop: the square root will never reach zero Engineering Computation: An Introduction Using MATLAB and Excel 27