In: Computer Science
Let P(x) be a polynomial of degree n
and A = [an , an-1,.... ]
Write a function integral(A, X1, X2) that takes 3 inputs A, X0 and X1
A as stated above
X1 and X2 be any real number, where X1 is the lower limit of the integral and X2 is the upper limit of the integral. Please write this code in Python.
DONT use any inbuilt function, instead use looping in Python to solve the question. You should begin the code this way def integral(A, X1, X2):
NO INBUILT functions, I repeat
`Hey,
Note: If you have any queries related to the answer please do comment. I would be very happy to resolve all your queries.
import numpy as np
def func(A,x):
y=0;
for i in range(len(A)):
y=y*x+A[i];
return y;
def trapezoidal (a, b, n,A):
# Grid spacing
h = (b - a) / n
# Computing sum of first and last terms
# in above formula
s = (func(A,a) + func(A,b))
# Adding middle terms in above formula
i = 1
while i < n:
s += 2 * func(A,a + i * h)
i += 1
# h/2 indicates (b-a)/2n.
# Multiplying h/2 with s.
return ((h / 2) * s)
def integral(A, X1, X2):
return trapezoidal(X1,X2,100,A);
print(integral(np.array([1,2,3,4]),1,5));
Kindly revert for any queries
Thanks.