LECTURE 0: Introduction and Background

Size: px
Start display at page:

Download "LECTURE 0: Introduction and Background"

Transcription

1 1 LECTURE 0: Introduction and Background September 10, Computational science The role of computational science has become increasingly significant during the last few decades. It has become the third main pillar of scientific research in addition to the traditional theoretical and experimental fields. A model is an artificial object which reflects and reproduces the essential features, relationships and functions of a concrete object or phenomenon in a simplified way and can therefore be used as a tool for examining and analyzing reality. Computer simulations are sometimes called "computer experiments". They are performed by computer programs based on mathematical models, and the models are derived from scientific knowledge (theories) which is used to explain the phenomena under study or the underlying laws. Computer simulations can be used to test the mathematical models that are based on the underlying theories, thus also testing the theories themselves. In this way, computer simulations can serve as a bridge between experiments and theory. A problem of computational science is typically characterized by the following: i. They have a precise mathematical definition. ii. They are not solvable using traditional methods. iii. They require profound knowledge of the application field. During this course, we will learn to use a set of numerical methods that form the basic set of tools for scientific computing. We concentrate on forming a profound understanding on how the methods work and where things can go wrong, instead of just using these tools as "black boxes" (put something in and something comes out). Toward the end of the course, we will also have an introduction to the world of computer simulations and modeling.

2 2 2 Programming issues In this section, you will find a collection of some issues and tips related to the programming tasks that you will be doing during this course. 2.1 General The aim of this course is to learn in practice how to use some of the many computational tools for solving scientific problems on a computer. During the course you will learn to implement numerical methods by writing your own codes and to understand methods in software packages and numerical libraries. The exercises consist of theoretical calculations and to a large part of programming tasks. The programming tasks must be done using one of the following programming languages: C/C++, Fortran or Java. Mathematical software, such as Matlab, Maple or Mathematica, can be used as an additional tool (e.g. for checking the results, plotting etc.), but not to replace hand-written codes (unless explicitly instructed to do so). 2.2 Programming tips A good numerical program should be i. efficiently coded ii. easy to read and change (modular) iii. easy to export from one platform (computer architecture) to another Think before you start! What problem do you want to solve and which algorithm is best suited to your case. How should you begin the actual coding work. Use pseudocode Before beginning the actual coding, write out the algorithm using pseudocode. This helps you plan the program and serves as a bridge between the mathematics and the computer program. In the beginning think small! Don t try to write the whole program at once. Code small pieces of the code separately and test that they function correctly. Check and double check Spend time checking the code before running it.

3 3 Use test cases Begin testing the program with very short simple test cases. Print out information, check that everything works correctly before continuing to more difficult cases. Use several tests because faulty programs often work on simple cases but fail on more difficult ones. Modularize your code Build your program using several building blocks (subprograms, functions, procedures). Test each module separately. Try to keep each of the modules small. This will help debugging the code and it makes the program more readable. You can also change the code more easily by editing only one or part of the modules instead of rewriting the whole code. Generalize slightly If the program can be written to handle slightly more general cases, then it is often worth the extra effort to do so. For example, instead of writing the program for a given set of numbers, a few additional statements can be used to make the program work for an arbitrary set of numbers. However, the programming task should not be made overly complicated by introducing too much generality. Include comments Comments help you to remember later on what the program does and they are vital for someone else attempts to read your code. It is recommended that you include a short description before each segment of the program which explains the purpose of that section of the code and the main variables (at least input and output). Use meaningful variable and function names It is helpful to name all main variables and functions according to their purpose. This improves the readability of the program. For example, double sum, double average(double x, double y). Declare nonchanging constants All nonchanging parameter values should be declared as constants. In C, this can be done using the preprocessor command #define for the parameter declarations which can be placed in a separate header file. For example, a header file parameters.h can include the following declarations: #define MAXSIZE 500 #define PI Include warning messages A robust program always warns if it encounters a situation it wasn t designed to handle. Warning messages also help debugging the code when inevitable bugs appear.

