Sunday, 30 December 2018

Numerical Integration using trapezoidal and Simpson's 1/3 rule



The following is a python 3 code which uses the two of the most common numerical methods used for numerical integration, viz. trapezoidal and Simpson's 1/3 rule. I have assumed your familiarity with the theory of the methods. The code uses python's symbolic math library, sympy, and the math library. Sympy has been mainly used to calculate the function values at different x values. Moreover, to handle the error which arises while the user inputs $\pi$ as pi, which python treats as a string, I have used symbolic math. Nevertheless, this discrepancy can also overcome if the limits are passed as function arguments. So it's better as it suits yourself. Other than sympy, the math library is also used to deal with the trigonometric functions. The code can be, of course, modified as per one's use.  My attempt is just to help a beginner programmer in python looking to execute these numerical methods. The various methods or functions of sympy library can be easily found on the python documentation.
You can drop any suggestions and doubts below, I'll be happy to look into it.

Numerical solution of singularly perturbed ODE

The sigulary perturbed ODEs are solved using the method of finite differences, using the formula for first and second derivative as: $...