W4260: Modeling the Universe. Lecture 4

Similar documents
An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Computers in Engineering Root Finding Michael A. Hawker

Introduction to Programming II W4260. Lecture 2

Today s class. Roots of equation Finish up incremental search Open methods. Numerical Methods, Fall 2011 Lecture 5. Prof. Jinbo Bi CSE, UConn

1.00 Lecture 19. Numerical Methods: Root Finding

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Remember Java Data Types

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Exercises C-Programming

Contents. Hilary Term. Summary of Numerical Analysis for this term. Sources of error in numerical calculation. Solving Problems

CS 320: Concepts of Programming Languages

1.00 Lecture 19. Packaging Functions in Objects

Functions and Recursion

Handout 2 - Root Finding using MATLAB

CS 2113 Midterm Exam, November 6, 2007

Lecture 8 Mathematics

From: Robert Sharpley Subject: Homeworks #6 Date: February 22, :09:53 AM EST Cc: Robert Sharpley

MS213: Numerical Methods Computing Assignments with Matlab

ALGORITHM 2-1 Solution for Exercise 4

Chapter 1. Fundamentals of Higher Order Programming

AC : MATHEMATICAL MODELING AND SIMULATION US- ING LABVIEW AND LABVIEW MATHSCRIPT

1.00 Lecture 25. Root Finding

MATH2070: LAB 3: Roots of Equations

Introduction to C++ Introduction to C++ Week 6 Dr Alex Martin 2013 Slide 1

What Secret the Bisection Method Hides? by Namir Clement Shammas

10.4 Linear interpolation method Newton s method

COMP 208 Computers in Engineering

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving

Biostatistics 615/815 Lecture 13: Numerical Optimization

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

The Bisection Method versus Newton s Method in Maple (Classic Version for Windows)

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Introduction to Computer Methods Midterm Examination Fall Term 2002 October 29, Open book, notes and calculator Time allowed: 50 minutes

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Lecture 7 Symbolic Computations

Computer Science & Engineering 150A Problem Solving Using Computers

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

PART 1: SHORT ANSWER QUESTIONS: TOTAL MARKS 40/100

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

LECTURE 0: Introduction and Background

Answers to Homework 5: Interpolation: Polynomial Interpolation 2

Chapter 1. Numeric Artifacts. 1.1 Introduction

CSCI 171 Chapter Outlines

5.5 Newton s Approximation Method

Problem Solving (Computing) Array and Pointer

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

CS321 Introduction To Numerical Methods

Department of Computer Science, University of Otago

Introduction to 8086 Assembly

Review Functions Subroutines Flow Control Summary

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

And Now to Something Completely Different: Finding Roots of Real Valued Functions

Structured Programming. Dr. Mohamed Khedr Lecture 4

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Computational Mathematics: Models, Methods and Analysis. Zhilin Li

4.0 Programming with MATLAB

Intro. Speed V Growth

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

This quiz is open book and open notes, but do not use a computer.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Bisecting Floating Point Numbers

Pointers and scanf() Steven R. Bagley

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

An introduction to Scheme

After an "Hour of Code" now what?

Exact real arithmetic. Keith Briggs

Python lab session 1

Interpolation. TANA09 Lecture 7. Error analysis for linear interpolation. Linear Interpolation. Suppose we have a table x x 1 x 2...

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

VALLIAMMAI ENGINEERING COLLEGE

MATH2070: LAB 3: Roots of Equations

Programming for Engineers Iteration

B553 Lecture 12: Global Optimization

What Every Programmer Should Know About Floating-Point Arithmetic

Introduction to Functional Programming

The Newton Method as a Short WEB Example

Numerical Methods Lecture 7 - Optimization

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #05

ENGR 102 Engineering Lab I - Computation

UNIT 5B Binary Search

2. Numbers In, Numbers Out

1 MATH 253 LECTURE NOTES for FRIDAY SEPT. 23,1988: edited March 26, 2013.

