Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user...

28
Lecture 5 1. What is a variable 2. What types of information are stored in a variable 3. Getting user input from the keyboard 1

Transcript of Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user...

Page 1: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

1

Lecture 5

1. What is a variable2. What types of information are stored in a

variable3. Getting user input from the keyboard

Page 2: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

2

Definition

• A variable is a name to a memory location. • The data stored in that location can, of course, VARY at any

time.

1101110011001010011100000100110000111000101101001111010111011011101010011011100100111100001101010101101001010111011001100000000010100011111000011110110111100101110000111001011011111111001101110111101001110101110001111101001110110001011001111001110100010000110011110100000001001111111000010010111100111111000111101101101100011011110010001010110101100000111101010101111100111011011011011110011000001000000000000101000000000011100010000010110111101001100111101010110000010100001111000001110011111101000010100101100111001111101001011000100000000101100100101111010111000111101101001011111100100011100111001110101000110001100011001001011101101000110101101010011001011100111110100011111110001111011010000100000100010110110110011101111000000110000001010000010111000110011011010011011100010011000100101

Catia Project

MATLAB Software

Variable named area

Variable named base

Page 3: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

3

An important distinction

• A variable has a name, a value (or multiple values), a type, and a location.

• When you refer to a variable, you typically refer to its name, but you actually mean the value that it currently has.– Example:

y = x + 3

This really means “take the current value of x, add 3, and store that value in a variable named y”

Page 4: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

4

Example Area of triangle

• Problem:Solve the area of a triangle where the base is 12.3, one side is 8.9, and the angle between those two sides are 34 degrees.

1) Summarize the problem– Givens

• base = 12.3• side1 = 89• angle between two sides = 34 degrees

– Find• area of triangle

Page 5: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

5

Example Area of triangle

2) Diagram

3) Assumptionslengths are in meters

4) Theoryarea = ½ * base * heightheight = side * sin(angle)

h

Page 6: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

6

Example Area of triangle

5) Solve

6) Verify AccuracyHard to do but sind(34) is a little more than 0.5, so h would be a little more than 4.5. Using 4.5 would give an area of roughly 28, so something a little more than 28 makes sense.

7a) AlgorithmDefine base, side and angleCalculate areaShow result

Page 7: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

7

BAD code

• Why would this be a bad code?

Absolutely NOTHING is re-usable

Page 8: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

8

Rules to follow when naming variables AND script files

1. It cannot contain spaces: Use_the_underscore, or capitalizeTheFirstLetterOfAdditionalWords

2. It cannot start with a digit. However, it can have digits in the rest of it.3. It cannot contain any special characters (other than the underscore of

course!)4. It should not be any keywords MATLAB knows, or5. It should not be the filename of any saved files

Also:• All variables are case sensitive. AGE is not the same as age.

Strong Advice: Avoid single letters, especially i and j. The name should simply represent its content.

Page 9: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

9

Best habit to naming a variable

• The name should simply represent its content.

Page 10: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

10

Avoid single letters

• b – side of a triangle?– base?– magnetic field?

• v– velocity?– voltage?– volume?

• ESPECIALLY: i and j are imaginary numbers

Page 11: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

11

Final Script File

Page 12: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

12

CAUTION

CAUTION: This isn’t math anymore

• sind() calculates the sine of an angle in degrees• MULTIPLICATION is NOT implied in MATLAB

– in math: (2)(5)(5.5) = ?– in MATLAB:

Page 13: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

13

Basic Data Manipulation

• How about solving equations?

• Normal algebra ≠ programming– Assume the following mathematic equation:

z = x + y

• In algebra: when z is 10 and y is 7, what is x equal to?

z = x + y10 = x + 7

Solve for x, obtain _____ Look at MATLAB:

Page 14: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

14

Basic Data Manipulation

Assign values to variables z and y

Page 15: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

15

Basic Data Manipulation

Assign values to variables z and y

Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

Page 16: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

16

Basic Data Manipulation

In MATLAB, all the known variables must be on the right side of the equal sign before executing the command.

At any time, only one variable can be on the left.

Assign values to variables z and y

Once x is on the left side, and all known variables (z and y) are on the right, MATLAB executes the command.

Page 17: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

17

“A scalar”

• A single value.

• Example: the number 7 is a scalar.

age = 17 %creates a scalar variable

weight = 145.32 %another scalar variable

Page 18: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

18

Basic Data Understanding

• How exactly is the data stored in the memory?• binary (i.e. machine) language: 0 and 1’s

• How is the number 2 represented then?

• Remember that the symbol we see is NOT the value represented by it.

• For example: what does this equal?|||| + =84

5 79 (in Futurama Alien Language 1)

0100000000000000000000000000000000000000000000000000000000000000

Page 19: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

19

Definition – a DATA TYPE

• Simply put: "The type-of-data stored in a variable"

Page 20: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

20

• Size and Weight

• Data-types are means of – controlling how much memory is used per “unit", and– Specifying/defining what kind of operations apply to a variable

Why do data-types matter?

Device “Hard Drive Size”

Apollo 11 Computer

2 KB

Cell phone A couple GB (4, 8, 16)

Tablets Around 32GB

Laptops Around 640 GB

Desktop Up in the Terabytes (TB) now!

Page 21: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

21

Can’t MATLAB figure it out?

• Why do I need to know what type of data a variable holds? Can’t MATLAB figure that out?

– Yes, when it knows up front how much space a value will take up, but you won’t be using hard-coded values all semester.

Page 22: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

User Interface

• Instead of the programmer specifying the values, the program uses an external interface where the values are entered by the user

22

(No longer hardcoded)

Use of input: Here we use the word “input” to mean “information coming into the solver from outside” – input doesn’t always come from the user.

Page 23: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

23

Keyboard interface

• There are built-in functions to get information from the user. • These functions typically ask the programmer to supply

‘prompts’ to clue the user on what is being asked.

input() variableName = input(‘prompt’);

• Two examples given in the MATLAB help:num_apples = input('How many apples? ');name = input('Enter your name: ', 's');

Use of input: Here we use the word “input” to mean a specific function that collections information from the user.

Page 24: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Syntax is data type dependent

• What data type is the user asked for?

– num_apples = input('How many WHOLE apples? ');

The user is asked to provide a number – here, an integer.

– name = input('Enter your name: ', 's');The user is asked to provide a sequence of characters – i.e. a string.

24

These are the only 2 forms of using the input() built-in function.

Page 25: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Form #1. input()- integer

• In the first form, only the prompt is inside the parentheses:

num_apples = input('How many WHOLE apples? ');

There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job.

The one argument is the prompt string:'How many WHOLE apples? '

25

function call argument

Page 26: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Form #2. input(…, 's') - string

• In the second form of the input() function, there are TWO arguments: the prompt string, and another string: ‘s’

name = input('Enter your name: ', 's');

• If the 2nd argument is present, it must be the letter 's' and no other letter!

26

1st argument 2nd argument

For this function, the second argument tells the input() function to expect a string from the user.

Page 27: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Code for the triangle solver

• The program requires two values: a base and a height.

• To collect them from the user:Base = input('Enter base of triangle: ');

Height = input('Enter height of triangle: ');Units = input('Enter unit system chosen: ', 's');

– Without the space it’s not very pretty, and the user may actually press the space bar, which is a problem for strings:

27

Notice the space… why?

Page 28: Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.

Next chapter?

28

How do we display the results to the user…?

….in a clean & professional format?