Numerical methods. Examples with solution

Similar documents
Hence a root lies between 1 and 2. Since f a is negative and f(x 0 ) is positive The root lies between a and x 0 i.e. 1 and 1.

M.SC. PHYSICS - II YEAR

Exam in TMA4215 December 7th 2012

(f(x) P 3 (x)) dx. (a) The Lagrange formula for the error is given by

TABLE OF CONTENTS INTRODUCTION, APPROXIMATION & ERRORS 1. Chapter Introduction to numerical methods 1 Multiple-choice test 7 Problem set 9

LECTURE NOTES ELEMENTARY NUMERICAL METHODS. Eusebius Doedel

Previous Year Questions & Detailed Solutions

Review. Numerical Methods Lecture 22. Prof. Jinbo Bi CSE, UConn

Department of Applied Mathematics and Theoretical Physics. AMA 204 Numerical analysis. Exam Winter 2004

COURSE Numerical integration of functions (continuation) 3.3. The Romberg s iterative generation method

Jim Lambers MAT 460/560 Fall Semester Practice Final Exam

BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination December, 2015 BCS-054 : COMPUTER ORIENTED NUMERICAL TECHNIQUES

COURSE Numerical integration of functions

Today s class. Linear Algebraic Equations LU Decomposition. Numerical Methods, Fall 2011 Lecture 8. Prof. Jinbo Bi CSE, UConn

x x2 2 + x3 3 x4 3. Use the divided-difference method to find a polynomial of least degree that fits the values shown: (b)

AIMS Exercise Set # 1

5. Hand in the entire exam booklet and your computer score sheet.

MTH603 FAQ + Short Questions Answers.

Virtual University of Pakistan

Numerical Methods in Physics and Astrophysics

Numerical Methods in Physics and Astrophysics

5.7 Cramer's Rule 1. Using Determinants to Solve Systems Assumes the system of two equations in two unknowns

Introduction to Numerical Analysis

Mathematical Methods for Numerical Analysis and Optimization

Exact and Approximate Numbers:

TMA4125 Matematikk 4N Spring 2017

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF MATHEMATICS ACADEMIC YEAR / EVEN SEMESTER QUESTION BANK

Examination paper for TMA4125 Matematikk 4N

Applied Numerical Analysis

Math 5630: Iterative Methods for Systems of Equations Hung Phan, UMass Lowell March 22, 2018

MATHEMATICAL METHODS INTERPOLATION

PARTIAL DIFFERENTIAL EQUATIONS

Numerical and Statistical Methods

Introductory Numerical Analysis

Numerical Marine Hydrodynamics Summary

Numerical and Statistical Methods

Fourth Order RK-Method

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

INSTITUTE OF AERONAUTICAL ENGINEERING Dundigal, Hyderabad MECHANICAL ENGINEERING TUTORIAL QUESTION BANK

1 Solutions to selected problems

Math Numerical Analysis Mid-Term Test Solutions

Integration. Topic: Trapezoidal Rule. Major: General Engineering. Author: Autar Kaw, Charlie Barker.

COURSE Iterative methods for solving linear systems

Two hours. To be provided by Examinations Office: Mathematical Formula Tables. THE UNIVERSITY OF MANCHESTER. 29 May :45 11:45

Midterm Review. Igor Yanovsky (Math 151A TA)

NUMERICAL METHODS USING MATLAB

Examination paper for TMA4130 Matematikk 4N

Do not turn over until you are told to do so by the Invigilator.

Fundamental Numerical Methods for Electrical Engineering

Tutorial 1, B. Tech. Sem III, 24 July, (Root Findings and Linear System of Equations)

A = (a + 1) 2 = a 2 + 2a + 1

Solutions to Exam 2, Math 10560

Midterm for Introduction to Numerical Analysis I, AMSC/CMSC 466, on 10/29/2015

School of Sciences Indira Gandhi National Open University Maidan Garhi, New Delhi (For January 2012 cycle)

Preliminary Examination, Numerical Analysis, August 2016

(0, 0), (1, ), (2, ), (3, ), (4, ), (5, ), (6, ).

NUMERICAL ANALYSIS I. MARTIN LOTZ School of Mathematics The University of Manchester. May 2016

STATISTICS AND NUMERICAL METHODS

Chapter 5: Numerical Integration and Differentiation

USHA RAMA COLLEGE OF ENGINEERING & TECHNOLOGY

Numerical Methods with MATLAB

Page No.1. MTH603-Numerical Analysis_ Muhammad Ishfaq

Homework and Computer Problems for Math*2130 (W17).

Unit I (Testing of Hypothesis)

Numerical Analysis: Solutions of System of. Linear Equation. Natasha S. Sharma, PhD

