CS107: Computing for Math CS107: Computing for Math and Science and Science • Instructor: Prof. Louis Steinberg – office: Hill 401 – email: lou@cs.rutgers.edu – Office hours: by appointment • TAs: – Andre Cohen office: CBIM acohen@ cs.rutgers.edu – Xiaoye Han office: Hill 405 xiaoye @ cs.rutgers.edu CS107, Prof. Steinberg, f10 Lecture 02 1
CS107: Computing for Math CS107: Computing for Math and Science and Science • Instructor: Prof. Apostolos Gerasoulis – office: Core 328 – email: grasoul@cs.rutgers.edu – Office hours: by appointment • TA: – Nader Boushehrinejadmoradi Office: Hill 353 naderb@cs.rutgers.edu CS107, Prof. Steinberg, f10 Lecture 02 2
Items posted on web site Items posted on web site • Assignment 1 on Sakai ??? • Practice problems 1 on Remus CS107, Prof. Steinberg, f10 Lecture 02 3
Web Sites Web Sites • http:// http://remus remus.rutgers.edu/cs107 .rutgers.edu/cs107 • – Policies – Syllabus – Lecture notes – etc.... – You are assumed to know anything posted. – Also Sakai site CS107, Prof. Steinberg, f10 Lecture 02 4
Should you take CS107? Should you take CS107? • Math or science major, had calc I: YES • Exceptions: – Computer Science major or minor: NO – Except: unsure about CS: MAYBE – Option B: The Computer-Oriented Mathematics Major: NO CS107, Prof. Steinberg, f10 Lecture 02 5
Kinds of Knowledge Kinds of Knowledge • Knowing that – Knowing a fact, e.g. rules of Chess • Knowing how – Knowing how to use facts to achieve a goal, e.g. how to win at chess CS107, Prof. Steinberg, f10 Lecture 02 6
E.G., Algebra E.G., Algebra • Knowing that If X = Y then X + a = Y + a • Knowing how – To solve 3*x - 5 = 10, start by adding 5 to both sides of the equation 3*x - 5 + 5 = 10 + 5 3*x = 15 CS107, Prof. Steinberg, f10 Lecture 02 7
Learning “ “Knowing How Knowing How” ” Learning • The only way to learn “How” is practice • Only way to learn most of the course material is to do homework • Purpose of lecture and textbook is to prepare you to do this work CS107, Prof. Steinberg, f10 Lecture 02 8
Review Review • What is a computer • Anthropomorphisms – Follow instructions – Process data – Language CS107, Prof. Steinberg, f10 Lecture 02 9
What is a computer? What is a computer? • A machine that follows instructions and processes data CS107, Prof. Steinberg, f10 Lecture 02 10
How can a machine follow How can a machine follow instructions? instructions? • Example: Washing machine controller – Controls hot water, cold water, drain, motor CS107, Prof. Steinberg, f10 Lecture 02 11
Hot Wash, Warm Rinse Hot Wash, Warm Rinse Peg H C D M No Peg CS107, Prof. Steinberg, f10 Lecture 02 12
Machine Instructions Machine Instructions • A pattern of physical objects • Pegs, electrons, etc • Physically causes the specified behavior • Can be thought of as a language • Expresses commands • Made of pieces combined according to rules • Extremely detailed, so need “higher level” language CS107, Prof. Steinberg, f10 Lecture 02 13
Higher Level Language Higher Level Language • Compromise – Easier for humans to deal with – Write, check, understand, … – Still not too hard to make a machine obey CS107, Prof. Steinberg, f10 Lecture 02 14
Data processing Data processing • What Kaplan text calls “computation” • Any process that transforms some data into other data, e.g. addition CS107, Prof. Steinberg, f10 Lecture 02 15
Representation Representation • For a machine to process data, the data must be represented by some physical quantity – Length, voltage, … CS107, Prof. Steinberg, f10 Lecture 02 16
Matlab Matlab • Matlab is: – A programming language – A library of programs and pieces – An Interactive Development Environment (IDE) – A program that helps you build and test programs CS107, Prof. Steinberg, f10 Lecture 02 17
Matlab as a Calculator as a Calculator Matlab • E.g., convert 75 degrees Fahrenheit to Celsius • E.g. convert $100 to Euros ($1 = .79 Eur.) • E.g. wall area of a room 10’ x 12’ x 8’ • Note: precedence • Note: infix vs prefix form • Note: lack of units CS107, Prof. Steinberg, f10 Lecture 02 18
Operations Operations • Plus + 4 + 5 • Minus - 4 - 5 • Times .* 4 .* 5 • Divide ./ 4 ./ 5 • Power .^ 4 .^ 5 • Square root sqrt sqrt(4) • Sin sin sin(4) • … etc. CS107, Prof. Steinberg, f10 Lecture 02 19
Variables Variables • Typically, calculators have a memory; matlab has many memory cells • Memory cell in matlab is called a “variable” • A variable has – a name, e.g. age, year, area, … – a value CS107, Prof. Steinberg, f10 Lecture 02 20
Variables Variables • The first time you type a variable’s name, Matlab: – Reserves a space in the computer’s memory – Associates that space with the name you typed • After that, when you type the name, Matlab uses the associated memory space CS107, Prof. Steinberg, f10 Lecture 02 21
Using Variables Using Variables • To refer to a variable just type its name • Most of the time name means “the value stored there” (degreesF - 32) .* 5 ./ 9 • To say “store this value here”, use = degreesF = 72 degreesC = (degreesF - 32) .* 5 ./ 9 CS107, Prof. Steinberg, f10 Lecture 02 22
Why use a variable? Why use a variable? • One use: to store an intermediate result for reuse discriminant = b.^2-4.*a.*c b + sqrt(discriminant)/(2.*a) b - sqrt(discriminant)/(2.*a) CS107, Prof. Steinberg, f10 Lecture 02 23
Why use a variable? Why use a variable? • Another use: in a repetitive computation – E.g. compute square root of 5. Use a variable named, say, root >> root = 2 root = 2 >> root = (root + 5 ./ root) ./ 2 root = 2.2500 >> root = (root + 5 ./ root) ./ 2 root = 2.2361 >> root .* root ans = 5.0000 CS107, Prof. Steinberg, f10 Lecture 02 24
Matlab Programs Programs Matlab • A Matlab program is made up of statements – One per line • One kind of statement is: Assignment statement CS107, Prof. Steinberg, f10 Lecture 02 25
Assignment statement Assignment statement • Purpose: store a piece of data in a variable • Form: variableName = expression • Meaning: – Evaluate expression – Store the result in the variable with name variableName – Print the result CS107, Prof. Steinberg, f10 Lecture 02 26
Assignment statement Assignment statement • May end with ; to prevent printing the value of the expression degreesF = 72; CS107, Prof. Steinberg, f10 Lecture 02 27
Grammatical Structure Grammatical Structure • An assignment statement has parts cost = nonTaxable + taxable .* 1.065 Variable Expression name CS107, Prof. Steinberg, f10 Lecture 02 28
Grammatical Structure Grammatical Structure • Parts can have parts nonTaxable + taxable .* 1.065 Expression expression operator expression CS107, Prof. Steinberg, f10 Lecture 02 29
Grammatical Structure Grammatical Structure • Parts can have parts that have parts nonTaxable + taxable .* 1.065 Expression expression operator expression expression operator expression CS107, Prof. Steinberg, f10 Lecture 02 30
Grammatical Structure Grammatical Structure totalCal = mms .* calPerMm position = startPos + time .* speed pay = hours .* rate + overtime * otrate CS107, Prof. Steinberg, f10 Lecture 02 31
Grammatical Structure Grammatical Structure totalCal = mms .* calPerMm position = startPos + time .* speed pay = hours .* rate + overtime * otrate CS107, Prof. Steinberg, f10 Lecture 02 32
Grammatical Structure Grammatical Structure totalCal = mms .* calPerMm position = startPos + time .* speed pay = hours .* rate + overtime * otrate CS107, Prof. Steinberg, f10 Lecture 02 33
Grammatical Structure Grammatical Structure totalCal = mms .* calPerMm position = startPos + time .* speed pay = hours .* rate + overtime * otrate CS107, Prof. Steinberg, f10 Lecture 02 34
Programs Programs • So far, we have used Matlab as a calculator – Type a line, Matlab does it • How do we type instructions once, use many times? CS107, Prof. Steinberg, f10 Lecture 02 35
Functions Functions • Suppose you want to calculate length of hypotenuse? hypotenuse = ? height = 4 base = 5 • In command window, type sqrt(5.^2 + 4.^2) CS107, Prof. Steinberg, f10 Lecture 02 36
Functions Functions • Suppose you want to calculate length of hypotenuse many times? CS107, Prof. Steinberg, f10 Lecture 02 37
Functions Functions • Suppose you want to calculate length of hypotenuse many times? sqrt( 4 .^2 + 5.^2) sqrt( 7 .^2 + 11.^2) sqrt( 20 .^2 + 21.^2) … • This is a pain - can’t we just do hyp(4, 5) hyp(7, 11) hyp(20, 21) CS107, Prof. Steinberg, f10 Lecture 02 38
Recommend
More recommend