ENGR 1202: Introduction to Engineering Principles and Practice II 3. Adding Program Control to Scripts - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

ENGR 1202: Introduction to Engineering Principles and Practice II 3. Adding Program Control to Scripts

Description:

Strings are row vectors of type character: phrase = this class ... Multiplication, division, raising to power -- put dot in front to get it done entry-wise ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 20
Provided by: jwhi2
Category:

less

Transcript and Presenter's Notes

Title: ENGR 1202: Introduction to Engineering Principles and Practice II 3. Adding Program Control to Scripts


1
ENGR 1202 Introduction to Engineering
Principles and Practice II3. Adding Program
Control to Scripts
  • Jim Bowen
  • Dept. of Civil Engineering
  • UNC Charlotte
  • August 28, 2001

2
Class 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

3
Todays 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

4
Using 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

5
Strings, 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

6
More 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

7
More 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

8
More 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)

9
More 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

10
In-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

11
Making 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)

12
Making 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

13
Adding 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,

14
Programming Looping
  • Loops are used to repeatedly execute commands
  • Two types of loops
  • Variable controlled loops (for) p.10
  • Relation controlled loops (while) p.11

15
The 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

16
The 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

17
The 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


18
In-class script 3.2
  • Count the numbers between 10 and 55 that are
    evenly divisible by 6 (that is, rem(,6) 0)

19
An 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
Write a Comment
User Comments (0)
About PowerShow.com