4 4 Use appropriate data structures Think what kind of data structures are natural to the problem at hand. Using structures instead of long lists of separate variables can often help to clarify the program. Be careful when using built-in functions Many built-in functions (e.g. mathematical functions such as sin, log, exp etc.) are available in scientific programming languages. Be careful to check the type of input and output arguments of these functions. Think about efficiency Remove all unnecessary operations from inner loops (e.g. constant parameter values should be set in the beginning of the program, not repeatedly in each loop). When optimizing, concentrate on the most time consuming parts of the program. Remember to optimize not only the computer time but also your own time! Do not over-optimize! There are many ways to improve the efficiency of numerical programs but for the purpose of this course, it is more important that the code is clear and works correctly than achieving maximum efficiency. 2.3 Case studies Mathematical constants Many programming languages do not have predefined values of mathematical constants such as π. These must be explicitly defined in the program. Be careful not to mistype long sequences of digits, or alternatively, use simple calculations to define the variables; e.g. π = 4.0 arctan(1.0). Exponents Think carefully before writing statements with exponents. The general function x y is calculated in many computers as exp(ylnx) when y is not an integer. Sometimes this unnecessarily complicated and may contribute to round-off errors. The built-in function sqrt should be used instead of exponent 0.5 or 1/2. Small integer exponents should be written as products, e.g. x x x x. Consider using loops if possible, e.g j = ( 1) k can easily be calculated in a loop by writing j = 1 outside the loop and j = j inside the loop. Avoid mixed mode Avoid mixing real and integer expressions in your code. Use intrinsic type conversion functions when you need to convert for example an integer to a floating point number. E.g. 1/m should be written as 1.0/real(m) (in C as 1.0/(double)m).