2. Numbers In, Numbers Out

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Lecture #15 March 3, 2004 A Worked Problem in Floating Point

Types of files Command line arguments File input and output functions Binary files Random access

Full file at C How to Program, 6/e Multiple Choice Test Bank

Programming and Data Structure

UNIVERSITY OF CALIFORNIA COLLEGE OF ENGINEERING

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scan Converting Lines

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

CS450 - Structure of Higher Level Languages

Transcription:

W4260: Modeling the Universe Lecture 4

Overview Input and output (I/O) Files Scope Global variables More on functions The art of programming Before starting Program structure/layout Comments Root finding

I/O: talking to the computer Your program

Generic stream I/O pipe Data received along a pipe Your program Data sent out along a pipe How to access a pipe? - with a function!

Generic stream I/O pipe Your program 3 steps: 1. Open the pipe (stream) 2. Send/receive data 3. Close the pipe (stream)

1. Opening a file (stream/pipe) Must decide: input or output? (read or write?) New data type (File type) In addition to Int, Float, Double, etc FileIn = open( input.data, r ) input.data is the name of the file FileIn is the name of the pipe (variable of type File)

1. Opening a file (stream/pipe) r is the access mode Modes: r read w write a append FileIn = open( input.data, r )

1. Opening a file (stream/pipe) Can open two pipes at the same time (must have different names) Modes: r read w write a append FileIn = open( input.data, r ) FileOut = open( stuff.out, w ) Note: if output file (stuff.out) doesn t exist, it will be created (if it does exist, all data inside will be lost unless mode is a )

1. Opening a file input.data FileIn Your program FileOut stuff.out

Easy! 3. Closing a file (stream/pipe) FileIn = open( file.data, r ) FileIn.close()

2. Writing to a file First: text stuff.out my data FileOut = open( stuff.out, w ) FileOut.write( my data\n ) write is a function which writes the data \n means newline

2. Writing to a file (slightly) more difficult: outputting numbers x stuff.out 3.142 x = 3.142 FileOut = open( stuff.out, w ) FileOut.write( my data\n ) FileOut.write( %f\n % x) %f means float format (e.g 3.142)

2. Writing to a file Converting numbers to output (write & print) print format-string % (variables) %f means float format %d means integer format %e means exponential format print %f % (pi) print %e %e %e\n % (x, y, z) print "number %d is %f\n" % (7, 45.6)

2. Reading from a file file.data 4 3.142 i x line is a string variable (line = 4 3.142 ) Python FileIn = open( input.data, r ) line = FileIn.readline() data = split(line) i = int(data[0]); x = float(data[1]);

2. Reading from a file Python file.data 4 3.142 i x split is a function which splits line into the bits between spaces data is a list (data = 4, 3.142 ) Python FileIn = open( input.data, r ) line = FileIn.readline() data = split(line) i = int(data[0]) x = float(data[1]) data[0] data[1]

2. Reading from a file Python file.data 4 3.142 i x int() is a function which converts the string 4 into the integer 4 (i = 4) Python fin = open( input.data, r ) line = fin.readline() data = split(line) i = int(data[0]); x = float(data[1]); (same thing for x)

B. Scope (or namespace)

B. Scope Variables declared within a function can only be used within that function def average(): return (y + x)/2 y = 3.0 x = 5.6 a = average()

B. Scope Variables declared within a function can only be used within that function def average(): return (y + x)/2 Doesn t work y = 3.0 x = 5.6 a = average()

B. Scope Variables declared within a function can only be used within that function def average(): return (y + x)/2 y = 3.0 x = 5.6 a = average() These refer to different variables! main.x average.x

B. Scope Variables declared within a function can only be used within that function def average(x, y): return (y + x)/2 y = 3.0 x = 5.6 a = average(x, y) Correct way Pass variables into function

