Download presentation
Presentation is loading. Please wait.
1
R. Smith - University of St Thomas - Minnesota
Today’s Class Recap Homework Running Matlab Arithmetic Expressions Talking about syntax Variables Notational alternatives Fibonacci numbers 9/20/2018 R. Smith - University of St Thomas - Minnesota
2
Computer Fundamentals
CPU – RAM – Hard Drive CPU does the work RAM holds the information (“working storage”) Programs to follow (“execute”) Data to process (“inputs”) Results of the processing (“outputs”) Hard Drive provides large capacity “permanent storage” Programs – sequences of instructions Instructions are absurdly simple (“add 2 numbers”) It takes THOUSANDS to create an interesting program It takes MILLIONS to create a ‘computer desktop’ 9/20/2018 R. Smith - University of St Thomas - Minnesota
3
R. Smith - University of St Thomas - Minnesota
Homework Matlab text - Kaplan Read Chapters 1 through 3. We will cover 1.3 in more detail later Do Exercises 2.1 through 2.4 (pp 32-33) Due at the start of class, Monday. Do Exercises 3.1, -2, -3, -4, -7, -8, -9 (pp 61-63) Due at the start of class, Wednesday 9/20/2018 R. Smith - University of St Thomas - Minnesota
4
MATLAB and the computer
For now, we focus on the CPU and RAM (There’s a hard drive, but we’ll ignore it for now) When we start MATLAB, it is read into RAM The program resides in RAM MATLAB operations use data from RAM Input data resides in RAM MATLAB stores its results in RAM We store the outputs and results in RAM 9/20/2018 R. Smith - University of St Thomas - Minnesota
5
R. Smith - University of St Thomas - Minnesota
Starting Up Matlab Tour of the windows Invoking a Computation (Ch. 2) Expressions Add, subtract, multiply, divide, dot notation 2+3 8/2 10^3 Evaluating an Expression The value of an expression 9/20/2018 R. Smith - University of St Thomas - Minnesota
6
R. Smith - University of St Thomas - Minnesota
Compound expressions “Simple” expressions – one operator “Compound expressions – multiple operators Substitute any number with an expression that yields a number At least, that’s what is allowed by the language Issues Ambiguities Order of evaluation When it’s ambiguous, use parentheses 9/20/2018 R. Smith - University of St Thomas - Minnesota
7
R. Smith - University of St Thomas - Minnesota
Syntax Options What does syntax mean, anyway “Infix” notation, like 2+3 “Functional style” notation, like plus(2,3) Examples: Calculate a hypotenuse sqrt(5^2 + 6^2) sqrt(plus(power(5,2),power(6,2))) 9/20/2018 R. Smith - University of St Thomas - Minnesota
8
Language and Meta-Language
When I give language examples, I need to distinguish between literal language elements (words, symbols) and “meta” elements that may take on any of a variety of possibilities Example of Addition: number + number Literal values are bold Meta elements are underlined words 9/20/2018 R. Smith - University of St Thomas - Minnesota
9
Meta Language for Notations
Infix Notation number symbol number number = any properly formed number symbol = character for arithmetic operator: + - / * ^ Functional Notation operator ( number , number ) operator = name of arithmetic operator 9/20/2018 R. Smith - University of St Thomas - Minnesota
10
‘Properly formed’ number
A number may start with a sign (optional) Signs are ‘+’ or ‘-’ Next comes a ‘decimal number’ Consists of 1 or more digits Optionally, one decimal point ‘.’ may appear May be at the start, middle, or end Next, may come an ‘e’ to specify exponent ‘e’ is followed by ‘+’ or ‘-’ sign (optional) 1 or more digits (at least 1 required) Examples: 123, 2.4, .5, -1, -.23, 1e-2 The numeric value is then used in expressions Non-numbers .e2, -e2, 0e, -23e4.5, 1e.5 9/20/2018 R. Smith - University of St Thomas - Minnesota
11
What about Expressions??
Infix Notation expression symbol expression expression = any properly formed expression symbol = character for arithmetic operator: + - / * ^ Functional Notation operator (expression , expression ) operator = name of arithmetic operator 9/20/2018 R. Smith - University of St Thomas - Minnesota
12
Elements of Functional Notation
Function Name Follows “variable name” rules (described later) Identifies a function defined elsewhere Parentheses enclose “argument list” Also called the “parameter list” Number of arguments? Some functions demand a precise number power(2,5) Some functions are flexible – take an arbitrary number Common in C, but not in Matlab 9/20/2018 R. Smith - University of St Thomas - Minnesota
13
R. Smith - University of St Thomas - Minnesota
Text vs. Numbers Matlab lets you do calculations on TEXT Text is indicated by single quote marks Numbers in text are DIFFERENT They do not represent a numeric value They represent the numeric value of inidivual CHARACTERS We see this when doing arithmetic on text Examples ‘0’ + 2 ‘1’ + 1 ‘A’ + 4 ‘C’ + 2 9/20/2018 R. Smith - University of St Thomas - Minnesota
14
R. Smith - University of St Thomas - Minnesota
Variables Lets us save intermediate or final results Assignment statement name = expression Examples: add some numbers, square root, multiply Variables let us easily identify locations in RAM We can use variables anywhere we can use numbers or expressions Variables, numbers, and expressions all reduce to numbers They are syntactically interchangeable 9/20/2018 R. Smith - University of St Thomas - Minnesota
15
R. Smith - University of St Thomas - Minnesota
What’s really going on? The appearance We evaluate an expression We save the result in a variable The implementation We do a series of arithmetic calculations We save the final result in RAM 9/20/2018 R. Smith - University of St Thomas - Minnesota
16
Good vs Bad Assignments
Legitimate variable name Variable name left of = Expression right of = Single arithmetic expression Expression calculates what you expect (disambiguated) Bad Bogus variable name Badly formed Using the wrong variable Poorly named Expression left of = 9/20/2018 R. Smith - University of St Thomas - Minnesota
17
“States” and saving results
In high school algebra, a mathematical expression does not normally have a “state” You “evaluate” an expression It may have a value or result It may be ‘true’ or ‘false’ Computations have state You “perform” a computation – it goes through a set of steps, and each step has intermediate results The intermediate and final results reflect the “state” “State” indicates that we are remembering a result, saving it in memory (RAM), for example 9/20/2018 R. Smith - University of St Thomas - Minnesota
18
R. Smith - University of St Thomas - Minnesota
Naming Variables Syntax of a name Any combination of upper- or lower-case letters Digits, as long as it’s not the first character May contain _ as in rick_smith No ‘special’ characters (operators, syntax markers) Certain keywords can’t be used If, else, elseif, while, end, for, break, switch, case, otherwise, try, catch, return, global, function, persistent Suggestions Mnemonic – suggests the variable’s content and use Not too long – make them understandable but type-able Not misleading – don’t name Sqrt3 = sqrt(2) Avoid ‘i, j’ – used with imaginary numbers Use typical, mundane names where appropriate 9/20/2018 R. Smith - University of St Thomas - Minnesota
19
R. Smith - University of St Thomas - Minnesota
Fibonacci numbers: Leonardo of Pisa’s book on algebra Alias “Filius Bonacci” shortened to Fibonacci Defined an ‘interesting sequence’ of numbers Converges on the Golden Section Ratio appearing in nature and in Greek architecture Algorithm: Next number in the sequence = sum of previous two numbers 3 Variables New (“next”), Previous, Final new = previous + final 9/20/2018 R. Smith - University of St Thomas - Minnesota
20
Notation Alternatives
These show up in various computing systems Infix – Matlab, C, Fortran, Java, etc. A + (B – C) Functional-style – Matlab, C, Fortran, Java, etc. *** plus (A, minus (B, C)) Prefix notation – Lisp and Scheme (plus a (minus b c)) Postfix notation – Forth, RPN calculators B C – A + 9/20/2018 R. Smith - University of St Thomas - Minnesota
21
*** Functional Notation in C, …
Most languages use it for “most” functions Library functions, more advanced math functions, etc. Log, exp, sine, cosine, etc. Input/output Most languages provide infix operators ONLY You must use x+y, doesn’t provide plus(x,y) Matlab has built-in functional forms for all arithmetic operations There are weird cases that require the functional forms 9/20/2018 R. Smith - University of St Thomas - Minnesota
22
Working Notation Problems
“Walking the parentheses” First, match the parentheses Do the inside ones first Postfix x 2 power sin x 2 power cos + 9/20/2018 R. Smith - University of St Thomas - Minnesota
23
Exercise: Notation Conversion
Mostly infix: sqrt(a^2, b^2) Functional: div(plus(a,plus(b,plus(c,plus(d,e))),5) Prefix: (sqrt (minus (power b 2) (times 4 (times a c))))) Postfix: a x 2 power times b x times plus c plus 9/20/2018 R. Smith - University of St Thomas - Minnesota
24
R. Smith - University of St Thomas - Minnesota
Parsing A computing term for analyzing input text Tokens – discrete chunks of the input text Operators: + - .* and so on Assignment statement = Parentheses … and more we’ll see later Identifiers – tokens that name things Function names and variable names White space – spaces, tabs, other ‘blanks’ Generally skipped / ignored in parsing 9/20/2018 R. Smith - University of St Thomas - Minnesota
25
R. Smith - University of St Thomas - Minnesota
More on Parsing Newline ‘token’ marks end of a line Each line must contain a syntactically complete statement Continuation with … Atomic tokens Tokens that have a meaning all by themselves Parentheses, operators, = No atomic tokens allowed in a variable name Character Strings Text delimited by single quotes (‘) Example: ‘This is an example character string’ Parsing rules are different ‘inside’ the string Semicolon ‘;’ separates statements 9/20/2018 R. Smith - University of St Thomas - Minnesota
26
R. Smith - University of St Thomas - Minnesota
Parsing Examples A = sqrt( b^2, c^2) Average = ( two four + 5) / five Thing = (24/foo)/bar 9/20/2018 R. Smith - University of St Thomas - Minnesota
27
R. Smith - University of St Thomas - Minnesota
Chapter 2 Homework Read Chapters 1 through 3. We will cover 1.3 in more detail later Do Exercises 2.1 through 2.4 – P. 32 Due at the start of class, Monday. 9/20/2018 R. Smith - University of St Thomas - Minnesota
28
R. Smith - University of St Thomas - Minnesota
Storing Numbers in RAM Numbers stored in bits – binary numbers 8 bits = byte (4 bits = nibble, 2 bits = ?) Number of bits -> power of 2 -> number range Old calculator Number of digits -> power of 10 -> number range 9/20/2018 R. Smith - University of St Thomas - Minnesota
29
R. Smith - University of St Thomas - Minnesota
Number “Formats” What about signs, decimal points, exponents? Signed integers = 1 bit sign, rest magnitude Floating point numbers Part set aside for mantissa (‘value’ part) Part set aside for exponent (‘power’ part) In Matlab we treat most things as floating point 0 ./ 0 = infinity 9/20/2018 R. Smith - University of St Thomas - Minnesota
30
R. Smith - University of St Thomas - Minnesota
Chapter 3 Homework Read Chapters 1 through 3. We will cover 1.3 in more detail later Do Exercises 3.1, -2, -3, -4, -7, -8, -9 Due at the start of class, Wednesday 9/20/2018 R. Smith - University of St Thomas - Minnesota
31
Creative Commons License
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 9/20/2018 R. Smith - University of St Thomas - Minnesota
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.