5 5 When to avoid arrays It is often possible to avoid arrays, even though the mathematical description of an algorithm may indicate that a sequence of values is computed. In many cases, only the final value of the sequence is required and thus it is unnecessary to store all the intermediate values. For example, Newton s method is mathematically described as x n+1 = x n f (x n f (x n ) but the pseudocode can be written as simply as for n=1 to 10 do x x f (x)/ f (x) end for Limit iterations In repetitive algorithms, you should always limit the number of steps by using a control variable. This will prevent endless cycling due to unseen problems. There are two ways to control the loop: (i) Monitoring the changing variable (difference between previous and current steps), and (ii) Limiting the maximum number of steps. For example, the Newton s method could be written as for n=1 to n max do d f (x)/ f (x) x x d output n, x if d then exit loop end for Floating-point equality Due to arithmetic round-off errors, reasonable tolerances should be permitted when determining whether two floating-point numbers are equal. For example, if x and y are known to have magnitudes comparable to 1, we can use the branching statement if x y < ε then... to obtain n decimal digits with precision ε = n. If x and y have very large or very small orders of magnitude, then the relative error between x and y is needed: if x y < ε max( x, y )then...

6 6 Function evaluations There are several alternative ways of coding functions. The simplest approach is to use an assignment statement at appropriate places in the code. For example, consider the function f (x) = 2x + lnx sinx This can be coded simply as y 2x + ln(x) sin(x) where y and x are real variables. Another approach is to code an internal procedure called f which returns the value of f (x) at a given value of x. E.g. f at x = 2.5 is given by y f (2.5) and the subprogram for f is written in pseudocode as real function f(x) real x f 2x + ln(x) sin(x) end function f The first approach is simple and safe. On the other hand, if the functional form of f is complicated and values of f are evaluated several times in the code, then the best choice is to code the function once as a subprogram. This approach also allows for easy modifications of the program, if for example, you want to replace the function f by some other function g.

7 7 3 Taylor series Taylor series are needed for several applications during this course. Examples are error analysis, avoiding loss of significance, Newton s method for locating roots of equations, etc. Taylor series for f (x) about x = c. f (x) f (c) + f (c)(x c) + f (c) 2! f (k) (c) = (x c) k k! (x c) 2 + f (c) (x c) ! The special case c = 0 is called a Maclaurin series: f (x) f (0) + f (0)x + f (0) 2! f (k) (0) = x k k! x 2 + f (0) x ! Examples. e x = 1 + x + x2 2! + x3 3! +... = x k k! sinx = x x3 3! + x5 5!... = ( 1) k x 2k+1 (2k + 1)! cosx = 1 x2 2! + x4 x2k 4!... = ( 1) k (2k)! 1 1 x = 1 + x + x2 + x = ( x < ) x k ( x < 1) ln(1 + x) = x x2 2 + x = k 1 xk ( 1) k=1 k ( x < ) ( x < ) ( 1 < x 1)

8 8 A Taylor series converges rapidly near the point of expansion and slowly (or not at all) at more remote points. Figure 1 shows the partial sums of the Taylor series for the functions sin(x) and exp(x). 4 Partial sums of the Taylor series for sin(x) 3 S S 3 0 sin(x) 1 2 S Partial sums of the Taylor series for exp(x) 20 exp(x) 15 S 5 S 4 10 S 3 5 S 2 0 S Figure 1: Partial sums of the Taylor series for the functions sin(x) and exp(x).

9 3.1 Truncation error Taylor s theorem for f (x). If the function f possesses continuous derivatives of orders 0,1,2,...,n + 1 in a closed interval I = [a,b], then for any c and x in I, f (x) = n f (k) (c) (x c) k + E n+1 k! where the error term E n+1 can be given in the form E n+1 = f (n+1) (ξ) (x c)n+1 (n + 1)! Here ξ = ξ(x) is a point that lies between c and x. Example. Consider the function f (x) = e x. Taking the first five terms of the Taylor series for f about c = 0, we have where the error term is The point ξ lies in the interval [0,1]. If we take x = 1.0, we have S 5 = The exact answer is exp(1.0) = e x = 1 + x + x2 2! + x3 3! + x4 4! + E 5 E 5 = f (5) (ξ) x 5 5! Thus the true error in our approximation is In cases where the exact answer is not known, we can use the error term to estimate the deviation from the exact answer. Since f (x) = e x, we have f (5) (x) = e x. Thus the maximum value of f (5) in the interval [0,1] is reached at x = 1. We can now calculate an upper boundary for the error error e1 5! 15 = This is larger than the actual error resulting from the truncation. 9

10 10 Taylor s theorem for f (x + h). If the function f possesses continuous derivatives of orders 0,1,2,...,n + 1 in a closed interval I = [a,b], then for any x in I, f (x + h) = n f (k) (x) h k + E n+1 k! where h is any value such that x + h is in I and where with ξ between x and x + h. E n+1 = f (n+1) (ξ) (n + 1)! hn+1 This form of the Taylor s theorem is obtained by change of variables: x x+h and c x. The Taylor expansion for f (x + h) corresponds to several important theorems. For example, taking n = 1 f (x + h) = f (x) + f (x)h +O(h 2 ) where the so-called big Oh notation means that the error converges to zero at the same rate as h 2 as h 0. Thus we obtain the familiar approximation for the derivative of f : f (x) f (x + h) f (x) h

Numerical Methods in Scientific Computation

Numerical Methods in Scientific Computation 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

More information

CS321 Introduction To Numerical Methods

CS321 Introduction To Numerical Methods CS3 Introduction To Numerical Methods Fuhua (Frank) Cheng Department of Computer Science University of Kentucky Lexington KY 456-46 - - Table of Contents Errors and Number Representations 3 Error Types

More information

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

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

(Refer Slide Time: 02:59)

(Refer Slide Time: 02:59) Numerical Methods and Programming P. B. Sunil Kumar Department of Physics Indian Institute of Technology, Madras Lecture - 7 Error propagation and stability Last class we discussed about the representation

More information

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

Introduction to Computer Programming with MATLAB Calculation and Programming Errors. Selis Önel, PhD Introduction to Computer Programming with MATLAB Calculation and Programming Errors Selis Önel, PhD Today you will learn Numbers, Significant figures Error analysis Absolute error Relative error Chopping

More information

Our Strategy for Learning Fortran 90

Our Strategy for Learning Fortran 90 Our Strategy for Learning Fortran 90 We want to consider some computational problems which build in complexity. evaluating an integral solving nonlinear equations vector/matrix operations fitting data

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

Approximate First and Second Derivatives

Approximate First and Second Derivatives MTH229 Project 6 Exercises Approximate First and Second Derivatives NAME: SECTION: INSTRUCTOR: Exercise 1: Let f(x) = sin(x 2 ). We wish to find the derivative of f when x = π/4. a. Make a function m-file

More information

Review of Calculus, cont d

Review of Calculus, cont d Jim Lambers MAT 460/560 Fall Semester 2009-10 Lecture 4 Notes These notes correspond to Sections 1.1 1.2 in the text. Review of Calculus, cont d Taylor s Theorem, cont d We conclude our discussion of Taylor

More information

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

Lecture Objectives. Structured Programming & an Introduction to Error. Review the basic good habits of programming Structured Programming & an Introduction to Error Lecture Objectives Review the basic good habits of programming To understand basic concepts of error and error estimation as it applies to Numerical Methods

More information

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

Numerical computing. How computers store real numbers and the problems that result Numerical computing How computers store real numbers and the problems that result The scientific method Theory: Mathematical equations provide a description or model Experiment Inference from data Test

More information

Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017)

Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017) MATH-GA 2010.001/CSCI-GA 2420.001, Georg Stadler (NYU Courant) Fall 2017: Numerical Methods I Assignment 1 (due Sep. 21, 2017) Objectives. This class is for you and you should try to get the most out of

