In: Computer Science
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1].
Hint: compute the error from the true answer
python code:
#function
def f(x):
return (x**2)
#to calculate actual area
def trueAnswer(a,b):
return (((b**3)/3)-((a**3)/3))
#get upper and lower limit
a=int(input("What is your starting point?"))
b=int(input("Where do you want to end?"))
#get intervals
n=float(input("How many intervals do you want?"))
#calculate the width
width=(b-a)/n
print('Width of one Interval= ', width)
i=0
Area=0
#left hand Riemann sum
while (i<(n)):
Area=f(a+width*i)*width + Area
i=i+1
#print the values
print("The approximate area is = ", Area)
print("The actual area is = ", trueAnswer(a,b))
output:
Note: the given code works for only this function."f(x) = x^2 ".
//for any clarification , please do comments, if you found this solution useful,please give me thumbs up