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.
Python program for the provided problem statement
# import library
from scipy.integrate import quad
def integrand(x,A):
size = len(A)
s = 0
for i in range(size):
s += A[i]*x**(size-i-1)
return s
# this will calculate integral value
def integral(A, x1, x2):
ans, err = quad(integrand, x1, x2, args=A)
# display final result
print("\nIntegration of Polynomial :=> ",ans)
# list for coefficients
A = []
A = [int(item) for item in input("Enter coefficients of polynomial
[an, an-1....a0]: ").split()]
# display polinomial
s = ""
for i in range(len(A)):
s += (str)(A[i])+"*x^"+(str)(len(A)-i-1)
if i != len(A)-1:
s += " + "
# input limits
print("\nPolynomial :=> ",s)
x1 = int(input("\nEnter lower limit: "))
x2 = int(input("Enter upper limit: "))
integral(A, x1, x2)
Code Screenshot
program output 1 screenshot
program output 2 screenshot