Applied Numerical Analysis (AE2220-I) R. Klees and R.P. Dwight

Suggested solutions, TMA4125 Calculus 4N

Numerical Analysis Solution of Algebraic Equation (non-linear equation) 1- Trial and Error. 2- Fixed point

CHAPTER 8: Matrices and Determinants

Examination paper for TMA4122/TMA4123/TMA4125/TMA4130 Matematikk 4M/N

CS 257: Numerical Methods

Practice Problems for the Final Exam

1 Lecture 8: Interpolating polynomials.

Review for Exam 2 Ben Wang and Mark Styczynski

b n x n + b n 1 x n b 1 x + b 0

Numerical Methods for Engineers. and Scientists. Applications using MATLAB. An Introduction with. Vish- Subramaniam. Third Edition. Amos Gilat.

ASSIGNMENT BOOKLET. Numerical Analysis (MTE-10) (Valid from 1 st July, 2011 to 31 st March, 2012)

Root Finding: Close Methods. Bisection and False Position Dr. Marco A. Arocha Aug, 2014

SOLUTION OF EQUATION AND EIGENVALUE PROBLEMS PART A( 2 MARKS)

Solutions Preliminary Examination in Numerical Analysis January, 2017

8.7 Taylor s Inequality Math 2300 Section 005 Calculus II. f(x) = ln(1 + x) f(0) = 0

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Subbalakshmi Lakshmipathy College of Science. Department of Mathematics

Lösning: Tenta Numerical Analysis för D, L. FMN011,

Basics of Calculus and Algebra

University of Houston, Department of Mathematics Numerical Analysis, Fall 2005

NUMERICAL ANALYSIS. K.D. Anderson J.S.C. Prentice C.M. Villet

Iterative Methods. Splitting Methods

Lecture 9 Approximations of Laplace s Equation, Finite Element Method. Mathématiques appliquées (MATH0504-1) B. Dewals, C.

EAD 115. Numerical Solution of Engineering and Scientific Problems. David M. Rocke Department of Applied Science

Name of the Student: Unit I (Solution of Equations and Eigenvalue Problems)

Review of matrices. Let m, n IN. A rectangle of numbers written like A =

Week 1: need to know. November 14, / 20

Lecture Notes on Introduction to Numerical Computation 1

ALGEBRA II WITH TRIGONOMETRY EXAM

ECONOMICS 207 SPRING 2006 LABORATORY EXERCISE 5 KEY. 8 = 10(5x 2) = 9(3x + 8), x 50x 20 = 27x x = 92 x = 4. 8x 2 22x + 15 = 0 (2x 3)(4x 5) = 0

. (a) Express [ ] as a non-trivial linear combination of u = [ ], v = [ ] and w =[ ], if possible. Otherwise, give your comments. (b) Express +8x+9x a

MA2501 Numerical Methods Spring 2015

Note: Final Exam is at 10:45 on Tuesday, 5/3/11 (This is the Final Exam time reserved for our labs). From Practice Test I

MATH 163 HOMEWORK Week 13, due Monday April 26 TOPICS. c n (x a) n then c n = f(n) (a) n!

Transcription:

Numerical methods Examples with solution

CONTENTS Contents. Nonlinear Equations 3 The bisection method............................ 4 Newton s method.............................. 8. Linear Systems LU-factorization.............................. Jacobi iterative method.......................... 3 Gauss-Seidel iterative method....................... 5 3. Interpolation and approximation of functions 7 Polynom interpolation........................... 8 The least square method.......................... 0 4. Numerical integration Trapezoid rule................................ 3 Composite trapezoid rule.......................... 5 Simpson s rule................................ 6 Composite Simpson s rule......................... 8 5. Initial value problems for ODEs 3 Euler s method............................... 3 Runge-Kutta method............................ 34

. NONLINEAR EQUATIONS. Nonlinear Equations 3

. NONLINEAR EQUATIONS Example.. Find the roots of equation by bisection method: x 3 = ln(0 x) with the precision ε = 0 3. Solution: 70 60 50 40 30 0 0 0 0 x 3 ln(0 x) 0 0 3 4 x f(x) -.97. -0.855. -0.4468.3 0.0337.4 0.59.5.349.6.9678.7.7967.8 3.779.9 4.767 5.906 It seen the root (x-intercept) lies in the interval, (from the graph,). The function values are tabulated on this interval f(x) = x 3 ln(0 x) and is found the sign of functional values are changing between. and.3. The function is continuous on a given interval. The equation has one root in the interval.,.3. 4

