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
Definition A variable is a name to a memory location. The data stored in that location can, of course, VARY at any time Catia Project MATLAB Software Variable named area Variable named base
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” 3
Example 4 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
Example 5 Area of triangle 2) Diagram 3) Assumptions lengths are in meters 4) Theory area = ½ * base * height height = side * sin(angle) h
Example 6 Area of triangle 5) Solve 6) Verify Accuracy Hard 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) Algorithm Define base, side and angle Calculate area Show result
BAD code Why would this be a bad code? Absolutely NOTHING is re- usable 7
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, or 5.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. 8
Best habit to naming a variable The name should simply represent its content. 9
Avoid single letters 10
Final Script File 11
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: 12
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? 13 z = x + y 10 = x + 7 Solve for x, obtain _____ Look at MATLAB:
Basic Data Manipulation 14 Assign values to variables z and y
Basic Data Manipulation 15 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.
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. 16 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.
“A scalar” A single value. Example: the number 7 is a scalar. age = 17 %creates a scalar variable weight = %another scalar variable 17
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? ||||+= (in Futurama Alien Language 1)
Definition – a DATA TYPE Simply put: "The type-of-data stored in a variable" 19
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 phoneA couple GB (4, 8, 16) TabletsAround 32GB LaptopsAround 640 GB DesktopUp in the Terabytes (TB) now! 20
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. 21
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.
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'); 23 Use of input: Here we use the word “input” to mean a specific function that collections information from the user.
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.
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
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 2 nd argument is present, it must be the letter 's' and no other letter! 26 1 st argument 2nd argument For this function, the second argument tells the input() function to expect a string from the user.
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?
Next chapter? 28 How do we display the results to the user…? ….in a clean & professional format?