anatomy of a program variables, assignment, mathematical ... · lecture 2 16 variable &...

33
Lecture 2 1 Previous Lecture: Intro to the course “Computational senses” Running a program in Matlab Today, Lecture 2: Anatomy of a program Variables, assignment, mathematical operations Functions for input & output Writing a program—systematic problem solving Announcements: Set up folders (directories) on your laptop, flash drive, or cloud storage to store course files (see website announcement) Register your clicker or clicker app (see links in Syllabus) See website for office hours and consulting hours First project will be posted after Tue lecture Screen-free zone Remember:

Upload: others

Post on 20-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 1

Previous Lecture: Intro to the course “Computational senses” Running a program in Matlab

Today, Lecture 2: Anatomy of a program Variables, assignment, mathematical operations Functions for input & output Writing a program—systematic problem solving

Announcements: Set up folders (directories) on your laptop, flash drive, or cloud

storage to store course files (see website announcement) Register your clicker or clicker app (see links in Syllabus) See website for office hours and consulting hours First project will be posted after Tue lecture

Scr

een-f

ree z

one

Remember:

Page 2: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 2

Formula

Surface area of a sphere?

Page 3: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 3

Formula

Surface area of a sphere? 24 rA

Page 4: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 4

Formula

Surface area of a sphere?

Have the cosine of some angle in [0, pi/2]

and want cos(/2)?

24 rA

Page 5: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 5

Formula

Surface area of a sphere?

Have the cosine of some angle in [0, pi/2]

and want cos(/2)?

24 rA

2

)cos(1)2/cos(

Page 6: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 7

Interactive computation in Command Window

>> r= 6

r =

6

>> a= 4*pi*r^2

a =

452.3893

>> v= 4/3*pi*r^3

v =

904.7787

Page 7: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 8

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

Page 8: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 9

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

r= input('Enter the radius: ');

Page 9: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 10

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

r= input('Enter the radius: ');

A= 4*pi*r^2;

Page 10: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 11

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

r= input('Enter the radius: ');

A= 4*pi*r^2;

fprintf('Surface area is %f units^2!\n', A)

Page 11: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 12

A computer program

input output

computation

Page 12: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Where does computation happen?

Code lives on a disk (hard drive)

Matlab: Folder pane

Variables live in memory (RAM)

Matlab: Workspace pane

1/27/2020 Lecture 2 13

Evan-Amos CC BY-SA

Page 13: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 14

Variable & assignment

Variable: a named computer memory space for storing a value

r

A

Page 14: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 15

Variable & assignment

Variable: a named computer memory space for storing a value

Valid names start with a letter, can contain digits

Use meaningful variable names!

Create a variable by assigning a value to it

By default, a number has the type (class) double, for “double precision floating point number”

r

A

Page 15: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 16

Variable & assignment

Variable: a named space for storing a value

Assignment: putting a value into a variable

Assignment operator: =

An assignment statement, e.g., r= 2*4.5

Expression on right-hand-side (rhs) is evaluated before the assignment operation

Update variable’s value with another assignment statement, e.g., r= 7

r

Page 16: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 17

Assignment

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1 + x

z= 4^2 – cos(y)

Page 17: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 18

Assignment

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1 + x

z= 4^2 – cos(y)

Question: can we reverse the order of the 3 statements above?

Page 18: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 19

Assignment

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1 + x

z= 4^2 – cos(y)

Question: can we reverse the order of the 3 statements above?

NO! Any variable on the rhs must be initialized.

Page 19: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 20

Assignment

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1 + x

z= 4^2 – cos(y)

Question: can we reverse the order of the 3 statements above?

NO! Any variable on the rhs must be initialized.

Page 20: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 21

Matlab’s built-in functions

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1+x

z= 4^2 – cos(y)

Question: can we reverse the order of the 3 statements above?

NO! Any variable on the rhs must be initialized.

Page 21: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 22

Matlab’s built-in functions

Expression on rhs is evaluated before the assignment operation

Examples:x= 2*3.14

y= 1+x

z= 4^2 – cos(y)

Question: can we reverse the order of the 3 statements above?

NO! Any variable on the rhs must be initialized.

Page 22: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 23

Statements in a program are executed in sequence

% A program fragment ...

x= 2*3.14

y= 1 + x

x= 5

% What is y now?

A: 6 B: 7.28 C: some other value D: error

Page 23: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 25

Script execution(A script is a sequence of statements, an “m-file”)

% Quad1

% Solves x^2 + 5x + 6 = 0

a = 1;

b = 5;

c = 6;

d = sqrt(b^2 - 4*a*c);

r1 = (-b - d)/(2*a)

r2 = (-b + d)/(2*a)

Memory space

1

5

6

1

-3

a

b

c

-2

d

r1

r2

Page 24: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 26

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

r= input('Enter the radius: ');

A= 4*pi*r^2;

fprintf('Surface area is %f units^2!\n', A)

Page 25: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 27

Input & output

variable = input('prompt ');

fprintf('message to print ')

Page 26: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 28

Input & output

variable = input('prompt ')

r= input('Enter radius: ')

fprintf('message to print ')

fprintf('Increase ')

fprintf('is %f inches\n', x)

fprintf('Position (%d,%d)\n', x, y)

Page 27: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 29

Substitution sequences (conversion specifications)

%f fixed point (or floating point)

%d decimal—whole number

%e exponential

%g general—Matlab chooses a format

%c character

%s string

During discussion: Find out how to control the number of decimal places shown with %f

Page 28: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 30

% Example 1_1: Surface area of a sphere

% r: radius of the sphere [unit]

% A: surface area of the sphere [unit^2]

r= input('Enter the radius: ');

A= 4*pi*r^2;

fprintf('Surface area is %f!\n', A)

Page 29: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 31

Comments

For readability!

A comment starts with % and goes to the end of the line

Start each program (script) with a concise description of what it does

Define each important variable/constant

Units, assumptions/constraints

Top a block of code for a specific task with a concise comment

Comment: "What we are trying to do"

Code: "How we are doing it"

Page 30: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 32

Example

Modify the previous program to calculate the increase in surface area given an increase in the radius of a sphere.

Note: 1 mile = 5280 feet

Page 31: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

% Example 1_2: Print surface area increase in

% miles^2 given an increase in the radius

r= input('Enter radius r in miles: ');

delta= input('Enter delta r in inches: ');

1 mile = 5280 feet

Page 32: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Tips for writing a program

Check that you know what is given (or is input, or is assumed)

Be goal-oriented: start by writing the last statement(s) for the program output

What is the program supposed to produce? You know this from the problem statement

Allows you to work backwards from the results

Name as a variable what you don’t know

Helps you break down the steps

Allows you to temporarily skip over any part that you don’t know yet how to do

1/27/2020 Lecture 2 35

Page 33: Anatomy of a program Variables, assignment, mathematical ... · Lecture 2 16 Variable & assignment Variable: a named space for storing a value Assignment: putting a value into a variable

Lecture 2 36

So far, all the statements in our scripts are executed in order

We do not have a way to specify that some statements should be executed only under some condition

We need a new language construct…

What’s next?