Question: Implement a python function which can numerically integrate a mathematical function. A simple approach based on the Riemann sum is sufficient. Use it to evaluate

Implement a python function which can numerically integrate a mathematical function. A simple approach based on the Riemann sum is sufficient.
Use it to evaluate the following definite integrals:
[\pi /20] arccos(cosx/(1+2cosx ))dx.
[11]coshx dx.
[e 1]1/xdx.
[10]2dx.
Feel free to use numpy except for functions like np.trapz
Report and test your answers, making sure each is accurate to 5 decimal digits. the exact values are 5/24 pi /2,(ee^1),1, and 2.
Additionally, the last integral has to be correct given a small number of discretization steps, say, 1,2,3(using the same function as the others without handling this extra case in a special way). It's essentially meant as a unit test for your algorithm and its implementation. You can use the function provided below.import numpy as np
def constant_function_of_one(x):
'''Evaluates a constant function returning 1.
It handles both a single number and np.array as input.
Args:
x: either a float or np.array of length n
Returns:
either a single 1.(as a float) or a length-n np.array of 1.
'''
return 0* x +1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!