cs249 lecture notes, Fall 2001 Week 2, Monday Reading: Chapter 2 Read Chapter 2 for Thursday Homework 1 out today, due next Monday. Hand out syllabus. Talk about course alternatives. My lecture notes are on the web page. If you miss class, be sure to pick them up. Useful even if you come to class. http://rocky.wellesley.edu/cs249/notes High-level languages -------------------- MATLAB is a high-level language, which means that it was designed to be easy for humans to read and right. At first, that claim will seem incredible, but it really is, compared to low level-languages, which are easy for computers to execute. Anyone know another HLL? Used one? General-purpose vs. application-specific HLL. LLLs are machine specific. HLLs are portable. To execute a HLL program, have to translate. 1) Interpretation. Line by line translation. 2) Compilation. Translation all at once. HLL code is called source code. Compiler generates object code or executable. Naming conventions: .c .C .f .java .o .exe What is a program? A sequence of instructions that specifies HOW to perform a computation. Instructions: input, output, simple math, comparison, repetition Specify HOW: in excruciating detail... beginning programmers often have the feeling of teaching an idiot, because the computer seems not to know many things that are obvious to people. Think of it as an exercise in inter-species communication. Computation: More general than just math -- anything a computer can do is considered computation in broad sense. Amazing thing #1: all computations are made up of the same set of simple instructions. Amazing thing #2: compilers are programs that translate other programs, sometimes improving them in the process! Natural and formal languages ---------------------------- Nat lang: evolved, people speak it Formal lang: designed, people use it for special purpose Mathematics uses a formal language, chemistry too. Syntax refers to the rules and structure of the language. Semantics refers to the meaning of the language. A syntax error is a statement that is meaningless because it violates the rules of the language: 1) 3+3=6 is syntactically correct, 3=+6$ is not 2) H2O is correct, 2Zz is not. The statement 3+3=5 is syntactically correct, but semantically incorrect, because what it means is false. The chemical reaction Pb -> Au is syntactically correct, but semantically unlikely. When you read or hear a sentence in English, you unconsciously figure out its structure (nouns, verbs, subjects, objects). This process is called parsing. MATLAB interpreter ------------------ Input-execute-display cycle The >> thing is a prompt. An expression is a combination of operands and operators. Many of the operators are familiar. You can invoke built-in functions like sin(1) and log(10) To raise a number to a power : y^x To raise e to a power exp(x) Translation from mathematical notation to MATLAB is generally easy, but it is still a translation. Common error: no juxtapositive multiplication, use * Errors ------ Is programming a walk in the park? Why, no. Sometimes it is frustrating and unpleasant, like all worthwhile things. Some things that go wrong: 1) Syntax errors: computer is a stickler for spelling and grammar. Syntax errors are discovered by the compiler, and prevent it from generating code. You can't run until you get rid of all the syntax errors. 2) Run-time errors: An unusual condition, like dividing by zero, can cause the program to stop executing at some point. Called "exception" in Java. 3) Logic errors: The program runs, but does not do what you expected. 4) Approximation errors: some numbers cannot be represented in MATLAB's number format, like 2/3. Have to round off to the nearest representable number. Really, these are not four kinds of errors, but three times when you might detect errors. There is only one kind of error --- the program does not do what you wanted it to do. Why? Because you did not specify the right computation. Computer interprets code quite literally, with no understanding of what you are trying to accomplish. Scientific notation ------------------- We write 3.1 x 10^3 MATLAB writes 3.1e3 >> tan (3.141) >> c = 2.14e8 Variables --------- You can create a new variable (a name) and assign a value to it. = is the assignment operator. >> c = 2.14e8 >> distance = 4000 >> time = distance / c When a variable appears in an expression, it gets evaluated (replaced by its value). Variable names begin with lower-case letters and can contain numbers and upper case letters. Some names are reserved because they are keywords. Some are predefined, like pi and inf. Help ---- Start using help and helpwin every time you use a new command. Sometimes the documentation is hard to understand because it refers to things we don't know about yet, but it is still worth reading. Absolute and relative errors ---------------------------- sqrt(18 pi) * (9/e)^9 ~= 9! Off by 3343.13. Is that a lot? Depends... in absolute terms, it sounds like a lot. In relative terms it's pretty reasonable. relative error = absolute error / actual value (usually we only care about the absolute value of error) Ok... Do part 1 of the homework for Thursday.