. NONLINEAR EQUATIONS The root are computed in the interval.,.3, thus a 0 =., b 0 =.3. Compute the first approximation x : x = b0 + a 0 =.3 +. =.5. Compute the functional values of the function f(x) = x 3 ln(0 x) in points a 0, x, b 0 : f(a 0 ) = 0.446, f(x ) = 0.59, f(b 0 ) = 0.0337 and find the interval a, b : f(a 0 )f(x ) 0 a = x 0 =.5, b = b 0 =.3. Find the approximation error b0 a 0 = 0.05 ε = 0 3. The computation continues. Compute the next approximation x = a + b =.5 +.3 =.75 and find the interval a, b : f(a ) = 0.59, f(x ) = 0.0935, f(b ) = 0.0337, f(a )f(x ) 0 a = x =.75 b = b =.3. Find the approximation error b a = 0.05 ε = 0 3. The computation continues. Compute the next approximation x 3 = a + b =.75 +.3 =.875 and find the interval a 3, b 3 : f(a ) = 0.0935, f(x 3 ) = 0.0305, f(b ) = 0.0337, f(a ) f(x 3 ) 0 a 3 = x 3 =.875, b 3 = b =.3. 5

. NONLINEAR EQUATIONS Find the approximation error b a = 0.05 ε = 0 3. The computation continues. Compute the next approximation x 4 = a3 + b 3 =.875 +.3 =.938 and find the interval a 4, b 4 : f(a 3 ) = 0.0305, f(x 3 ) = 0.004, f(b 3 ) = 0.0337, f(a 3 )f(x 3 ) < 0 a 4 = a 3 =.875, b 4 = x 3 =.938 Find the approximation error. b 3 a 3 = 0.006 ε = 0 3. The computation continues. Compute the next approximation x 4 : x 4 = a4 + b 4 =.875 +.938 =.906 and find the interval a 5, b 5 : f(a 4 ) = 0.0305 f(x 4 ) = 0.046 f(b 4 ) = 0.004 f(a 4 ) f(x 4 ) 0 a 5 = x 4 =.906, b 5 = b 4 =.938 Find the approximation error. b 4 a 4 = 0.003 ε = 0 3. The computation continues. Compute the next approximation x 5 : x 5 = a5 + b 5 =.906 +.938 =.9 and find the interval a 6, b 6 : f(a 5 ) = 0.046 f(x 5 ) = 0.0066 f(b 5 ) = 0.004 f(a 5 ) f(x 5 ) > 0 a 6 = x 5 =.906, b 6 = b 5 =.938 Find the approximation error b4 a 4 = 0.006 ε = 0 3. The computation continues. 6

. NONLINEAR EQUATIONS Compute the next approximation x 4 : x 6 = a6 + b 6 =.9 +.938 =.93 Find the approximation error b6 a 6 = 0.0008 < ε = 0 3. The desired precision is satisfied. All information are written to the table: i a i f(a i ) x i f(x i ) b i f(b i ) b i a i / 0..5.3 + 0.05.5.75.3 + 0.05.75.875.3 + 0.05 3.875.938 +.3 + 0.006 4.875.906.938 + 0.003 5.906.9.938 + 0.006 6.9.930.938 + 0.0008 The root of the equation is: ξ =.93 ± 0.00 Calculation in Matlab: a=., b=.3 f=inline( x^3-log(0-x) ) x=(a+b)/ while abs(b-a)/ > e-3 if f(a)*f(x)<0;b=x;else a=x;end; x=(a+b)/ end given interval definition of a function f(x) initial approximation approximations in the calculation of cycle 7

. NONLINEAR EQUATIONS Example.. Find the roots of equation by Newton s method: x 3 = ln(0 x) with the pecision ε = 0 6. Solution: The equation has one root in the interval.,.3 (see the Example..). Compute the the first and second derivative: f(x) = x 3 ln(0 x), f (x) = 3 x + 0 x, f (x) = 6 x + (0 x), Verify assumptions of method: f(a) f (a) = 0.4468 4.4336 = 0.008 0. = b a and it is seen that the condition is not satisfied. Reduce the interval in which the search root is. 0. 0. 0 0. 0. 0.3 0.4 x 3 ln(0 x) It is seen that the root lies in the interval.5,.3 (from the graph). 0.5..5.5.75.3 And again, verify the method conditions, this time in the interval.5,.3 : f(a) f (a) = 0.59 = 0.0450 < 0.05 = b a 4.808 f(b) f (b) = 0.0337 = 0.0065 < 0.05 = b a 5.849 8