More information

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

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types Introduction to Computer Programming in Python Dr William C Bulko Data Types 2017 What is a data type? A data type is the kind of value represented by a constant or stored by a variable So far, you have

More information

Introduction to numerical algorithms

Introduction to numerical algorithms Introduction to numerical algorithms Given an algebraic equation or formula, we may want to approximate the value, and while in calculus, we deal with equations or formulas that are well defined at each

More information

Chapter 1. Numeric Artifacts. 1.1 Introduction

Chapter 1. Numeric Artifacts. 1.1 Introduction Chapter 1 Numeric Artifacts 1.1 Introduction Virtually all solutions to problems in electromagnetics require the use of a computer. Even when an analytic or closed form solution is available which is nominally

More information

New Mexico Tech Hyd 510

New Mexico Tech Hyd 510 Numerics Motivation Modeling process (JLW) To construct a model we assemble and synthesize data and other information to formulate a conceptual model of the situation. The model is conditioned on the science

More information

Introduction to Computational Mathematics

Introduction to Computational Mathematics Introduction to Computational Mathematics Introduction Computational Mathematics: Concerned with the design, analysis, and implementation of algorithms for the numerical solution of problems that have

More information

MATH2070: LAB 3: Roots of Equations

MATH2070: LAB 3: Roots of Equations MATH2070: LAB 3: Roots of Equations 1 Introduction Introduction Exercise 1 A Sample Problem Exercise 2 The Bisection Idea Exercise 3 Programming Bisection Exercise 4 Variable Function Names Exercise 5

More information

Computational Economics and Finance

Computational Economics and Finance Computational Economics and Finance Part I: Elementary Concepts of Numerical Analysis Spring 2016 Outline Computer arithmetic Error analysis: Sources of error Error propagation Controlling the error Rates

More information

Computational Economics and Finance

Computational Economics and Finance Computational Economics and Finance Part I: Elementary Concepts of Numerical Analysis Spring 2015 Outline Computer arithmetic Error analysis: Sources of error Error propagation Controlling the error Rates

More information

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

The Bisection Method versus Newton s Method in Maple (Classic Version for Windows) The Bisection Method versus (Classic Version for Windows) Author: Barbara Forrest Contact: baforres@uwaterloo.ca Copyrighted/NOT FOR RESALE version 1.1 Contents 1 Objectives for this Lab i 2 Approximate

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

ELEMENTARY MATLAB PROGRAMMING

ELEMENTARY MATLAB PROGRAMMING 1 ELEMENTARY MATLAB PROGRAMMING (Version R2013a used here so some differences may be encountered) COPYRIGHT Irving K. Robbins 1992, 1998, 2014, 2015 All rights reserved INTRODUCTION % It is assumed the

More information

MATH 353 Engineering mathematics III

MATH 353 Engineering mathematics III MATH 353 Engineering mathematics III Instructor: Francisco-Javier Pancho Sayas Spring 2014 University of Delaware Instructor: Francisco-Javier Pancho Sayas MATH 353 1 / 20 MEET YOUR COMPUTER Instructor:

More information

2 Computation with Floating-Point Numbers

2 Computation with Floating-Point Numbers 2 Computation with Floating-Point Numbers 2.1 Floating-Point Representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However, real numbers

More information

Classes of Real Numbers 1/2. The Real Line

