1.00 Lecture 20 More on root finding Numerical lntegration.

Slides:



Advertisements
Similar presentations
1.00 Lecture 19 Numerical Methods: Root Finding. Remember Java® Data Types Type Size (bits) Range byte8-128 to 127 short16-32,768 to 32,767 int32-2,147,483,648.
Advertisements

Numerical Integration
Lecture 5 Newton-Raphson Method
Computational Physics Numerical Integration Dr. Guy Tel-Zur Tulips by Anna Cervova, publicdomainpictures.net.
Root Finding COS D Root Finding Given some function, find location where f ( x )=0Given some function, find location where f ( x )=0 Need:Need:
Computational Methods in Physics PHYS 3437
Chapter 7 Numerical Differentiation and Integration
Today’s class Romberg integration Gauss quadrature Numerical Methods
Numerical Integration
Chapter 4 Roots of Equations
CSCE Review—Fortran. CSCE Review—I/O Patterns: Read until a sentinel value is found Read n, then read n things Read until EOF encountered.
A few words about convergence We have been looking at e a as our measure of convergence A more technical means of differentiating the speed of convergence.
Lecture 3: Integration. Integration of discrete functions
Chapter 1 Introduction The solutions of engineering problems can be obtained using analytical methods or numerical methods. Analytical differentiation.
Numerical Integration
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM.
NUMERICAL DIFFERENTIATION The derivative of f (x) at x 0 is: An approximation to this is: for small values of h. Forward Difference Formula.
Quadrature Greg Beckham. Quadrature Numerical Integration Goal is to obtain the integral with as few computations of the integrand as possible.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Numerical Differentiation and Integration Part 6 Calculus.
MATH 685/ CSI 700/ OR 682 Lecture Notes Lecture 8. Nonlinear equations.
Parallel Numerical Integration Spring Semester 2005 Geoffrey Fox Community Grids Laboratory Indiana University 505 N Morton Suite 224 Bloomington IN
Chapter 4, Integration of Functions. Open and Closed Formulas x 1 =a x 2 x 3 x 4 x 5 =b Closed formula uses end points, e.g., Open formulas - use interior.
Numerical Integration In general, a numerical integration is the approximation of a definite integration by a “weighted” sum of function values at discretized.
Automatic Integration
3. Numerical integration (Numerical quadrature) .
Lecture 19 - Numerical Integration CVEN 302 July 22, 2002.
Simulation of Random Walk How do we investigate this numerically? Choose the step length to be a=1 Use a computer to generate random numbers r i uniformly.
1 Chapter 7 NUMERICAL INTEGRATION. 2 PRELIMINARIES We use numerical integration when the function f(x) may not be integrable in closed form or even in.
1 Numerical Analysis Lecture 12 Numerical Integration Dr. Nader Okasha.
MA2213 Lecture 4 Numerical Integration. Introduction Definition is the limit of Riemann sums I(f)
Chapter 17 Boundary Value Problems. Standard Form of Two-Point Boundary Value Problem In total, there are n 1 +n 2 =N boundary conditions.
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
CISE301_Topic11 CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4:
Lecture Notes Dr. Rakhmad Arief Siregar Universiti Malaysia Perlis
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
Review Taylor Series and Error Analysis Roots of Equations
Approximate computations Jordi Cortadella Department of Computer Science.
MA/CS 375 Fall MA/CS 375 Fall 2002 Lecture 31.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Roots of Equations ~ Open Methods Chapter 6 Credit:
4. Numerical Integration. Standard Quadrature We can find numerical value of a definite integral by the definition: where points x i are uniformly spaced.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 22.
5. Integration 2.Quadrature as Box Counting 3.Algorithm: Trapezoid Rule 4.Algorithm: Simpson’s Rule 5.Integration Error 6.Algorithm: Gaussian Quadrature.
Quadrature rules 1Michael Sokolov / Numerical Methods for Chemical Engineers / Numerical Quadrature Michael Sokolov ETH Zurich, Institut für Chemie- und.
Numerical Differentiation and Quadrature (Integration)
Lecture 5 - Single Variable Problems CVEN 302 June 12, 2002.
Numerical Methods Solution of Equation.
Techniques for Numerical Integration
INTRO TO OPTIMIZATION MATH-415 Numerical Analysis 1.
Computational Physics (Lecture 4) PHY4370. Random Variable, density function and distribution function.
Introduction to Computer Simulation of Physical Systems (Lecture 9) Numerical and Monte Carlo Methods PHYS 3061.
Introduction to Computer Simulation of Physical Systems (Lecture 10) Numerical and Monte Carlo Methods (CONTINUED) PHYS 3061.
Assignment 1: due 1/19/16 Estimate all of the zero of x3-x2-2x+1 graphically. Write a MatLab code for Newton’s method. Use your code to refine the graphical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 20 Numerical Integration of Functions.
Numerical Methods Some example applications in C++
Chapter 4, Integration of Functions
NUMERICAL DIFFERENTIATION Forward Difference Formula
Lecture 19 – Numerical Integration
Programming in C Paper – VIII B.
4. Numerical Integration
Chapter 22.
Taylor series in numerical computations (review)
Computational Lab in Physics: Numerical Integration
Numerical Analysis Lecture 45.
Chapter 7 Numerical Differentiation and Integration
Numerical Integration:
SKTN 2393 Numerical Methods for Nuclear Engineers
Comp 208 Computers in Engineering Yi Lin Winter, 2007
CISE-301: Numerical Methods Topic 1: Introduction to Numerical Methods and Taylor Series Lectures 1-4: KFUPM CISE301_Topic1.
Numerical Integration
Chapter 4, Integration of Functions
Presentation transcript:

