1 / 22

COMP 116: Introduction to Scientific Programming

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 ) .

iorwen
Download Presentation

COMP 116: Introduction to Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 116: Introduction to Scientific Programming Lecture 3: Variables and Arrays

  2. 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 )

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

  4. Review • Variables • Arrays

  5. 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

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

  7. <variableName> = <expresssion>

  8. 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;

  9. 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)

  10. <variableName> = <expresssion>

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

  12. Relational Operators • Type doc ops at prompt for more info • Useful for conditional logic in programming,

  13. Logical Operators • Type doc ops at prompt for more info • Useful for binary logic and expressions in programming,

  14. Operator Precedence (Full)

  15. 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)

  16. Review • Variables • Arrays

  17. Regular variables and array variables • Regular(scalar) variable x • Array(vector) variable y

  18. 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)

  19. Array-Scalar arithmetic • x=[5 3 2 11 6 5 2] • x+2 • x*5 • y=x/5+2

  20. 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

  21. 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)

  22. Practice • using assignment, variables, and expressions <variable> = <expression> • solving simple one variable equations using arrays

More Related