Title: ENGR 1202: Introduction to Engineering Principles and Practice II 3. Adding Program Control to Scripts
1ENGR 1202 Introduction to Engineering
Principles and Practice II3. Adding Program
Control to Scripts
- Jim Bowen
- Dept. of Civil Engineering
- UNC Charlotte
- August 28, 2001
2Class Announcements
- Look for class announcements on the web
- 1st assignment deadline extended to 7 PM
- Help session added 5-7 PM, Smith 229
- Script added for submitting homework hw_submit
3Todays Lecture
- Using check_script
- Strings and String Functions
- More on Expressions and Vectors
- In-class script
- Adding Interaction, Program Control
- input and disp
- Loops (for, while)
- Branching (if elseif else)
- Logical Expressions
- An example script
4Using check_script
- Steps in grading your own script
- Write your script and save w/ .m extension (no
blanks, no funny characters) - Enter get_script at MATLAB command line
- Enter assignment number
- Enter MOSAIC username (must be exact)
- Select file to check
- Run script, note grade
- Example using assignment_one.m
5Strings, String functions (p. 4, 111)
- Strings are row vectors of type character
- phrase this class sucks! (1 x 17 char.
Vector) - Functions are available just for strings (p. 111)
- lower, upper - converts case
- strcmp - compare 2 or more strings
- sort - sorts characters based on ASCII code
- Examples script on web - show_strings.m
- phrase sort(phrase) note columns 1 2 are
blanks - strcmp(bowen,Bowen) ans 0
(false) - strcmp(bowen,lower(Bowen)) ans 1
6More on Expressions - Scalars
- Size(scalar) 1,1 ex3_0.m
- All the standard arithmetic operations are
available for scalars - addition
- - subtraction
- multiplication
- / division
- raise to a power
7More on Creating Vectors
- Colon notation can be used for creating vectors
- A 15 same as A 1 2 3 4 5
- B 11 0.5 12.5 same as B 11 11.5 12.0
12.5 - All the standard arithmetic operations are also
available for scalars applied to vectors - addition - subtraction
- Multiplication / division
- raise to a power
8More on Expressions - Vectors
- Examples (ex3_1.m)
- A 3 1 2 3 4 5 (column vector)
- B sqrt( 2 1 2 3 4 5) (row vector, w/
func.) - C 200 .5 210 sin( 3/2 pi)
9More on Expressions - Vectors
- Operations may also be performed entry-wise
- Addition and subtraction are always entry-wise
- Multiplication, division, raising to power -- put
dot in front to get it done entry-wise - Examples (ex3_2.m)
- A 1 2 3 6 7 10 A 7 9 13
- B 1 2 3 . 1 2 10 B 1 4
30 - C 1 2 3 . 3 C 1
8 27
10In-class Script 3_1
- In the last 300 years the worlds human
population has risen from 0.5 to 4.0 billion.
This corresponds to an exponential growth rate, k
.00693 yr-1. Assume exponential growth to
calculate population for every year between 2000
and 2100. - Equation P P0 exp (k t), t time
- Assume t year-2000, P0 5.0, k 0.00693
- Solution ics3_1.m
11Making Your Programs Useful
- Congratulations! You are a computer programmer!
- Your programming toolbox is missing a few
things - Using the commands you know now, your programs
will be - Unable to interact with user (no inputs)
- Many lines long (no looping)
- Not very flexible (no branching)
12Making Your Programs Interactive
- Use input to get numbers and strings from
screen - x input(Enter a number )
- (x set to input value)
- Use disp to display strings and number to
screen - disp(x) (displays value w/o any
text) - disp( x ,num2str(x)) (display w/ text)
- Example ex3_3.m
13Adding Program Controls (p. 10)
- Useful programs are flexible
- Useful programs do a lot with few lines of code
- Program controls make these features possible
- Two types of program control
- Looping (for, while) p.10, p.11
- Branching (if) p.12
- Other commands available (I rarely use these)
- switch - case - otherwise,
14Programming Looping
- Loops are used to repeatedly execute commands
- Two types of loops
- Variable controlled loops (for) p.10
- Relation controlled loops (while) p.11
15The for command (ex3_4.m)
- for i 1 2 (i loop goes from 1 to 2 by 1)
- for j 1 3 (j goes from 1 to 3 by 1)
- a(i,j) i 2 / (j 2)
- disp( i,j, a(i,j),int2str(i),,,int2s
tr(j),,,num2str(a(i,j))) - end
- end
- Indenting is essential to avoid errors
- For block terminated with end
- Nested loops possible, looping goes in to out
16The while command (ex3_5.m)
- i 1 i is
initialized - while i lt 2 termination controlled by
expression - j 1 j is initialized
- while j lt 3
- a(i,j) i 2 / (j 2)
- disp( i,j, a(i,j),int2str(i),,,int2
str(j),,,num2str(a(i,j))) - j j1 j is
incremented by 1 - end
- i i_ 1 i is
incremented by 1 - end
- Termination, nesting, indentation same as before
17The if - else - end structure (p. 12)
- If logical expression 1 (e.g. i1 or j lt
2 ) - execute this if logical expression 1 is true
- elseif logical expression 2 (optional)
- execute this if 1 is false and 2 is true
- else (also
optional) - execute this otherwise
- end (ex3_6.m)
- For every for, while, and if there must be a
matching end
18In-class script 3.2
- Count the numbers between 10 and 55 that are
evenly divisible by 6 (that is, rem(,6) 0)
19An example script lmad.m
- Shown last time as simulation of Monty Hall
Problem - Things to see in script
- Comment lines (start with )
- Variable assignments
- Logical expressions
- Display of strings using disp
- Script available on web lmad.m