. NONLINEAR EQUATIONS x i f (x i ) f (x i ).5 4.808 7.53.6 4.877 7.573.7 4.953 7.633.8 5.099 7.693.9 5.07 7.753.3 5.849 7.83 It is seen from the first and second derivative values in the interval.5,.3 : Find the initial approximation x 0 = b =.3. Compute the next approximation x : f (x) > 0 na.5,.3 f (x) > 0 na.5,.3. The Newton method conditions are satisfied. x = x 0 f(x 0) 0.03367697433946 =.3 f (x 0 ) 5.84945873563 =.9350485098864 and the approximation error x x 0 = 0.00649549036 ε = 0 6. The computation continues. Compute the next approximation x : x = x f(x ) f (x ) =.9350485098864 0.0006453367995 5.3437883453 =.93478053989 and the approximation error x x = 0.0000304584875 ε = 0 6. The computation continues. Compute the next approximation x 3 : x 3 = x f(x ) f (x ) =.93478053989 0.0000000039978 5.340705040059 =.93478043638 and the approximation error x x 0 = 0.0000000007775 = 7.775 0 0 < ε = 0 6. The desired precision is satisfied. All information are written to the table: i x i x i x i 0.3.9350485098864 0.00649549036.93478053989 0.0000304584875 3.93478043638 0.0000000007775 9

. NONLINEAR EQUATIONS The root of the equation is: ξ =.93473 ± 0 6 Calculation in Matlab: f=inline( x^3-log(0-x) ) input the function f(x) fd=inline( 3*x^+./(0-x) ) input the first derivative f (x) xs=.3 initial approximation x=xs-f(xs)/fd(xs) the next approximation computation while abs(xs-x)>e-6 approximations in the cycle computation xs=x; x=xs-f(xs)/fd(xs) end 0

. LINEAR SYSTEMS. Linear Systems

. LINEAR SYSTEMS Example.. The matrix is given. 3 A = 6 9 0 3 9 5. Compute the LU-decomposition A = LU by using Gaussian elimination method with direct choice of main element. Using the LU-decomposition,compute the solution of a system of linear equations Ax = b,where b = ( 9, 59, 8), inverse matrix A and determinant deta. Solution: Compute by using Gaussian elimination method: 3 6, 3 3 6 9 0 0 3 9 5 0 0 0 3 0 0 0 The matrix U is the result of the Gaussian elimination method, the matrix L we prepare of multipliers, which are written next to matrices : 0 0 3 L = 6 0, U = 0. 3 0 0 0 In solving Ax = b first compute y from Ly = b and then from Ux = y compute x. y = 9 9 Weget : 6y +y = 59 y = 5, 3y +y 3 = 8 x 3x +x 3 = 9 x x 3 = 5 x = 3. x 3 =

. LINEAR SYSTEMS Inverse matrix is calculated by solving the system Aa i = e i, i =,, 3, where e i are the columns of the unit matrix I = (e,e,e 3 ). The results a i are the columns of inverse matrix, i.e. A = (a,a,a 3 ). The solution of systems is again decomposed into two steps. y =, 0, 0 0 0 6y +y = 0,, 0 Y = 6 0, 3y +y 3 = 0, 0, 3 0 x 3x +x 3 =, 0, 0 5 3 8 x x 3 = 6,, 0 A = 0. x 3 =, 0, 3 0 Determinant det A be calculated as the product of the diagonal elements of the matrix L and U: deta = detldetu = ( ) (( ) ( ) ) =. Example.. Solve the system of linear equations x +x 3x 3 = 4, x +5x +x 3 = 5, 4x x +x 3 =, using Jacobi iterative method with the pecision ǫ = 0. Solution: To ensure convergence, modify the system in form with strong diagonal dominant matrix. Replace the first and third equation 4x x +x 3 =, x +5x +x 3 = 5, x +x 3x 3 = 4. 3

. LINEAR SYSTEMS The modified system is rewrite to iterative form x = 4 ( + x x 3 ), x = 5 (5 x x 3 ), x 3 = 3 ( 4 x x ) The recurrent formulas for Jacobi method are obtained by iteration indices. x (k+) = ( + x(k) 4 x (k) 3 ), x (k+) = (5 x(k) 5 x (k) 3 ), x (k+) 3 = ( 4 x(k) 3 x (k) ). The computation converges for any initial approximation x (0) = (x (0), x (0), x (0) 3 ), so the following initial approximation is choosen x (0) = ( 0, 0, 0 ). Substitute x (0) into the right side of the recurrent formulas. Compute continue equally x () = ( 3,, 4 3 ). x () = ( 4, 9 5, 3 ), x (3) = (.8500,.333, 0.8389 ), atd. Find that the true is x (k) x (k ) R 0 3, compute thus x () x (0) R = max{ 3 0, 0, 4 0 } = 3, 3 x () x () R = max{ 4 + 3, 9, 5 4 4 } =, 3 3 5 x (3) x () R = 0.5667, etc. The results are written to the table: Stop after the eleventh iteration, because x () x (0) R = 0.0077 0 3, and the result is x = 3.00 ± 0, x =.00 ± 0, x 3 =.00 ± 0. 4

