Numerical Methods in Scientific Computation

Similar documents
Scientific Computing. Error Analysis

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming

LECTURE 0: Introduction and Background

Matlab Advanced Programming. Matt Wyant University of Washington

Introduction to Computer Programming with MATLAB Calculation and Programming Errors. Selis Önel, PhD

Introduction to Computational Mathematics

Chapter 1. Numeric Artifacts. 1.1 Introduction

Basics of Computation. PHY 604:Computational Methods in Physics and Astrophysics II

Numerical computing. How computers store real numbers and the problems that result


Introduction. C provides two styles of flow control:

Truncation Errors. Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 4.

Programming Languages and Program Development Life Cycle Fall Introduction to Information and Communication Technologies CSD 102

CSc 10200! Introduction to Computing. Lecture 1 Edgardo Molina Fall 2013 City College of New York

MATLAB is a multi-paradigm numerical computing environment fourth-generation programming language. A proprietary programming language developed by

Method & Tools for Program Analysis & Design

Repetition Structures Chapter 9

Our Strategy for Learning Fortran 90

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Approximations and Errors

We deliver Global Engineering Solutions. Efficiently. This page contains no technical data Subject to the EAR or the ITAR

Outline. Program development cycle. Algorithms development and representation. Examples.

Computing Seminar Introduction Oct

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Coding and Unit Testing! The Coding Phase! Coding vs. Code! Coding! Overall Coding Language Trends!

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

Chapter 7. Iteration. 7.1 Multiple assignment

2 Computation with Floating-Point Numbers

How to approach a computational problem

Getting Started with MATLAB

Introduction Objectives: 1) differentiate between high-level, low-level, and machine language; 2) explain why it is necessary to translate a program

Introduction to Scientific Computing with Matlab

Flow Control: Branches and loops

Test Automation Beyond Regression Testing

Testing! Prof. Leon Osterweil! CS 520/620! Spring 2013!

(Refer Slide Time: 02:59)

CSC 121 Spring 2017 Howard Rosenthal

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Programming for Engineers Iteration

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Scientific Programming in C VI. Common errors

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

PIETRO, GIORGIO & MAX ROUNDING ESTIMATING, FACTOR TREES & STANDARD FORM

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

STEPHEN WOLFRAM MATHEMATICADO. Fourth Edition WOLFRAM MEDIA CAMBRIDGE UNIVERSITY PRESS

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Lecture Transcript While and Do While Statements in C++

Bits, Words, and Integers

Floating-point representation

3.5 Floating Point: Overview

Chapter 1. Math review. 1.1 Some sets

A Multiple-Precision Division Algorithm

Reading Assignment. Lazy Evaluation

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

2.0 MATLAB Fundamentals

2 Computation with Floating-Point Numbers

Up next. Midterm. Today s lecture. To follow

2.1.1 Fixed-Point (or Integer) Arithmetic

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Expressions and Casting

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

Chapter 6 Programming the LC-3

Fortran 90 Two Commonly Used Statements

Physics 331 Introduction to Numerical Techniques in Physics

Computing and compilers

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

Integers are whole numbers; they include negative whole numbers and zero. For example -7, 0, 18 are integers, 1.5 is not.

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Structure Array 1 / 50

Lecture 03 Approximations, Errors and Their Analysis

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

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

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Flow Chart & Algorithms

MAT 003 Brian Killough s Instructor Notes Saint Leo University

National Numeracy and Mathematics Progression Framework BEGIN

Testing. UW CSE 160 Winter 2016

CS 542G: Preliminaries, Floating-Point, Errors

MATH2070: LAB 3: Roots of Equations

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

n! = 1 * 2 * 3 * 4 * * (n-1) * n

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

PROBLEM SOLVING AND PYTHON PROGRAMMING

Variable A variable is a value that can change during the execution of a program.

CS321 Introduction To Numerical Methods

A Multiple -Precision Division Algorithm. By David M. Smith

