(Solved): python Question 6 Given a function \( f(x) \), and a real interval \( [a, b] \), the numerical integ ...
python
Question 6 Given a function \( f(x) \), and a real interval \( [a, b] \), the numerical integration of \( f(x) \) over interval \( [a \), b] can be calculated as: \[ \int_{a}^{b} f(x) d x \approx \sum_{i=1}^{n} \frac{b-a}{n} f\left(a+\frac{(b-a)}{n} \times(i-1 / 2)\right) \] In equation (1), \( n \) represents the number of sub-intervals into which the interval [a, b] will be divided; and it controls the accuracy of numerical integration.
Write a program to allow the user to specify a trigonometric function \( f(f \) can only be sin, cos or tan), and input the interval end points \( a, b \) and number of sub-intervals \( n \). Your program should then calculate the numerical integration of \( f \) over \( [a, b] \) using equation (1), and output the result. Your program should be robust enough to handle possible improper inputs (e.g. the user inputs a floating point number as \( n \) ). Python has built-in trigonometric functions. To call them, use the following statement in your program to import them from the math package: \( \gg \gg\rangle \) from math import sin \( \gg \gg \) from math inport cos \( \gg \gg) \) from math import tan You can then invoke the trigonometric functions like the following examples: \[ \begin{array}{l} \gg \gg \gg \sin (1) \\ 0.8414709848078965 \\ \gg \gg\rangle \cos (3.1415) \\ -0.9999999957076562 \\ \gg \gg\rangle \tan (0) \\ 0.0 \end{array} \]