. LINEAR SYSTEMS k x (k) x (k) x (k) 3 x (k) x (k ) R 0 0 0 0 3.0000.0000.3333 3.0000 3.467.9333 0.6667 0.9333 3.8500.333 0.8389 0.5667 4.86.97.78 0.889 5 3.0708.989.0370 0.097 6 3.0388.009 0.9494 0.00 7.9694.056 0.9940 0.0694 8.9906.9890.087 0.0367 9 3.0.995 0.9995 0.05 0 3.0.995 0.9995 0.05.9955.009.00 0.0077 Example.3. Solve the system of linear equations x +x 3x 3 = 4, x +5x +x 3 = 5, 4x x +x 3 =, using the Gauss-Seidel iterative methods with the pecision ǫ = 0. Solution: To ensure convergence, modify the system in form with strong diagonal dominant matrix. Replace the first and third equation 4x x +x 3 =, x +5x +x 3 = 5, x +x 3x 3 = 4. The modified system is rewrite to iterative form x = ( + x 4 x 3 ), x = (5 x 5 x 3 ), x 3 = ( 4 x 3 x ) 5

. LINEAR SYSTEMS The recurrent formulas for Jacobi method are obtained by iteration indices. x (k+) = ( + x(k) 4 x (k) 3 ), x (k+) = (5 x(k+) 5 x (k) 3 ), x (k+) 3 = ( 4 x(k+) 3 x (k+) ). The computation converges for any initial approximation x (0) = (x (0), x (0), x (0) 3 ), so the following initial approximation is choosen x (0) = ( 0, 0, 0 ). Substitute x (0) into the right side of the recurrent formulas. Compute x () = ( 3.0000,.000,.0667 ). continue equally x () = (.9833,.9800, 0.9989 ), atd. Find that the true is x (k) x (k ) R 0, compute thus x () x (0) R = max{ 3 0,. 0,.0667 0 } = 3, x () x () R = max{.9833 + 3,.98., 0.9989.0667 } = 0.00, atd. The results are written to the table: k x (k) x (k) x (k) 3 x (k) x (k ) R 0 0 0 0 3.0000.000.0667 3.0000.9833.9800 0.9989 0.00 3 3.0044.000 0.999 0.00 4.999.9998.000 0.0054 Stop after the four iteration, because x (4) x (3) R = 0.0054 0, and the result is x = 3.00 ± 0, x =.00 ± 0, x 3 =.00 ± 0. 6

3. INTERPOLATION AND APPROXIMATION OF FUNCTIONS 3. Interpolation and approximation of functions 7

3. INTERPOLATION AND APPROXIMATION OF FUNCTIONS Example 3.. The nodes x i and function values f i are given below. i 0 x i 3 f i Find the interpolation polynom for theese datas: a) in basic form, b) in Lagrang form, c) in Newton form. Solution: a)interpolating polynomial is finding in form p(x) = a 0 + a x + a x. Replace the interpolation conditions p(x i ) = f i, i = 0,, p( 3) = = a 0 3a + 9a =, p( ) = = a 0 a + a =, p() = = a 0 + a + a =. These equalities represent system of linear equations, which can be written in the form 3 9 a 0 a = a. The results are a 0 =, a 8 = 3, a = 5, the interpolation polynomial has the 8 form p(x) = 8 + 3 x + 5 8 x. b) Interpolation polynomial in Lagrange form is prescribed p(x) = f 0 ϕ 0 (x) + f ϕ (x) + f ϕ (x), where ϕ 0 (x), ϕ (x), ϕ (x) are Lagrange basis polynomials: ϕ 0 (x) = (x + )(x ) ( 3 + )( 3 ) = (x + )(x ), 8 8

3. INTERPOLATION AND APPROXIMATION OF FUNCTIONS ϕ (x) = ϕ (x) = (x + 3)(x ) ( + 3)( ) = (x + 3)(x ), 4 (x + 3)(x + ) = (x + 3)(x + ). ( + 3)( + ) 8 It is obtained p(x) = 8 (x + )(x ) + 4 (x + 3)(x ) + (x + 3)(x + ). 4 c) Interpolation polynomial in Newton form is prescribed p(x) = f 0 + f[x, x 0 ](x x 0 ) + f[x, x, x 0 ](x x 0 )(x x ), where f[x, x 0 ] a f[x, x, x 0 ] are the relative difference st and nd order. The results are written to the table: i x i f i st order nd order 0 3 5 8 3 Using the computed values in the first row of the table, find interpolation polynomial: p(x) = (x + 3) + 5 (x + 3)(x + ). 8 9