Divide: Paper & Pencil

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

4.1 QUANTIZATION NOISE

1.1 - Functions, Domain, and Range

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

Solving Equations with Inverse Operations

Measurements: Significant Figures

CS 1313 Spring 2000 Lecture Outline

Roundoff Errors and Computer Arithmetic

Transcription:

Numerical Methods in Scientific Computation Programming and Software Introduction to error analysis 1

Packages vs. Programming Packages MATLAB Excel Mathematica Maple Packages do the work for you Most offer an interactive environment 2

MATLAB Software product from The MathWorks, Inc. Originally focused on matrix manipulations Interactive tool to do numerical functions, and visualization Commands can be saved into user scripts call and m-file Graphics and graphical user interfaces (GUI) are built into program 3

MATLAB Base product is extended with a variety of toolboxes Optimization Statistics Curve fitting Image processing 4

MATLAB Example: Find roots of >> p = [1-6 -72-27] >> r = roots (p) >> r = 12.1229-5.7345-0.3884 x 3 6x 2 72x 27 5

MATLAB Advantages: Large library of functions to evaluate a wide variety of numerical problems Interactive tool allows immediate evaluation of results Disadvantages Expensive Not as fast as C/C++/FORTRAN code 6

Programming language concepts Interpreted Languages MATLAB Usually integrated with an interactive GUI Allows quick evaluation of algorithms Ideal for debugging and one-time analysis and in cases where high speed is not critical 7

Programming language concepts Compiled Languages FORTRAN, C, C++ Source code is created with an editor as a plain text file Programs then needs to be compiled (converted from source code to machine instructions with relative addresses and undefined external routines still needed). The compiled routines (called object modules) need then to be linked with system libraries. Faster execution than interpreted languages 8

Programming concepts Data representation Constants, variables, type declarations Data organization and structures Arrays, lists, trees, etc. 9

Programming concepts Mathematical formulas Assignment, priority rules Input/Output Stdin / Stdout, files, GUI 10

Programming concepts Structured programming Set of rules that prescribe good programming style Single entry point and exit point to all control structures Flexible enough to allow creativity and personal expression Easy to debut, test and update 11

Structured programming Control structures Sequence Selection Repetition Computer code is clearer and easier to follow 12

Algorithm expression Flowcharts Visual representation Useful in planning Pseudocode Simple representation of an algorithm Basic program constructs without being language-specific Easy to modify and share with others 13

Flowcharts 14

Control structures Sequence Implement one instruction at a time 15

Control structures Selection Branching IF/THEN/ELSE CASE/ELSE 16

Control structures 17

Control structures 18

Control structures Repetition DOEXIT loops (break loop) Similar to C while loop DOFOR loop (count-controlled) 19

Control structures pretest posttest midtest 20

Control structures 21

Modular programming Break complicated tasks into manageable parts Independent and self-contained Well-defined modules with a single entry point and single exit point Always a good idea to return a value for status or error code 22

Modular programming Makes logic easier to understand and digest Allows black-box development of modules Requires well-defined interfaces with no side effects Isolates errors Allows for reuse of modules and development of libraries 23

Software engineering approach Specification: A clear statement of the problem, the requirements, and any special parameters of operation Algorithm/Design: A flow chart or pseudo code representation of how exactly how will the problem be solved. 24

Software engineering approach Implementation: Breaking the algorithm into manageable pieces that can be coded into the language of choice, and putting all the pieces together to solve the problem. Verification: Checking that the implementation solves the original specification. In numerical problems, this is difficult because most of the time you don t know what the correct answer is. 25

Example case How do you solve the sum of integers problem? S = n i= 0 i a) Simplest sum = 1 + 2 + 3 + 4 + 5 +... + N Coded specifically for specific values of N. 26

Possible solutions b) Intermediate solution Does not require much thought, takes advantage of looping ability of most languages: C/C++: MATLAB 27

