Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Numerical Analysis with Python I don\'t really get the concept of this problem.

ID: 3585517 • Letter: N

Question

Numerical Analysis with Python

I don't really get the concept of this problem. Please explain and code in Python. Thanks!

Write a computer code to implement the Composite Trapezoidal Rule quadrature TO T1 to approximate the definite integral .b 1[f] = | f(r)dr, using the equally spaced points 10 = a, xi-ro + h,T2 = 10 + 2h, . . . ,xN 1, where (a) Test your code with f(x) 1/1+x) in [0, 2] by computing the error fl Thfl h = (b-a)/N. for h-2/20, 2/40, 2/80, and verify that Th has a convergent trend at the expected, quadratic rate

Explanation / Answer

Python code

# First define a function to calculate the definite integral using trapezoidal rule

# call the function whenever required

def trapezoidal(f, a, b, h):

Th_of_f = 0.0 # initilization of the variable containing solution

Th_of_f += h*(f(a)+f(b))/2 # adding the first and last terms ie f(xo) and f(xN)

for i in range(1,int((b-a)/h -1)): # loop for intermediate grid points

Th_of_f += h*f(a+ i*h) # adding intermediate function values

return Th_of_f # returning the solution

# End of trapezoidal function

# define the function to be integrated

def f(x):

return 1/((1+x)*(1+x)) # return the fvalue of function at x

# End of function f

# define lower and upper limits

a = 0 #lower limit

b = 2 #upper limit

h = float(2)/20 # value of the step size

I_of_f = float(2)/3

# Now call the function trapezoidal and

Th_of_f = trapezoidal(f, a, b, h)

print 'Th[f] = ',Th_of_f,'|I[f]-Th[f]= ',abs(I_of_f-Th_of_f) # print the result

# call the functipon for the next value of h

h = float(2)/40 # value of the step size

Th_of_f = trapezoidal(f, a, b, h)

print 'Th[f] = ',Th_of_f,'|I[f]-Th[f]= ',abs(I_of_f-Th_of_f)# print the result

# call the functipon for the next value of h

h = float(2)/80 # value of the step size

Th_of_f = trapezoidal(f, a, b, h)

print 'Th[f] = ',Th_of_f,'|I[f]-Th[f]= ',abs(I_of_f-Th_of_f)# print the result

OUTPUT

Th[f] = 0.650822146819 |I[f]-Th[f]= 0.0158445198481

Th[f] = 0.658544440913 |I[f]-Th[f]= 0.0081222257534

Th[f] = 0.662553414183 |I[f]-Th[f]= 0.00411325248383