3. INTERPOLATION AND APPROXIMATION OF FUNCTIONS Example 3.. Aproximate the following data x i.5 3 5 7 y i 0 6.5 6 5 by the line ϕ(x) = c x + c by the least squares method. Solution: Find the system of linear equations for the unknown coefficients c, c. 5 5 c x i + c x i = i= i= 5 5 c x i + c = i= i= 5 y i x i i= 5 y i i= Compute individual totals: 5 x i = +.5 + 3 + 5 + 7 = 90.5 i= 5 x i = +.5 + 3 + 5 + 7 = 8.5 i= 5 y i x i = 0 +.5 + 6.5 3 + 6 5 + 5 7 = 57 i= 5 y i = 0 + + 6.5 + 6 + 5 = 8.5 i= Replace the system of linear equations: 90.5c + 8.5c = 57 8.5c + 5c = 8.5 Solve the system and the coefficients c =.3647, c = 3.0493 are obtained. The line ϕ(x) =.3647x 3.0493 is the result. 0

3. INTERPOLATION AND APPROXIMATION OF FUNCTIONS 6 4 zadane body nalezena funkce 0 8 6 4 0 3 4 5 6 7 Calculation in Matlab: x=[,.5,3,5,7] y=[0,,6.5,6,5] n=length(x) imput x i imput y i number of points A=[sum(x.^) sum(x);sum(x) n] matrix system b=[sum(y.*x);sum(y)] c=a\b f=c()*x+c() plot(x,y, o,x,f) vector of right-hand sides calculation of the coefficients line values in points x i graph

4. NUMERICAL INTEGRATION 4. Numerical integration

4. NUMERICAL INTEGRATION Example 4.. Compute the integral by composite trapezoid tule for n = 8: 3 e x dx Solution: Compute the step h in the interval a, b, h = b a n = 3 ( ) 8 = 0.5. Write to the table division points x 0,...,x 8 and the values of integrated functions in these points. i x i y i = e x i 0 -.783-0.5.84 0 3 0.5.84 4.783 5.5 9.4877 6 54.598 7.5 58.08 8 3 803.0839 Finally, compute the approximate value of the integral: I 0.5 ( ) ( ) n y0 + y n y0 + y 8 7 = h + y i = 0.5 + y i = i= i= (.783 + 803.0839 = 0.5 +.84 + +.84 +.783 + 9.4877+ ( ) 805.80 +54.598 + 58.08) = 0.5 + 588.385 = 30.64305 The result is: I 0.5 = 30.64305 3

4. NUMERICAL INTEGRATION Calculation in Matlab: a=-,b=3 interval bounds a, b n=8 number of segments on dividing n h=(b-a)/n x=a:h:b computation of step h computation of x i y=exp(x.^) computation of y i I=h*((y()+y(n+))/+sum(y(:n))) computation of I = h ( y 0 +y n + ) n i= y i In Matlab is indexed from, and we index points from 0, so the indexes in the formula in Matlab are shifted by. 4

4. NUMERICAL INTEGRATION Example 4.. Compute the integral by composite trapezoid rule with the precision ε = 0 3 : 3 e x dx Solution: Compute the integral composite trapezoid formula for n =, 4, 8, 6,..., until the error is less than the precision, i.e. I h I h < ε. n h I h I h I h ε = 0 3 = 0.00 8.388-4 4.76 4000.0 8 30.643 790.5745 6 4 688.983 63.748 3 8 508.640 80.88 64 6 46.798 46.8474 8 3 449.96.8307 56 64 446.9969.965 5 8 446.55 0.748 04 56 446.0696 0.855 048 5 446.03 0.0464 4096 04 446.06 0.06 89 048 446.0087 0.009 6384 4096 446.0080 0.0007 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 < 0.00 The result is: 3 e x = 446.008 ± 0.00 5