Possible solutions c) Analytical solution Requires some thought about the sequence remember back to one of your math classes. sum = n* ( n + 1) 2 28

Verification of algorithm What can fail in the above algorithms. (To know all the possible failure modes requires knowledge of how computers work). Some examples of failures are: For (b): This algorithm is fairly robust. But, when N is large execution will be slow compared to (c) 29

Algorithm (c) This is most common algorithm for this type of problem, and it has many potential failure modes. For example: (c.1) What if N is less than zero? Still returns an answer but not what would be expected. (What happens in (b) if N is negative?). 30

Algorithm (c) (c.2) In which order will the multiplication and division be done. For all values of N, either N or N+1 will be even and therefore N*(N+1) will always be even but what if the division is done first? Algorithm will work half of the time. If division is done first and N+1 is odd, the algorithm will return a result but it will not be correct. This is a bug. For verification, it means the algorithm works sometimes but not always. 31

Algorithm (c) (c.3) What if N is very large? What is the maximum value N*(N+1) can have? There are maximum values that integers can be represented in a computer and we may overflow. What happens then? Can we code this to handle large values of N? 32

Verification By breaking the program into small modules, each of which can be checked, the sum of the parts is likely to be correct but not always.. Problems can be that the program only works some of the time or it may not work on all platforms. The critical issue to realize all possible cases that might occur. 33

Introduction to error analysis Error is the difference between the exact solution and a numerical method solution In most cases, you don t know what the exact solution is, so how do you calculate the error Error analysis is the process of predicting what the error will be even if you don t know what the exact solution Errors can also be introduced by the fact that the numerical algorithm has been implemented on a computer 34

Significant digits Can a number be used with confidence? How accurate is the number? How many digits of the number do we trust? 35

Significant digits Can the speed be estimated to one decimal place? 36

Significant digits The significant digits of a number are those that can be used with confidence The digits that are known with certainty plus one estimated digit zeros are not always significant digits 0.0001845, 0.001845, 0.01845 4.53x10 4, 4.530x10 4, 4.5300x10 4 Exact numbers vs. measured numbers Exact numbers have an infinite number of significant digits π is an exact number but usually subject to round-off 37

Significant digits 38

Accuracy and precision Accuracy is how close a computed or measured value is to the true value Precision is how close individual computed or measured values agree with each other Reproducibility Inaccuracy/Bias vs. Imprecision/Uncertainty Inaccuracy: systematic deviation from the truth imprecision: magnitude of the scatter 39

Accuracy and precision 40

Accuracy and precision The level of accuracy and precision required depend on the problem Error to represent both the inaccuracy and imprecision of predictions 41

Error definitions Two general types of errors Truncation errors due to approximations of exact mathematical functions Round-off errors due to limited significant digit representation of exact numbers E t = true value approximation 42

Error definitions E t does not capture the order of magnitude of error 1V error probably doesn t matter if you re measuring line voltage, but it does matter if you re measuring the voltage supply to a VLSI chip Therefore, its better to normalize the error relative to the value 43

Error definitions Example: Line voltage Chip supply voltage 44

Error definitions What if we don t know the true value? Use an approximation of the true value a How do we calculate the approximate error? Use an iterative approach Approximate error = current approximation previous approximation Assumes that the iteration will converge 45

Error Definitions For most problems, we are interested in keeping the error less than specified error tolerance How many iterations do you do, before you re satisfied that the result is correct to at least n significant digits? 46

Example Infinite series expansion of e x As we add terms to the expansion, the expression becomes more exact Using this series expansion, can we calculate e 0.5 to three significant digits? 47

Example Calculate the error tolerance First approximation Second approximation Error approximation 48

Example Third approximation Error approximation 49

Example 50

Next class HW1, due 9/8 Chapra & Canale 6 th Edition 1.8 (typo: dy/dx -> dy/dt), 1.12, 2.5 (choose order n=6), 2.14 Next class Continue on error analysis 51