Classes of Real Numbers 1/2. The Real Line Classes of Real Numbers All real numbers can be represented by a line: 1/2 π 1 0 1 2 3 4 real numbers The Real Line { integers rational numbers non-integral fractions irrational numbers Rational numbers

More information

Scientific Computing. Error Analysis

Scientific Computing. Error Analysis ECE257 Numerical Methods and Scientific Computing Error Analysis Today s s class: Introduction to error analysis Approximations Round-Off Errors Introduction Error is the difference between the exact solution

More information

Floating-point numbers. Phys 420/580 Lecture 6

Floating-point numbers. Phys 420/580 Lecture 6 Floating-point numbers Phys 420/580 Lecture 6 Random walk CA Activate a single cell at site i = 0 For all subsequent times steps, let the active site wander to i := i ± 1 with equal probability Random

More information

Data Types and Basic Calculation

Data Types and Basic Calculation Data Types and Basic Calculation Intrinsic Data Types Fortran supports five intrinsic data types: 1. INTEGER for exact whole numbers e.g., 1, 100, 534, -18, -654321, etc. 2. REAL for approximate, fractional

More information

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

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Program Development Cycle Program development

More information

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic September 28, 2018 Lecture 1 September 28, 2018 1 / 25 Floating point arithmetic Computers use finite strings of binary digits to represent

More information

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

Definition. A Taylor series of a function f is said to represent function f, iff the error term converges to 0 for n going to infinity.

Definition. A Taylor series of a function f is said to represent function f, iff the error term converges to 0 for n going to infinity. Definition A Taylor series of a function f is said to represent function f, iff the error term converges to 0 for n going to infinity. 120202: ESM4A - Numerical Methods 32 f(x) = e x at point c = 0. Taylor

More information

6. Control Statements II

6. Control Statements II Visibility Declaration in a block is not visible outside of the block. 6. Control Statements II Visibility, Local Variables, While Statement, Do Statement, Jump Statements main block int main () int i

More information

Graphics calculator instructions

Graphics calculator instructions Graphics calculator instructions Contents: A B C D E F G Basic calculations Basic functions Secondary function and alpha keys Memory Lists Statistical graphs Working with functions 10 GRAPHICS CALCULATOR

More information

Functions and Graphs. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

Functions and Graphs. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996. Functions and Graphs The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996. Launch Mathematica. Type

More information

Exercises C-Programming

Exercises C-Programming Exercises C-Programming Claude Fuhrer (claude.fuhrer@bfh.ch) 0 November 016 Contents 1 Serie 1 1 Min function.................................. Triangle surface 1............................... 3 Triangle

More information

CS321. Introduction to Numerical Methods

CS321. Introduction to Numerical Methods CS31 Introduction to Numerical Methods Lecture 1 Number Representations and Errors Professor Jun Zhang Department of Computer Science University of Kentucky Lexington, KY 40506 0633 August 5, 017 Number

More information

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy. Math 340 Fall 2014, Victor Matveev Binary system, round-off errors, loss of significance, and double precision accuracy. 1. Bits and the binary number system A bit is one digit in a binary representation

More information

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

Outline. Program development cycle. Algorithms development and representation. Examples. Outline Program development cycle. Algorithms development and representation. Examples. 1 Program Development Cycle Program development cycle steps: Problem definition. Problem analysis (understanding).

More information

Floating-point representation

Floating-point representation Lecture 3-4: Floating-point representation and arithmetic Floating-point representation The notion of real numbers in mathematics is convenient for hand computations and formula manipulations. However,

More information

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (

Table : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = ( Floating Point Numbers in Java by Michael L. Overton Virtually all modern computers follow the IEEE 2 floating point standard in their representation of floating point numbers. The Java programming language

More information

Basics of Computational Geometry

Basics of Computational Geometry Basics of Computational Geometry Nadeem Mohsin October 12, 2013 1 Contents This handout covers the basic concepts of computational geometry. Rather than exhaustively covering all the algorithms, it deals

More information

MST30040 Differential Equations via Computer Algebra Fall 2010 Worksheet 1

MST30040 Differential Equations via Computer Algebra Fall 2010 Worksheet 1 MST3000 Differential Equations via Computer Algebra Fall 2010 Worksheet 1 1 Some elementary calculations To use Maple for calculating or problem solving, the basic method is conversational. You type a

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

Binary floating point encodings

Binary floating point encodings Week 1: Wednesday, Jan 25 Binary floating point encodings Binary floating point arithmetic is essentially scientific notation. Where in decimal scientific notation we write in floating point, we write

More information

1 Week 1: Basics of scientific programming I

1 Week 1: Basics of scientific programming I MTH739N/P/U: Topics in Scientific Computing Autumn 2016 1 Week 1: Basics of scientific programming I 1.1 Introduction The aim of this course is use computing software platforms to solve scientific and

More information

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

Interpolation. TANA09 Lecture 7. Error analysis for linear interpolation. Linear Interpolation. Suppose we have a table x x 1 x 2... TANA9 Lecture 7 Interpolation Suppose we have a table x x x... x n+ Interpolation Introduction. Polynomials. Error estimates. Runge s phenomena. Application - Equation solving. Spline functions and interpolation.

More information

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

4 Visualization and. Approximation

4 Visualization and. Approximation 4 Visualization and Approximation b A slope field for the differential equation y tan(x + y) tan(x) tan(y). It is not always possible to write down an explicit formula for the solution to a differential

More information

2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices

2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices 2nd Year Computational Physics Week 1 (experienced): Series, sequences & matrices 1 Last compiled September 28, 2017 2 Contents 1 Introduction 5 2 Prelab Questions 6 3 Quick check of your skills 9 3.1

More information

Introduction to Scientific Computing Lecture 1

Introduction to Scientific Computing Lecture 1 Introduction to Scientific Computing Lecture 1 Professor Hanno Rein Last updated: September 10, 2017 1 Number Representations In this lecture, we will cover two concept that are important to understand

More information

2.1.1 Fixed-Point (or Integer) Arithmetic

2.1.1 Fixed-Point (or Integer) Arithmetic x = approximation to true value x error = x x, relative error = x x. x 2.1.1 Fixed-Point (or Integer) Arithmetic A base 2 (base 10) fixed-point number has a fixed number of binary (decimal) places. 1.

More information

Physics 331 Introduction to Numerical Techniques in Physics

Physics 331 Introduction to Numerical Techniques in Physics Physics 331 Introduction to Numerical Techniques in Physics Instructor: Joaquín Drut Lecture 2 Any logistics questions? Today: Number representation Sources of error Note: typo in HW! Two parts c. Call

More information

Lecture Notes on Contracts

Lecture Notes on Contracts Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,

More information

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

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill 12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring Chris Hill Review of Lecture 2 Examined computer hardware Computer basics and the main features of programs Program design:

More information

Roundoff Errors and Computer Arithmetic

Roundoff Errors and Computer Arithmetic Jim Lambers Math 105A Summer Session I 2003-04 Lecture 2 Notes These notes correspond to Section 1.2 in the text. Roundoff Errors and Computer Arithmetic In computing the solution to any mathematical problem,

More information

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch Computational Photonics Seminar 02, 30 April 2012 Programming in MATLAB controlling of a program s flow of execution branching loops loop control several programming tasks 1 Programming task 1 Plot the

More information

Bisecting Floating Point Numbers

Bisecting Floating Point Numbers Squishy Thinking by Jason Merrill jason@squishythinking.com Bisecting Floating Point Numbers 22 Feb 2014 Bisection is about the simplest algorithm there is for isolating a root of a continuous function:

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

Math 2250 Lab #3: Landing on Target

Math 2250 Lab #3: Landing on Target Math 2250 Lab #3: Landing on Target 1. INTRODUCTION TO THE LAB PROGRAM. Here are some general notes and ideas which will help you with the lab. The purpose of the lab program is to expose you to problems

More information

Top bar items. Current folder should be shown. Command Window This has the the prompt >>

Top bar items. Current folder should be shown. Command Window This has the the prompt >> MA1710: Week 2 for, if, else, break Getting started in session 2 With the Windows 7 operating system that we have in our Labs start Matlab as you did last week by clicking the mouse pointer on Start (bottom

More information

Basic stuff -- assignments, arithmetic and functions

Basic stuff -- assignments, arithmetic and functions Basic stuff -- assignments, arithmetic and functions Most of the time, you will be using Maple as a kind of super-calculator. It is possible to write programs in Maple -- we will do this very occasionally,

More information

Machine Computation of the Sine Function

Machine Computation of the Sine Function Machine Computation of the Sine Function Chuck Allison CNS 3320 Numerical Software Engineering Utah Valley State College February 2007 Abstract This note shows how to determine the number of terms to use

More information

Using Arithmetic of Real Numbers to Explore Limits and Continuity

Using Arithmetic of Real Numbers to Explore Limits and Continuity Using Arithmetic of Real Numbers to Explore Limits and Continuity by Maria Terrell Cornell University Problem Let a =.898989... and b =.000000... (a) Find a + b. (b) Use your ideas about how to add a and

More information

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

f( x ), or a solution to the equation f( x) 0. You are already familiar with ways of solving The Bisection Method and Newton s Method. If f( x ) a function, then a number r for which f( r) 0 is called a zero or a root of the function f( x ), or a solution to the equation f( x) 0. You are already

More information

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Lecture 6 COP 3014 Spring February 11, 2018 Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information

Python Programming: An Introduction to Computer Science

Python Programming: An Introduction to Computer Science Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers Python Programming, 3/e 1 Objectives n To understand the concept of data types. n To be familiar with the basic

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information

More information

Divide: Paper & Pencil

Divide: Paper & Pencil Divide: Paper & Pencil 1001 Quotient Divisor 1000 1001010 Dividend -1000 10 101 1010 1000 10 Remainder See how big a number can be subtracted, creating quotient bit on each step Binary => 1 * divisor or

More information

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

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple Chapter 2 Interactive MATLAB use only good if problem is simple Often, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated

More information

Unit testing with pytest and nose 1

Unit testing with pytest and nose 1 Unit testing with pytest and nose 1 Hans Petter Langtangen 1,2 1 Center for Biomedical Computing, Simula Research Laboratory 2 Department of Informatics, University of Oslo Mar 23, 2015 Contents 1 Requirements

More information

Lesson 4: Numerical Computations; Newton's method

Lesson 4: Numerical Computations; Newton's method Lesson 4: Numerical Computations; Newton's method restart; Catastrophic cancellation in the quadratic formula One case where roundoff error can be severe is if you subtract two numbers that are very close

More information

Outline. Numerical Analysis Basics. Some Higher-Level Languages. Programming Languages. BASIC and VBA MATLAB

Outline. Numerical Analysis Basics. Some Higher-Level Languages. Programming Languages. BASIC and VBA MATLAB umerical Analysis Basics Larry Caretto Mechanical Engineering 39 umerical Analysis of Engineering Systems February 5, 214 Outline Programming Languages Binary umbers and Data Types Limits on the size and

More information

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

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. Reals 1 13 Reals Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method. 13.1 Floating-point numbers Real numbers, those declared to be

More information

The Newton Method as a Short WEB Example

The Newton Method as a Short WEB Example The Newton Method as a Short WEB Example August 31, 1996 15:22 Abstract: This is written in John Krommes FWEB but with the output higher level language being C. FWEB is the most supported of the dialects

More information

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

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building

More information

Iteration. The final line is simply ending the do word in the first line. You can put as many lines as you like in between the do and the end do

Iteration. The final line is simply ending the do word in the first line. You can put as many lines as you like in between the do and the end do Intruction Iteration Iteration: the for loop Computers are superb at repeating a set of instructions over and over again - very quickly and with complete reliability. Here is a short maple program ce that

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

Numerical Methods Lecture 1

Numerical Methods Lecture 1 Numerical Methods Lecture 1 Basics of MATLAB by Pavel Ludvík The recommended textbook: Numerical Methods Lecture 1 by Pavel Ludvík 2 / 30 The recommended textbook: Title: Numerical methods with worked

More information

ITERATION AND RECURSION 3

ITERATION AND RECURSION 3 ITERATION AND RECURSION 3 COMPUTER SCIENCE 61A June 26, 2012 1 Newton s Method Newton s method is an algorithm that is widely used to compute the zeros of functions. It can be used to approximate a root

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up More on Scripts and Functions Basic Programming Lecture 2 Lecture 3 Lecture 4 Wrap Up Last time Loading data from file: load( filename ) Graphical input and

More information

Most nonzero floating-point numbers are normalized. This means they can be expressed as. x = ±(1 + f) 2 e. 0 f < 1

Most nonzero floating-point numbers are normalized. This means they can be expressed as. x = ±(1 + f) 2 e. 0 f < 1 Floating-Point Arithmetic Numerical Analysis uses floating-point arithmetic, but it is just one tool in numerical computation. There is an impression that floating point arithmetic is unpredictable and

More information

ME1107 Computing Y Yan.

ME1107 Computing Y Yan. ME1107 Computing 1 2008-2009 Y Yan http://www.staff.city.ac.uk/~ensyy About Fortran Fortran Formula Translation High level computer language Basic, Fortran, C, C++, Java, C#, (Matlab) What do we learn?

More information

SYSTEMS OF NONLINEAR EQUATIONS

SYSTEMS OF NONLINEAR EQUATIONS SYSTEMS OF NONLINEAR EQUATIONS Widely used in the mathematical modeling of real world phenomena. We introduce some numerical methods for their solution. For better intuition, we examine systems of two

More information

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

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information. Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Floating-Point Numbers in Digital Computers

Floating-Point Numbers in Digital Computers POLYTECHNIC UNIVERSITY Department of Computer and Information Science Floating-Point Numbers in Digital Computers K. Ming Leung Abstract: We explain how floating-point numbers are represented and stored

More information

Floating Point Arithmetic

Floating Point Arithmetic Floating Point Arithmetic CS 365 Floating-Point What can be represented in N bits? Unsigned 0 to 2 N 2s Complement -2 N-1 to 2 N-1-1 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678

More information

AMS 27L LAB #1 Winter 2009

AMS 27L LAB #1 Winter 2009 AMS 27L LAB #1 Winter 2009 Introduction to MATLAB Objectives: 1. To introduce the use of the MATLAB software package 2. To learn elementary mathematics in MATLAB Getting Started: Log onto your machine

More information

NOTE: Answer ANY FOUR of the following 6 sections:

NOTE: Answer ANY FOUR of the following 6 sections: A-PDF MERGER DEMO Philadelphia University Lecturer: Dr. Nadia Y. Yousif Coordinator: Dr. Nadia Y. Yousif Internal Examiner: Dr. Raad Fadhel Examination Paper... Programming Languages Paradigms (750321)

More information

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Statistics Case Study 2000 M. J. Clancy and M. C. Linn Statistics Case Study 2000 M. J. Clancy and M. C. Linn Problem Write and test functions to compute the following statistics for a nonempty list of numeric values: The mean, or average value, is computed

More information

Unavoidable Errors in Computing

Unavoidable Errors in Computing Unavoidable Errors in Computing Gerald W. Recktenwald Department of Mechanical Engineering Portland State University gerry@me.pdx.edu These slides are a supplement to the book Numerical Methods with Matlab:

More information

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB: Contents VARIABLES... 1 Storing Numerical Data... 2 Limits on Numerical Data... 6 Storing Character Strings... 8 Logical Variables... 9 MATLAB S BUILT-IN VARIABLES AND FUNCTIONS... 9 GETTING HELP IN MATLAB...

More information

Floating-Point Numbers in Digital Computers

Floating-Point Numbers in Digital Computers POLYTECHNIC UNIVERSITY Department of Computer and Information Science Floating-Point Numbers in Digital Computers K. Ming Leung Abstract: We explain how floating-point numbers are represented and stored

More information

Accuracy versus precision

Accuracy versus precision Accuracy versus precision Accuracy is a consistent error from the true value, but not necessarily a good or precise error Precision is a consistent result within a small error, but not necessarily anywhere

More information

MEI GeoGebra Tasks for A2 Core

MEI GeoGebra Tasks for A2 Core Task 1: Functions The Modulus Function 1. Plot the graph of y = x : use y = x or y = abs(x) 2. Plot the graph of y = ax+b : use y = ax + b or y = abs(ax+b) If prompted click Create Sliders. What combination

More information

C Program Structures

C Program Structures Review-1 Structure of C program Variable types and Naming Input and output Arithmetic operation 85-132 Introduction to C-Programming 9-1 C Program Structures #include void main (void) { } declaration

More information