B. Scope Note that argument passed to a function do not need to have the same name as those defined in the function itself. def average(x, y): return (y + x)/2 d = 3.0 e = 5.6 a = average(d, e) Print average(1.5, 1.8) Can also pass constant arguments to a function

B. Scope Variables are not visible everywhere Only within the local function def average(x, y): y = 3.0 x = 5.6 a = average(x, y) return (y + x)/2

B. Scope Can declare global variables at top of file OmegaM = 0.7 OmegaL = 0.3 def E(z): return sqrt(omegam*pow(1+z,3)+omegal) Declared before main (global variable) Print E(8.5) But cannot assign to global variable in a function

B. Scope: Global variables Global variables visible everywhere (in file) OmegaM = 0.3 OmegaM def E(x, y): Declared before main (global variable) return (OmegaM

B. Scope But cannot assign to global variable in a function ntimes = 0 def average(x, y): ntimes = ntimes + 1 return (x + y)/2 Declared before main (global variable) y = 3.0 x = 5.6 a = average(x, y) print called %d times\n % ntimes

B. Scope Can lead to unexpected behavior x=24 def foo(): y=2 print x+y def bar(): x=11 foo() >>> bar() 26

Aside on functions Passing data into functions: the wrong way to do it C program float tickle(double x) { x = (x*2-3.5)/1.8; } PYTHON program def tickle(x): x = (x*2-3.5)/1.8

Aside on functions Passing data into functions: the wrong way to do it C program float tickle(double x) { x = (x*2-3.5)/1.8; } PYTHON program def tickle(x): x = (x*2-3.5)/1.8 This has no effect, because x is a local copy! - variables set within a function are thrown away after the function is finished. (exception: arrays)

Aside on functions Passing data into functions: the wrong way to do it C program float tickle(double x) { x = (x*2-3.5)/1.8; } PYTHON program def tickle(x): x = (x*2-3.5)/1.8 x = 4.0; tickle(x); tickle(4.0); x = 4.0 tickle(x) tickle(4.0)

Aside on functions Passing data into functions: the right way to do it float tickle(double x) { double result; result = (x*2-3.5)/1.8; return result; } C program x = tickle(x); PYTHON program def tickle(x): result = (x*2-3.5)/1.8 return result x = tickle(x)

The art of programming Before writing any code, stop and think What needs to be done? What are the steps to do it? Write out pseudo-code (or flowchart) Variables required? Go through a short example if possible

A prime number algorithm Step 1: List out the numbers from 1 to n Step 2: Strike out all numbers divisible by 2 (except 2) Strike out all numbers divisible by 3 (except 3) and so on for 7,11,13 Step 3: List out the remaining numbers, they are prime. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Prime number variables: Array of numbers (1000) Integer for sqrt(1000) Integers for loops (i,j)

Program structure Put often repeated steps into function Functions at top of program Use consistent indentation C program for (i=1; i < n; i++) { x = findroot(coeffs, n); sum = sum + x; } PYTHON program for i in range(1, n): x = findroot(coeffs, n) sum = sum + x

Program structure Use descriptive variable names (e.g. sum instead of s) C program for (i=1; i < n; i++) { x = findroot(coeffs, n); sum = sum + x; } PYTHON program for i in range(1, n): x = findroot(coeffs, n) sum = sum + x

Comments In Python: anything after a # symbol C program PYTHON program /* sum the roots of the polynomial */ for (i=1; i < n; i++) { x = findroot(coeffs, n); sum = sum + x; } # sum the roots of the polynomial for i in range(1, n): x = findroot(coeffs, n) sum = sum + x

Readability C program PYTHON program /* sum the roots of the polynomial */ for (i=1; i < n; i++) { x = findroot(coeffs, n); sum = sum + x; } # sum the roots of the polynomial for i in range(1, n): x = findroot(coeffs, n) sum = sum + x Compare to: for (i=1; i < n; i++){ x = f(c, n); s = s + x;} for i in range(1, n): x = f(c, n) s = s + x

C is particularly easy to abuse

Q. What does this program do? #include <stdio.h> #include <math.h> double l;main(_,o,o){return putchar((_--+22&&_ +44&&main(_,-43,_),_&&o)?(main(-43,++o,O),((l=(o+21)/ sqrt(3-o*22-o*o),l*l<4&&(fabs(((time (0)-607728)%2551443)/405859.-4.7+acos(l/2))<1.57))[" #"])):10);}

Q. What does this program do? #include <stdio.h> #include <math.h> double l;main(_,o,o){return putchar((_--+22&&_ +44&&main(_,-43,_),_&&o)?(main(-43,++o,O),((l=(o+21)/ sqrt(3-o*22-o*o),l*l<4&&(fabs(((time (0)-607728)%2551443)/405859.-4.7+acos(l/2))<1.57))[" #"])):10);} A. Prints the phase of the moon

The International Obfuscated C Code Contest #define _ -F<00 --F-OO--; int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO() { _-_-_- -_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_-_-_-_-_- -_-_-_-_-_-_- -_-_-_ } http://www.ioccc.org/

1. Nonlinear Equations (root finding) One dimensional: f : R R e.g. f(x) = ax-b = 0 (linear) or f(x) = sin(2x)-x = 0 (nonlinear) Multidimensional f : R n R n e.g. {f(x,y)= x 2 +y 2 +xy-1=0, g(x,y)= x 2 +y 2 -xy-1=0} (nonlinear: two ellipses)

1-D Root-Finding Methods Bisection Method Newton s Method Secant Method Others

Bisection Method (Binary Search) a b

Bisection Method (Binary Search) a (a+b)/2 b

Bisection Method (Binary Search) a b

Bisection Method (Binary Search) Given f(), tol, and [a,b] such that sign(f(a)) sign(f(b)): while b-a>tol: m = (a+b)/2 # midpoint if sign(f(a)) == sign(f(m)): a = m # recurse on right half else: b = m # recurse on left half Guaranteed convergence! (if you can find initial a,b) but only at a linear rate: error k+1 c error k, c<1

Newton s Method x 0 x 1

Newton s Method x 0 x 2 x 1

Newton s Method Given f(), f (), tol, and initial guess x 0 k = 0 do x k+1 = x k - f(x k )/f (x k ) k++ while x k - x k-1 > tol Can diverge (especially if f 0). If it converges, does so at quadratic rate: e k+1 c e k 2. Requires derivative computation.

Newton s Method Can Diverge x 0

Secant Method Like Newton, but approximates slope using point pairs Given f(), tol, and initial guesses x 0, x 1 k = 1 do x k+1 = x k - f(x k )(x k - x k-1 )/(f(x k )-f(x k-1 )) k++ while x k - x k-1 < tol Can diverge. If it converges, does so at rate 1.618: e k+1 c e k 1.618.

Secant Method x 0 x 1

Linear Fractional Interpolation Instead of linear approximation, fit with fractional linear approximation: f(x) (x-u)/(vx-w) for some u,v,w Given f(), tol, and initial guesses a, b, c, f a =f(a), f b =f(b) do h = (a-c)(b-c)(f a -f b )f c / [(a-c)(f c -f b )f a -(b-c)(f c -f a )f b ] a = b; f a = f b b = c; f b = f c c += h; f c = f(c) while h > tol If it converges, does so at rate 1.839: e k+1 c e k 1.839.

1-D Root-Finding Summary Bisection: safe: never diverges, but slow. Newton s method: risky but fast! Secant method & linear fractional interpolation : f (x) not required, mid-speed. Hybrids: use a safe method where function poorly behaved, Newton s method where it s well-behaved.

Multi-dimensional Root-Finding No general good way to do this Once close to a root, use multi-dimensional Newton-Raphson