ENGINEERING MATHEMATICS 4 (BDA34003) Lecture Module 6: Numerical Integration Waluyo Adi Siswanto Universiti Tun Hussein Onn Malaysia This work is licensed under a Creative Commons Attribution 3.0 License.
Topics Rectangular rule Trapezoidal rule Simpson's rule Simpson 1/3 Simpson 3/8 Gauss Quadrature 2-point 3-point Lecture Module 6 BDA 34003 2
Learning Outcomes Students have the knowledge of effective numerical integration in engineering Students will be able to use various methods of numerical integrations to calculate complex problems with high accuracy without involving mathematical exact solution Students will be able to decide the appropriate numerical method to solve particular engineering problem Lecture Module 6 BDA 34003 3
y y(x) You want to calculate the yellow area, under the curve y(x) Then you can calculate the area x n Area= x 1 y(x)dx x 1 x n x Lecture Module 6 BDA 34003 4
y y 2 y n 1 y(x) If the function is difficult You will find it difficult to integrate. Then you can predict by approximation Area=Area 1+ Area 2+ Area 3 Area=h y 1 + h y 2 + h y n 1 y 1 Or you can write Area=h( y 1 + y 2 + y n 1 ) x 1 x n Of course not accurate. If you use smaller division? x Lecture Module 6 BDA 34003 5
Rectangular rule y n 1 y 2 y y(x) If you want to divide by N division h= x n x 1 N Number of data n = N + 1 y 1 x n x 1 y(x)dx = h( y 1 + y 2 + + y n 1) h h h x x 1 x n Lecture Module 6 BDA 34003 6
Trapezoidal rule y n y n 1 y 3 y 2 y y(x) If you want to divide by N division h= x n x 1 N Now the area is not rectangular but trapezoidal y 1 x n y(x)dx = h x 1 2 ( y + y )+ h 1 2 2 ( y + y )+ + h 2 3 2 ( y + y ) n 1 n h h h x n y(x)dx = h x 1 2 ( y + 2 y + + 2 y + y ) 1 2 n 1 n x x 1 x n Lecture Module 6 BDA 34003 7
Trapezoidal m function (trapezoidal.m): function I = trapezoidal(f_str, a, b, n) %TRAPEZOIDAL Trapezoidal Rule integration. % I = TRAPEZOIDAL(F_STR, A, B, N) returns the Trapezoidal Rule approximation % for the integral of f(x) from x=a to x=b, using N divisions (subintervals), where % F_STR is the string representation of f. I=0; g = inline(f_str); h = (b-a)/n; I = I + g(a); for ii = (a+h):h:(b-h) I = I + 2*g(ii); end I = I + g(b); I = I*h/2; Lecture Module 6 BDA 34003 8
Simpson's rule Simpson 1/3 x n y( x)dx = h x 1 3 ( y 1+ 4 y 2 + 2 y 3 + 4 y 4 + 2 y 5 + + 4 y n 1+ y n ) The number of division must be multiplication of 2 Simpson 3/8 x n x 1 y( x)dx = 3 h 8 ( y 1+ 3( y 2 + y 3 + y 5 + y 6 + )+ 2( y 4 + y 7 + )+ y n ) The number of division must be multiplication of 3 Lecture Module 6 BDA 34003 9
Simpson 1/3 m function (simpson13.m): function I = simpson13(f_str, a, b, n) %SIMPRULE Simpson's rule integration. % I = SIMPRULE(F_STR, A, B, N) returns the Simpson's rule approximation % for the integral of f(x) from x=a to x=b, using N subintervals, where % F_STR is the string representation of f. % An error is generated if N is not a positive, even integer. I=0; g = inline(f_str); h = (b-a)/n; if((n > 0) && (rem(n,2) == 0)) I = I + g(a); for ii = (a+h):2*h:(b-h) I = I + 4*g(ii); end for kk = (a+2*h):2*h:(b-2*h) I = I + 2*g(kk); end I = I + g(b); I = I*h/3; else disp('incorrect Value for N') end Lecture Module 6 BDA 34003 10
Simpson 3/8 m function (simpson38.m): function int=simpson38(f_str,x1,x2,n) h=(x2-x1)/n; x(1)=x1; f=inline(f_str); if((n > 0) && (rem(n,3) == 0)) sum=f(x1); for i=2:n x(i)=x(i-1)+h; end for j=2:3:n sum=sum+3*f(x(j)); end for k=3:3:n sum=sum+3*f(x(k)); end for l=4:3:n sum=sum+2*f(x(l)); end sum=sum+f(x2); int=sum*3*h/8; else disp('incorrect Value for N') end Lecture Module 6 BDA 34003 11
Example 6-1 Find the approximate value of 4 1 x x+ 1 dx By using : a) Exact integration, use SMath b) Rectangular rule, 12 division c) Trapezoidal rule, 12 division d) Simpson 1/3, 12 division e) Simpson 3/8, 12 division Lecture Module 6 BDA 34003 12
Exact integration in SMath Rectangular rule N = 12 h = 0.25 No data x x/sqrt(x+1) 1 1 0.70711 2 1.25 0.83333 3 1.5 0.94868 4 1.75 1.05529 5 2 1.15470 6 2.25 1.24808 7 2.5 1.33631 8 2.75 1.42009 9 3 1.50000 10 3.25 1.57648 11 3.5 1.64992 12 3.75 1.72062 13 4 15.15060 Result=0.25(15.15060)=3.78765 Lecture Module 6 BDA 34003 13
Trapezoidal rule N = 12 h = 0.25 No data x x/sqrt(x+1) 1 1 0.70711 2 1.25 0.83333 3 1.5 0.94868 4 1.75 1.05529 5 2 1.15470 6 2.25 1.24808 7 2.5 1.33631 8 2.75 1.42009 9 3 1.50000 10 3.25 1.57648 11 3.5 1.64992 12 3.75 1.72062 13 4 1.78885 2.49596 14.44350 In FreeMat: Result= 0.25 2 (2.49596+2(14.44350))=3.9229 Lecture Module 6 BDA 34003 14
Simpson 1/3 rule N = 12 h = 0.25 No data x x/sqrt(x+1) 1 1 0.70711 2 1.25 0.83333 3 1.5 0.94868 4 1.75 1.05529 5 2 1.15470 6 2.25 1.24808 7 2.5 1.33631 8 2.75 1.42009 9 3 1.50000 10 3.25 1.57648 11 3.5 1.64992 12 3.75 1.72062 13 4 1.78885 2.49596 7.85389 6.58961 In FreeMat : Result= 0.25 3 (2.49596+4(7.85389)+2(6.48961))=3.9242 Lecture Module 6 BDA 34003 15
Simpson 3/8 rule N = 12 h = 0.25 No data x x/sqrt(x+1) 1 1 0.70711 2 1.25 0.83333 3 1.5 0.94868 4 1.75 1.05529 5 2 1.15470 6 2.25 1.24808 7 2.5 1.33631 8 2.75 1.42009 9 3 1.50000 10 3.25 1.57648 11 3.5 1.64992 12 3.75 1.72062 13 4 1.78885 2.49596 10.47542 3.96808 In FreeMat : Result= (3)0.25 8 (2.49596+ 3(10.47542)+ 2(3.96808))=3.9242 Lecture Module 6 BDA 34003 16
Example 6-2 A racing car velocity record from start to 12 seconds is shown below. You have to calculate the distance of the car from its start position in 12 seconds. The data is taken every 1 second. Use Simpson's rule 1/3. 180 160 150 160 170 170 170 140 120 110 speed (km/h) 100 80 60 50 60 70 75 80 90 40 20 0 0 0 2 4 6 8 10 12 time (seconds) Lecture Module 6 BDA 34003 17
N = 12 h= 1/3600 no t v(t) 1 0 0 2 1 50 3 2 60 4 3 70 5 4 75 6 5 80 7 6 90 8 7 110 9 8 150 10 9 160 11 10 170 12 11 170 13 12 170 170 640 545 Result= 1 (170+ 4(640)+ 2(545))=0.3537 km 3 3600 Lecture Module 6 BDA 34003 18
Gauss Quadrature The main idea in Gauss Quadrature is to change the integration limits to natural (dimensionless) coordinate limits from -1 to 1 y y(x) ϕ x 1 ϕ(ξ) x n x Change to natural coordinates 1 1 Lecture Module 6 BDA 34003 19 ξ
Gauss Quadrature The main idea in Gauss Quadrature is to change the integration limits to natural (dimensionless) coordinate limits from -1 to 1 y y(x) x ξ = 1 2 [ (1 ξ) x 1 + (1+ ξ) x n ] ϕ(ξ)= y(x ξ ) ϕ x 1 ϕ(ξ) x n x I = x 1 n x 1 ϕ(ξ)d ξ 2 1 1 1 Lecture Module 6 BDA 34003 20 ξ I = x n x 1 2 I ξ
1 I ξ = ϕ(ξ)d ξ 1 I ξ = R 1 ϕ(ξ 1 ) + R 2 ϕ(ξ 2 ) + + R n ϕ(ξ n ) ξ j R j is the location of the integration point j relative to the center is the weighting factor for point j relative to the center, and n is the number of points at which ϕ(ξ) is to be calculated Lecture Module 6 BDA 34003 21
Gauss Quadrature Coefficients for Gaussian Quadrature n ±ξ j R j 1 0.0 2.0 2 0.5773502692 1.0 3 0.7745966692 0.555555556 0.0 0.888888889 4 0.8611363116 0.3478548451 0.3399810436 0.6521451549 5 0.9061798459 0.2369268851 0.5384693101 0.4786286705 0.0 0.5688888889 Lecture Module 6 BDA 34003 22
Program to generate abscissa and weight for any number of integration points function [x,a] = GaussNodes(n,tol) % USAGE: [x,a] = GaussNodes(n,tol) % n = order of integration points % tol = error tolerance (default is 1.0e4*eps). format long; if nargin < 2; tol = 1.0e4*eps; end A = zeros(n,1); x = zeros(n,1); nroots = fix(n + 1)/2; for i = 1:nRoots t = cos(pi*(i - 0.25)/(n + 0.5)); for j = i: 30 p0 = 1.0; p1 = t; for k = 1:n-1 p = ((2*k + 1)*t*p1 - k*p0)/(k + 1); p0 = p1;p1 = p; end dp = n *(p0 - t*p1)/(1 - t^2); dt = -p/dp; t = t + dt; if abs(dt) < tol x(i) = t; x(n-i+1) = -t; A(i) = 2/(1-(t^2))/(dp^2); A(n-i+1) = A(i); break end end if nroots == 1 x(i) =0; end end Lecture Module 6 BDA 34003 23
Program to use Gauss Quadrature, any number of integration points function I = GaussQuadrature(func,a,b,n) % USAGE: I = gaussquad(func,a,b,n) % func = handle of function to be integrated. % for example --> func= @(x) ((sin(x)/x)^2) % a,b = integration limits. % n = order of integration points % I = integral result format long; c1 = (b + a)/2; c2 = (b - a)/2; [x,a] = GaussNodes(n); sum = 0; for i = 1:length(x) y = feval(func,c1 + c2*x(i)); sum = sum + A(i)*y; end I = c2*sum; Lecture Module 6 BDA 34003 24
ϕ ϕ(ξ) Gauss Quadrature 1 point 0.0 1 1 2.0 ξ weighting I ξ = R 1 ϕ(ξ 1 ) I ξ = 2.0 ϕ(0) Lecture Module 6 BDA 34003 25
ϕ ϕ(ξ) Gauss Quadrature 2 points -0.5773502692 0.0 0.5773502692 1 1 1.0 1.0 ξ weighting I ξ = R 1 ϕ(ξ 1 ) + R 2 ϕ(ξ 2 ) I ξ = 1.0 ϕ( 0.5773502692) + 1.0 ϕ(0.5773502692) Lecture Module 6 BDA 34003 26
ϕ ϕ(ξ) Gauss Quadrature 3 points -0.7745966692 0.0 0.7745966692 1 1 0.888888889 0.555555556 0.555555556 ξ weighting I ξ = R 1 ϕ(ξ 1 ) + R 2 ϕ(ξ 2 ) + R 3 ϕ(ξ 3 ) I ξ = 0.555555556 ϕ( 0.7745966692) + 0.888888889 ϕ(0.0) + 0.555555556 ϕ(0.7745966692) Lecture Module 6 BDA 34003 27
Example 6-3 Find the approximate value of 4 1 x x+ 1 dx By using : a) Gauss Quadrature 2 points b) Gauss Quadrature 3 points Lecture Module 6 BDA 34003 28
Lecture Module 6 BDA 34003 29
Lecture Module 6 BDA 34003 30
Example 6-4 Find the approximate value of 4 1 1+ x 2 x 3 + 1 dx By using : a) Gauss Quadrature 4 points b) Check your result in Smath (Exact integration) c) Check your result, using Freemat (GaussQuadrature) Lecture Module 6 BDA 34003 31
Lecture Module 6 BDA 34003 32
Lecture Module 6 BDA 34003 33
Student Activity Lecture Module 6 BDA 34003 34
Problem 6-A1 A military radar detected an unidentified flying object and found that the vertical velocity (m/s) can be expressed in a polynomial function, v(t)=20 t 3 +48t 2 +14 t If the flying object starts moving from the ground to the sky, calculate the distance of the flying object when it moves 10 seconds from the ground, by using (a) (b) 3-points Gauss quadrature. Simpson s rule with 10 divisions. (you have to select the appropriate Simpson's method) Lecture Module 6 BDA 34003 35
Problem 6-A2 The velocity of a particle (m/s) at each 4 seconds time t, is measured and the recorded data is shown below Time 0 4 8 12 16 20 24 velocity 13.27 25.44 30.19 31.80 32.25 32.92 32.89 Time 28 32 36 40 44 48 velocity 32.24 32.16 29.74 30.38 28.06 29.94 a) Find the distance travel by the particle from 0 to 48 second by using trapezoidal rule b) Use Smath - Generate the polynomial function from 13 data then calculate the distance by Integrating it from 0 to 4 seconds Lecture Module 6 BDA 34003 36
Lecture Module 6 BDA 34003 37