4. NUMERICAL INTEGRATION Example 4.3. Compute the integral by composite Simpson rule for n = 6: 0 arctg (x) + x + dx Solution: Compute the step h in the interval a, b, h = b a n = 0 = 0.5. 6 Write to the table division points x 0,...,x 6 and the value of integrated functions in these points. i x i y i = arctg (x i ) + x i + 0 0 0.50.405 0.500.300 3 0.3750.5037 4 0.5000.750 5 0.650.9370 6 0.7500.64 7 0.8750.397 8.669 i x i y i = arctg (x i ) + x i + 9.50.8376 0.500 3.059.3750 3.64.5000 3.4659 3.650 3.6636 4.7500 3.8560 5.8750 4.043 6 4.58 Finally, compute the approximate value of the integral: I 0.5 = h 3 (y 0 + y n + 4 S L + S S ) = 0.5 3 (y 0 + y 6 + 4 (y + y 3 + y 5 + +y 7 + y 9 + y + y 3 + y 5 ) + (y + y 4 + y 6 + y 8 + y 0 + y + y 4 ) = = 0.047 (5.58 + 4 0.7797 + 8.808) = 5.00 6

4. NUMERICAL INTEGRATION The result is: I 0.5 = 5.00 Calculation in Matlab: a=0,b= interval bounds a, b n=6 number of segments on dividing n h=(b-a)/n computation of steph x=a:h:b computation of x i y=atan(x).^+x+ computation of y i SL=sum(y(::n)) computation of S L SS=sum(y(3::n-)) computation of S S I=h/3*(y()+y(n+)+4*SL+*SS) computation of I In Matlab is indexed from, and we index points from 0, so the indexes in the formula in Matlab are shifted by. 7

4. NUMERICAL INTEGRATION Example 4.4. For the function f(x) = sin x + arctanx + x 4 compute approximately the first derivative on the interval 0, by the formula f (x) f(x + h) f(x h) h with a step h = 0.5 a h = 0.. Compare the results with values of exact calculated derivative. Solution: The exact derivative is found by the formula: f (x) = x cosx + +x 4 +x 4x 3 arctan x ( + x 4 ). Find the nodes x i = ih, i = 0,..., 8 and compute the functional values in them f i = f(x i ) to compute approximate derivative with step h = 0.5. There are the first three columns in the table: i x i f i f[x i+,x i ] f (x i ) e i 0 0.00 0.0000 -.0000-0.5 0.3065.3676.43 0.0538 0.50 0.6838.433.565 0.085 3 0.75.0.008.84 0.076 4.00.34 0.4764 0.545 0.0688 5.5.603 0.5880 0.4570 0.30 6.50 0.940.600.948 0.0347 7.75 0.803 3.637 3.6746 0.409 8.00 0.697 -.754 - The formula for calculation of approximate derivative is written in form f (x i ) f i+ f i h = f[x i+, x i ], 8

4. NUMERICAL INTEGRATION. Compute the approximate derivative values in all internal nodes, eg f (0.75).34 0.6838 0.5 =.008;. This is the fourth column of the table. The fifth column contains the exact values of derivatives that are in the last column compared with the approximate values: e i = f[x i+, x i ] f (x i ). For h = 0. the procedure is analogous, we obtain more accurate values, as shown in the following images: h=0.5 0... derivace aproximace derivace 3 4 0 0. 0.4 0.6 0.8..4.6.8 h=0. 0... derivace aproximace derivace 3 4 0 0. 0.4 0.6 0.8..4.6.8 9

4. NUMERICAL INTEGRATION Calculation in Matlab: a=0,b= interval bounds a, b h=0.5 step h x=[a:h:b] computation of nodes x i n=length(x) number of nodes n + df= *x.*cos(x.^) +((+x.^4)./(+x.^)-... 4*x.^3.*atan(x))./(+x.^4).^ computation of f (x i ) f=sin(x.^)+atan(x)./(+x.^4) computation of f i = f(x i ) adf=[nan;(f(3:n)-f(:n-))/(*h);nan] approximate derivative tab=[[0:n-],x,f,adf,df,abs(adf-df)] table plot(x,df, :,x,adf, - ) graph 30

5. INITIAL VALUE PROBLEMS FOR ODES 5. Initial value problems for ODEs 3

5. INITIAL VALUE PROBLEMS FOR ODES Example 5.. Solve the differential equation with the initial condition: y = 0. y, y( ) =, x + by the Euler method in the interval, 3 with the step h = 0.5. Solution: First, compute the number of dividing segments in the interval a, b : n = b a h = 3 ( ) 0.5 = 0. The initial condition is x 0 =,, y 0 =. Compute x, y by the Euler metod: x = x 0 + h = + 0.5 =.5 ( ) ( ) y = y 0 + h x 0 + 0. y 0 = + 0.5 0. ( ) = 0.85 ( ) + Compute x, y, x 3, y 3 : x y = x + h =.5 + 0.5 = ( ) ( ) = y + h x + 0. y = 0.85 + 0.5 0. ( 0.85) = (.5) + = 0.6537 x 3 = x + h = + 0.5 = 0.5 ( ) ( ) y 3 = y + h x + 0. y = 0.6537 + 0.5 0. ( 0.6537) = ( ) + = 0.37 Other approximate values of solution x 4, y 4,..., x 0, y 0 compute accordingly. 3

5. INITIAL VALUE PROBLEMS FOR ODES The approximate values of the solution are in the table and graph: i x i y i 0 - - -.5-0.85 - -0.6537 3-0.5-0.37 4 0 0.0476 5 0.5 0.545 6 0.979 7.5. 8.98 9.5.588 0 3.648 y.5 0.5 0 0.5 Priblizne reseni.5 0.5 0 0.5.5.5 3 x Calculation in Matlab: a=-,b=3 h=0.5 n=(b-a)/h x()=-,y()=- for i=:n x(i+)=x(i)+h y(i+)=y(i)+h*(/(x(i)^+)-0.*y(i)) end [x y ] plot(x,y) interval bounds a, b input step h number of dividing segments n input initial condition computation of x i, y i in the cycle extract into the table graph 33

5. INITIAL VALUE PROBLEMS FOR ODES Example 5.. Solve the differential equation with the initial condition: y = sin(x) y x 3 + y, y() = 0, by the 4th order Runge Kutta method in the interval, with the step h = 0.. Solution: First, compute the number of dividing segments in the interval a, b : n = b a h The initial condition is x 0 =,, y 0 = 0. = 0. = 5. Compute x, y by the 4th order Runge Kutta method: k = h f (x 0, y 0 ) = h (sin (x 0 ) y 0 x 3 0 + y 0) = 0. ( sin () 0 3 + 0 ) = k k 3 = 0. ( = h f x 0 + h, y 0 + k ) = ( = h sin x 0 + h ) ( y 0 + k ) ( x 0 + h ) 3 + y 0 + k = ( ( = 0. sin + 0. ) ( 0 + 0. ) ( + 0. ) 3 + 0 + 0. ) = 0.304 ( = h f x 0 + h, y 0 + k ) = ( = h sin x 0 + h ) ( y 0 + k ) ( x 0 + h ) 3 + y 0 + k = ( ( = 0. sin + 0. ) ( 0 + 0.304 ) ( + 0. ) 3 + 0 + 0.304 ) = = 0.337 k 4 = h f (x 0 + h, y 0 + k 3 ) = = h (sin ) (x 0 + h) (y 0 + k 3 ) (x 0 + h) 3 + y 0 + k 3 = = 0. (sin ( + 0.) (0 0.337) ( + 0.) 3 + 0 0.337 ) = 0.4707 x = x 0 + h = + 0. =. 34

5. INITIAL VALUE PROBLEMS FOR ODES y = y 0 + 6 (k + k + k 3 + k 4 ) = = 0 + ( 0. + ( 0.304) + ( 0.337) 0.4707) = 0.3 6 Compute x, y : k = h f (x, y ) = h (sin (x ) y x 3 + y ) = = 0. (sin (.) ( 0.3) 3 0.3 ) = = 0.4696 ( k = h f x + h, y + k ) = ( = h sin x + h ) ( y + k ) ( x + h ) 3 + y + k = ( ( = 0. sin. + 0. ) ( 0.3 + 0.4696 ) (. + 0. ) 3 0.3+ + 0.4696 ) = 0.6577 ( k 3 = h f x + h, y + k ) = ( = h sin x + h ) ( y + k ) ( x + h ) 3 + y + k = ( ( = 0. sin. + 0. ) ( 0.3 + 0.6577 ) ( + 0. ) 3 + 0+ + 0.6577 ) = 0.6946 k 4 = h f (x + h, y + k 3 ) = h (sin ) (x + h) (y + k 3 ) (x + h) 3 + y + k 3 = = 0. (sin ( + 0.) (0 0.337) ( + 0.) 3 + 0 0.337 ) = 0.95 x = x + h =. + 0. =.4 y = y + 6 (k + k + k 3 + k 4 ) = = 0.3 + ( 0.4696 + ( 0.6577) + ( 0.6946) 0.95) =.0087 6 Other approximate values of solution x 3, y 3,..., x 5, y 5 compute accordingly. 35

5. INITIAL VALUE PROBLEMS FOR ODES The approximate values of the solution are in the table and graph: 0 Priblizne reseni i x i y i 0 0. -0.30.4 -.0087 3.6 -.357 4.8-4.6586 5-8.535 y 3 4 5 6 7 8 9...3.4.5.6.7.8.9 x Calculation in Matlab: f=inline( sin(x)*y-x^3+y ) right sidef(x, y) a=,b= h=0. n=(b-a)/h x()=,y()=0 for i=:n k=h*f(x(i),y(i)) k=h*f(x(i)+h/,y(i)+k/) k3=h*f(x(i)+h/,y(i)+k/) k4=h*f(x(i)+h,y(i)+k3) x(i+)=x(i)+h y(i+)=y(i)+/6*(k+*k+*k3+k4) end [x y ] plot(x,y) interval bounds a, b input step h number of dividing segments n input initial condition computation of x i, y i in the cycle extract into the table graph 36