1.00 Lecture 20 More on root finding Numerical lntegration

Newton’s Method Based on Taylor series expansion: f (x +δ) ≈ f (x) + f '(x)δ+ f ''(x)δ2/ –For small increment and smooth function, higher order derivatives are small and f ( x +δ ) = 0 implies δ= − f ( x )/ f '( x ) –If high order derivatives are large or 1st derivative is small, Newton can fail miserably –Converges quickly if assumptions met –Has generalization to N dimensions that is one of the few available –See Numerical Recipes for ‘safe’ Newton-Raphson method, which uses bisection when 1st derivative is small, etc.

Newton’s Method

Newton’s Method Pathology

Newton’s Method public class Newton { // NumRec, p. 365 public static double newt(MathFunction2 func, double a, double b, double epsilon) { double guess= 0.5*(a + b); for (int j= 0; j < JMAX; j++) { double fval= func.fn(guess); double fder= func.fd(guess); double dx= fval/fder; guess -= dx; System.out.println(guess); if ((a -guess)*(guess -b) < 0.0) { System.out.println("Error: out of bracket"); return ERR_VAL; // Experiment with this } // It’s conservative if (Math.abs(dx) < epsilon) return guess; } System.out.println("Maximum iterations exceeded"); return guess; }

Newton’s Method, p.2 public static int JMAX= 50; public static double ERR_VAL= -10E10 public static void main( String[] args) { double root = Newton.newt ( new FuncB(), -0.0, 8.0, ); System.out.println( “Root : ” + root); System.exit(0);} } Class FuncB implements MathFunction2{ public double fn(double x) { return x*x-2; } public double fd( double x) { return 2*x;} } public interface MathFunction2 { public double fn( double x); // Function value public double fd( double x); } // 1st derivative value

Examples f(x)= x2 + 1 –No real roots, Newton generates ‘random’ guesses f(x)= sin(5x) + x2 – 3 Root= –Bracket between –1 and 2, for example –Bracket between 0 and 2 will fail with conservative Newton (outside bracket) f(x)= ln(x2 –0.8x + 1) Roots= 0, 0.8 –Bracket between 0 and 1.2 works –Bracket between 0.0 and 8.0 fails Use Roots.java from lab to experiment!

Numerical Integration Classical methods are of historic interest only –Rectangular, trapezoid, Simpson’s –Work well for integrals that are very smooth or can be computed analytically anyway Extended Simpson’s method is only elementarymethod of some utility for 1-D integration Multidimensional integration is tough –If region of integration is complex but function values are smooth, use Monte Carlo integration –If region is simple but function is irregular, split integration into regions based on known sites of irregularity –If region is complex and function is irregular, or if sites of function irregularity are unknown, give up We’ll cover 1-D extended Simpson’s method only –See Numerical Recipes chapter 4 for more

Monte Carlo Integration z=f(x,y)

Finding Pi Randomly generate points in square 4r2. Odds that they’re in the circle are πr2 / 4r2, or π / 4. This is Monte Carlo Integration, with f(x,y)= 1 If f(x,y) varies slowly, then evaluate f(x,y) at each sample point in limits of integration and sum

FindPi public class FindPi { public static double getPi() { int count=0; for (int i=0; i < ; i++) { double x= Math.random() -0.5; // Ctr at 0,0 double y= Math.random() -0.5; if ((x*x + y*y) < 0.25) // If in region count++; // Increment integral } // More generally, eval f() return 4.0*count/ ; // Integral value } public static void main(String[] args) { System.out.println(getPi()); System.exit(0); }

Elementary Methods A= f(xleft)*h A= (f(xleft)+f(xright))*h/2 A= (f(xl)+4f(xm)+f(xr))*h/6

Elementary Methods class FuncA implements MathFunction { public double f ( double x) { return x*x*x*x +2;}} public class Integration { public static double rect ( MathFunction func, double a, double b, int n) { double h= (b-a)/n; double answer=0.0; for (int i=0; i<n; i++ ) answer += func.f (a+i*h); return h*answer;} public static double trap (MathFunction func, double a, double b, int n) } double h= (b-a)/n; double answer= func.f(a)/2.0; for ( int i=1;i<=n; i++) answer += func.f(a+i*h); answer -= func.f(b)/2.0; return h*answer;}

Elementary Methods, p.2 public static double simp(MathFunction func, double a, double b, int n) { // Each panel has area (h/6)*(f(x) + 4f(x+h/2) + f(x+h)) double h= (b-a)/n; double answer= func.f(a); for (int i=1; i <= n; i++) answer += 4.0*func.f(a+i*h-h/2.0)+ 2.0*func.f(a+i*h); answer -= func.f(b); return h*answer/6.0; } public static void main(String[] args) { double r= Integration.rect(new FuncA(), 0.0, 8.0, 200); System.out.println("Rectangle: " + r); double t= Integration.trap(new FuncA(), 0.0, 8.0, 200); System.out.println("Trapezoid: " + t); double s= Integration.simp(new FuncA(), 0.0, 8.0, 200); System.out.println("Simpson: " + s); System.exit(0); } } //Problems: no accuracy estimate, inefficient, only closed int

Better Trapezoid Rule Individual trapezoid approximation: Use this N-1 times for (x1, x2), (x2, x3), …(xN-1, xN) and add the results: results:

Better Trapezoid Rule

9Better Trapezoid Rule

Better Trapezoid Rule

Using Trapezoidal Rule Keep cutting intervals in half until desired accuracy met –Estimate accuracy by change from previousestimate –Each halving requires relatively little work because past work is retained By using a quadratic interpolation (Simpson’srule) to function values instead of linear (trapezoidal rule), we get better error behavior –By good fortune, errors cancel well with quadratic approximation used in Simpson’s rule –Computation same as trapezoid, but uses different weighting for function values in sum

Extended Trapezoid Method class Trapezoid {// NumRec p. 137 public static double trapzd( MathFunction func, double a, double b, int n) { if ( n==1) { s= 0.5*(b-a)*(func.f(a)+func.f(b)); return s;} else { int it=1; for( int j=0; j<n-2; j++) it *=2; double tnm= it; double delta= (b-a)/tnm; double x= a+0.5*delta; double sum= 0.0; for (int J=0;j<it; j++) { sum += func.f(x); x+= delta; } s= 0.5*(s+(b-a)*sum/tum);// Value of integral return s; }} private static double s;} // Current value of integral

Extended Simpson Method

public class Simpson { // NumRec p. 139 public static double qsimp(MathFunction func, double a, double b) double ost= -1.0E30; double os= -1E30; for (int j=0; j < JMAX; j++) { double st= Trapezoid.trapzd(func, a, b, j+1); s= (4.0*st -ost)/3.0; // See NumRec eq if (j > 4) // Avoid spurious early convergence if (Math.abs(s-os) < EPSILON*Math.abs(os) || (s==0.0 && os==0.0)) { System.out.println("Simpson iter: " + j); return s; } os= s; ost= st; } System.out.println("Too many steps in qsimp"); return ERR_VAL; } private static double s; public static final double EPSILON= 1.0E-15; public static final int JMAX= 50; public static final double ERR_VAL= -1E10; }

Using the Methods public static void main(String[] args) { // Simple example with just trapzd (see NumRec p. 137) System.out.println("Simple trapezoid use"); int m= 20; // Want 2^m+1 steps int j= m+1; double ans= 0.0; for (j=0; j <=m; j++) { // Must use Trapzd in loop! ans= Trapezoid.trapzd(new FuncC(), 0.0, 8.0, j+1); System.out.println("Iteration: " + (j+1) + “ Integral: " + ans); } System.out.println("Integral: " + ans); // Example using extended Simpson method System.out.println("Simpson use"); ans= qsimp(new FuncC(), 0.0, 8.0); System.out.println("Integral: " + ans); System.exit(0); } } // End Simpson class class FuncC implements MathFuction { public double f ( double x) { return x*x*x*x + 2; }}

Romberg Integration Generalization of Simpson (NumRec p. 140) –Based on numerical analysis to remove moreterms in error series associated with the numerical integral Uses trapezoid as building block as does Simpson –Romberg is adequate for smooth (analytic)integrands, over intervals with no singularities, where endpoints are not singular –Romberg is much faster than Simpson or theelementary routines. For a sample integral: Romberg: 32 iterations Simpson: 256 iterations Trapezoid: 8192 iterations

Improper Integrals Improper integral defined as havingintegrable singularity or approachinginfinity at limit of integration –Use extended midpoint rule instead of trapezoid rule to avoid function evaluations at singularities or infinities Must know where singularities or infinities are –Use change of variables: often replace x with1/t to convert an infinity to a zero Done implicitly in many routines Last improvement: Gaussian quadrature –In Simpson, Romberg, etc. the x values areevenly spaced. By relaxing this, we can getbetter efficiency and often better accuracy

Midpoint Rule