MATLAB Programming Lecture Notes: Week 4 - Looping in MATLAB - Prof. Mr Burchell, Study notes of Agricultural engineering

These lecture notes from week 4 of the engineering analysis and matlab course cover the concepts of looping in matlab, including the syntax and examples of for-end and while-end loops. The notes also include activities and practical examples.

Typology: Study notes

Pre 2010

Uploaded on 03/18/2009

koofers-user-nui
koofers-user-nui 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
BAE 200
Week 4 Lecture notes:
9/16/08
MATLAB Programming
(Engineering Analysis and MATLAB 7 - Chapter 8 p 233-240)
Goals:
1. Comments about Homework #2
2. Present concepts of Looping and examples
Notes:
-When we started MATLAB, we started with the SEQUENCES, which were just a series
of commands executed one after another
-Last week we introduced a selection structure that allowed for relational or logical
operators to drive the direction in which the program proceeds
-Examples of these selection structures?
-If we need a program to proceed multiple times consider using a Repetition Structure
Repetition Structures - also known as a loop, causes a group of statements to be executed
multiple times if needed. The number times executed depends on a counter or a logical
condition. Understanding looping is required for fundamental understanding of
programming !
Loop can be executed 0,
1, or multiple times
depending on a counter
or logical condition
pf3
pf4

Partial preview of the text

Download MATLAB Programming Lecture Notes: Week 4 - Looping in MATLAB - Prof. Mr Burchell and more Study notes Agricultural engineering in PDF only on Docsity!

BAE 200

Week 4 Lecture notes: 9/16/

MATLAB Programming (Engineering Analysis and MATLAB 7 - Chapter 8 p 233-240)

Goals:

  1. Comments about Homework #
  2. Present concepts of Looping and examples

Notes: -When we started MATLAB, we started with the SEQUENCES, which were just a series of commands executed one after another

-Last week we introduced a selection structure that allowed for relational or logical operators to drive the direction in which the program proceeds

-Examples of these selection structures?

-If we need a program to proceed multiple times consider using a Repetition Structure

Repetition Structures - also known as a loop, causes a group of statements to be executed multiple times if needed. The number times executed depends on a counter or a logical condition. Understanding looping is required for fundamental understanding of programming!

Loop can be executed 0, 1, or multiple times depending on a counter or logical condition

  • Loops: Two types

ƒ For-end loops (p 233): Used when you know how many times you need to repeat a set a commands. ¾ SYNTAX For index = expression (generally a vector, and the statements are repeated as many times as there are columns in the expression matrix) MATLAB statements (instructions that will be repeated) end ¾ Any variable can be used as a loop index statement, but k is often used ¾ k is an integer ¾ It is in the form k = initial value:increment:limit value (looks like when we generate a vector matrix).

  • Examples
  • if we consider k = 1:2:100 the value of k can be 1,3,5,7,….
  • k = 0:2:100, the value of k can be 0,2,4,6,8…..
  • k = 0:5:100, the value of k can be 0,5,10,15….
  • k=1:100 - notice no value for increment used. If no value used for the increment - MATLAB assumes this value to be 1, so k= 1,2,3,4….100.

¾ Activity #1 - p 233. Demonstrate a for loop to calculate 5 raised to the 100 th power total = 1; %sets an initial condition for k=1: total = total*5 %do not suppress the output here to see how it runs end

  • (^) Now suppress the output of total, then type total. It should give you the final

answer of 5^100

  • What is the value of the index at the completion of the loop?

¾ Study the for loop rules p 234

Assume this Program starts at 8 am at Kure Beach

time=8 % sets the initial condition - 8 means 8:00 am while (time<=15) % 1500 means 15:00 hours or 3:00pm if rain >= 0. Collect sample Deliver sample to Tritest Shut off power to sampler Leave power on to measure flow time= end time=time+

end

Go home to escape Hanna

End of program

More in-depth discussion of for loop indexing if time permits