Download presentation
Presentation is loading. Please wait.
Published byHubert Washington Modified over 9 years ago
1
Computational Physics Numerical Integration Dr. Guy Tel-Zur Tulips by Anna Cervova, publicdomainpictures.net Version 28-10-2010 16:00
2
MHJ- Chap. 7 – Numerical Integration Agenda – 1D integration (Trapezoidal and Simpson rules) – Gaussian quadrature (when the abscissas are not equally spaced)
3
Newton-Cotes quadrature: equal step methods Step size:
4
The strategy then is to find a reliable Taylor expansion for f(x) in the smaller sub intervals Let’s define x0 = a + h and use x0 as the midpoint. The general form for the Taylor expansion around x0 goes like: 7.2
5
Let us now suppose that we split the integral in Eq. (7.2) in two parts, one from x0−h to x0 and the other from x0 to x0 + h, that is, our integral is rewritten as: Next we assume that we can use the two-point formula for the derivative, meaning that we approximate f(x) in these two regions by a straight line, as indicated in the figure. This means that every small element under the function f(x) looks like a trapezoid. we are trying to approximate our function f(x) with a first order polynomial, that is f(x) = a + bx. The constant b is the slope given by the first derivative at x = x0:
6
The Trapezoidal Rule:
7
The trapezoidal rule algorithm
8
Trapezoidal Rule A worked out example in “C”. We will learn how to Integrate a general form of a function The code is under: lecture03/code_ch_07/trapez.c
9
// Computational Physics // Guy Tel-Zur, August 2010 // Trapezoidal Rule // Based on MHJ Page 129, Chapter 7, 2009 Fall Edition // Usage: standard gcc -o fn fn.c -lm #include int main() { // functions declarations: double TrapezoidalRule(double a, double b, int n, double(*func )(double)); double MyFunction(double x); // variables declarations: double a = 0.0; double b = 10.0; int n = 1000; double s; s = TrapezoidalRule(a, b, n, &MyFunction); printf("Trapezoidal Rule Integral=%Lf\n",s); return 0; } // end of main // Computational Physics // Guy Tel-Zur, August 2010 // Trapezoidal Rule // Based on MHJ Page 129, Chapter 7, 2009 Fall Edition // Usage: standard gcc -o fn fn.c -lm #include int main() { // functions declarations: double TrapezoidalRule(double a, double b, int n, double(*func )(double)); double MyFunction(double x); // variables declarations: double a = 0.0; double b = 10.0; int n = 1000; double s; s = TrapezoidalRule(a, b, n, &MyFunction); printf("Trapezoidal Rule Integral=%Lf\n",s); return 0; } // end of main
10
double TrapezoidalRule (double a, double b, int n, double(*func )(double)) { double TrapezSum; double fa,fb,x,step ; int j; step =(b-a)/((double)n ); fa =(*func)(a)/2.; fb =(*func)(b)/2.; TrapezSum= 0.; for(j=1;j<=n-1;j++) { x=j*step+a; TrapezSum+=(*func)(x); } TrapezSum=(TrapezSum+fb+fa)*step; return TrapezSum; } // end TrapezoidalRule double MyFunction(double x) { // write below the mathematical expression of the // function to be integrated return x*x; } double TrapezoidalRule (double a, double b, int n, double(*func )(double)) { double TrapezSum; double fa,fb,x,step ; int j; step =(b-a)/((double)n ); fa =(*func)(a)/2.; fb =(*func)(b)/2.; TrapezSum= 0.; for(j=1;j<=n-1;j++) { x=j*step+a; TrapezSum+=(*func)(x); } TrapezSum=(TrapezSum+fb+fa)*step; return TrapezSum; } // end TrapezoidalRule double MyFunction(double x) { // write below the mathematical expression of the // function to be integrated return x*x; }
11
Section 7.5 – Rectangle Rule Another very simple approach is the so-called midpoint or rectangle method. In this case the integration area is split in a given number of rectangles with length h and height given by the mid-point value of the function. This gives the following simple rule for approximating an integral Demo: ch7 code folder. Program: rectangle_trapez.c double RectangleRule (double a, double b, int n, double(*func )(double)) { double RectangleSum; double fa,fb,x,step ; int j; step =(b-a)/((double)n ); RectangleSum= 0.; for(j=0;j<=n;j++) { x=(j+0.5)*step+a; RectangleSum+=(*func)(x); } RectangleSum *= step; return RectangleSum; } // end Rectangle Rule double RectangleRule (double a, double b, int n, double(*func )(double)) { double RectangleSum; double fa,fb,x,step ; int j; step =(b-a)/((double)n ); RectangleSum= 0.; for(j=0;j<=n;j++) { x=(j+0.5)*step+a; RectangleSum+=(*func)(x); } RectangleSum *= step; return RectangleSum; } // end Rectangle Rule
12
Error analysis Trapezoidal & Rectangle rules: – Local error: α h^3 – Global error: α h^2 – Global error: α (b-a)
13
Can’t we do better? So far: Linear two-points approximations for f Now (Simpson’s): three-points formula (a parabola): Recall from Chapter 3, the 1 st and the 2 nd derivatives approximations: f’= f’’=
14
With the latter two expressions we can now approximate the function f as: I gave up using Equation Editor – Sorry… Next Slide… I gave up using Equation Editor – Sorry… Next Slide…
15
Simpson’s Rule
16
Note that the improved accuracy in the evaluation of the derivatives gives a better error approximation, O(h 5 ) vs. O(h 3 ). - This is the local error. The global error goes like: O(h 4 ). The full integral is therefore: Note that the improved accuracy in the evaluation of the derivatives gives a better error approximation, O(h 5 ) vs. O(h 3 ). - This is the local error. The global error goes like: O(h 4 ). The full integral is therefore:
17
Simpson’s Rule
18
7.3 Adaptive Integration Naïve approach, for example, two parts of Trapezoidal Integration with a different step size
19
Recursive Adaptive Integration
21
7.4 Gaussian Quadrature (GQ) We could fit a polynomial of order N with N equally spaced points, but Gauss, who was a genius, suggested to fit a polynomial of order 2N-1 with N points!!!
22
Recommended online video: http://physics.orst.edu/~rubin/COURSES/VideoLecs/Integration/Integration.html
23
Another recommended reference: Computational Physics By Steven E. Koonin So far – equally spaced points: Our derivation is exact if f is polynomial 4.9 Note: This is a set of N linear equations (for each p) !!! f(t) dt
24
Legendre Polynomials:
25
Guy: Scaling of the weights: ω n ’=(b-a)/2* ω n
26
Let’s sum what we did so far 2 points integration: Trapezoidal Rule (+Rectangle Rule),fit straight line 3 points integration: Simpson’s Rule, fit a parabola Quadrature Formula: Gauss Quadrature:
27
More Orthogonal Functions
29
Orthogonal polynomials, Legendre
31
from scitools.all import * import scipy.special as sp import numpy as np x=np.linspace(-1,1,100) for i in range (1,9): plot(x,sp.legendre(i)(x)) hold('on') print i axis([-1,1,-1,1]) title('Legendre Polynomials') show() hardcopy('fig1.png') print 'done!' from scitools.all import * import scipy.special as sp import numpy as np x=np.linspace(-1,1,100) for i in range (1,9): plot(x,sp.legendre(i)(x)) hold('on') print i axis([-1,1,-1,1]) title('Legendre Polynomials') show() hardcopy('fig1.png') print 'done!' Open spyder, code under: C:\Users\telzur\Documents\Weizmann\ScientificComputing\SC2011B\Lectures\03\co de\plot_legendre.py
33
The xi will be the zeros of the LN Now we will see how to find the weights wi
34
In a vector and a matrix form
37
The program1.cpp is a ready for comparing the 3 integrations methods mentioned. The Trapezoidal, Simpson’s and GQ function are to be written. >> Explain this program (open DevC++ IDE) The program1.cpp is a ready for comparing the 3 integrations methods mentioned. The Trapezoidal, Simpson’s and GQ function are to be written. >> Explain this program (open DevC++ IDE)
38
Integration in Python from scipy import integrate from scipy import sin,exp,inf x2 = lambda x: x**2 s1=integrate.quad(x2,0.,1.) invexp = lambda x: exp(-x) s2=integrate.quad(invexp,0,inf) print s1,s2 # Gaussian Quadrature (only for finite limits ) s3=integrate.quadrature(x2,0.,1.) print s3 xs = lambda x: sin(x)*exp(x) s5=integrate.quad(xs,-1,1) s6=integrate.quadrature(xs,-1,1) print s5,s6 Use Spyder. Execute int_demo.py under lecture3/code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.