220 likes | 357 Views
COMP 116: Introduction to Scientific Programming . Lecture 3: Variables and Arrays. Recap. You can use MATLAB as a calculator Operators, math functions, etc. 20 + 30, sin(pi/2), etc. Assignment: < variableName > = < expresssion > x=3 y=4 z = sqrt ( x^2 + y^2 ) .
E N D
COMP 116: Introduction to Scientific Programming Lecture 3: Variables and Arrays
Recap • You can use MATLAB as a calculator • Operators, math functions, etc. • 20 + 30, sin(pi/2), etc. • Assignment: • <variableName> = <expresssion> • x=3 • y=4 • z = sqrt( x^2 + y^2 )
What is Assignment?<variableName> = <expression> • Assigning a value to a storage location in computer memory. • Variable name on left-hand side • Expression on right-hand side • Expression is evaluated and reduced to a single value • Value is stored in storage location associated with variable name
Review • Variables • Arrays
What is a Variable? • A user defined name to represent a piece of memory for storing evaluated value(s). • A variable consists of 5 items Name: Meaningful human readable name How the user refers to variable Value: Actual value associated with variable Stored in memory Data Type: How to interpret variable for data representation Size: How much storage memory is needed to store data value Can be inferred from data type Storage location: Usually hidden from user by the interpreter or compiler How the computer refers to a variable
Mathematical statement vs. Programming command/definition • Consider the following two lines x = 1 x = x + 1 • What does the second statement mean? (a) Mathematically? • Makes no sense! (always false) (b) Programmatically? • Read value of x out of storage location • Add one to this value • Store new value back into x’s storage location • Decrement works the same way • x = x - 1
Variable Naming RulesUse Valid Names • Length • 1 to 63 characters long • If you type more than 63 characters, the characters past 63 are ignored • namelengthmax for actual length • Syntax • Starts with a single letter followed by any number of letters, digits, or underscores. • Digits [0-9], Letters [a-z, A-Z], Underscore ‘_’ • No spaces allowed Often, use underscores (or CamelCase) instead of spaces: • life_the_universe_and_everything_else = 42; • lifeTheUniverseAndEverythingElse = 42;
Naming Rules (contd.) • Avoid Keywords (if, else, while, for, …) • Result: Error • If in doubt, use iskeyword function • Avoid Function names (sin, abs, …) • Result: Hiding of useful functions • Use clear to eliminate problem names • Use which to find out if a variable name is already in use • Use meaningful names • currentStudent better than a, whateva, or currItem • Write readable names • currentStudent better than (cS, crSt, orcrrStdnt)
What is an Expression? • A mathematical sequence of operators, functions, variables, numbers, and parenthesis that evaluates to a value • Examples: • 7 • 5*(4+3) • sin(2*pi) • sqrt(pi) + tan(exp(2)) - 7 • 23 + sqrt( -1 ) / (4 – 4i)
Relational Operators • Type doc ops at prompt for more info • Useful for conditional logic in programming,
Logical Operators • Type doc ops at prompt for more info • Useful for binary logic and expressions in programming,
Exercise • What do the following do: • x=2 • x=x^2+3 • x==8 • x==7 • x<9 • x>5 • y=(x<9) & (x>5)
Review • Variables • Arrays
Regular variables and array variables • Regular(scalar) variable x • Array(vector) variable y
Creating array variables >> x=[5 3 2 11 6 5 2] >> x=[5, 3, 2, 11, 6, 5, 2] Using the colon operator >> x= -5:8 Random array >> x=rand(1,8)
Array-Scalar arithmetic • x=[5 3 2 11 6 5 2] • x+2 • x*5 • y=x/5+2
Array-Array arithmetic (use dot operator) • x= -5:8 • y= 7:20 Both arrays are the same size • x+ y • x .* y • z=x.*y + y +2
Exercise: Solving math equations • Solve the MATH equation • (not a programming command) • Programming: >> x= -6:6 >> y=x.*x -6*x+8 find 0 in the y array >> plot(x,y)
Practice • using assignment, variables, and expressions <variable> = <expression> • solving simple one variable equations using arrays