Respuesta :

Answer:

I am writing the function using Python:

def poly(roots, x):

   polx = 1

   for r in roots:

       polx = polx * (x - r)

   return polx

Explanation:

Given n+1 roots r0, r1, . . . , rn of a polynomial p(x) of degree n+1, p(x) is calculated by the following formula

p(x) = π(x - ri) = (x - r0)(x - r1) · · · (x - rn-1)(x - rn)

The function poly() takes two parameters, roots and x

roots is a list and x is a variable  

The for loop keeps computing polynomial p(x) for each root value in the list by the following formula:

polx = polx * (x - r)

The loop breaks when p(x) is computed for all the root values and the value of p(x) is stored in polx which is returned as ouput at the end. For example:

def poly(roots, x):

   polx = 1

   for r in roots:

       polx = polx * (x - r)

   return polx

x = 1

roots = [2, 3, 5]

print ( poly(roots, x) )

Here x = 1 and root values are 2 3 and 5 Then the function poly() is called which will work as follows:

First iteration:

polx = 1

Loop starts and checks first element in the list roots which is 2

Then computes p(x) as:        

polx = polx * (x - r)

       = 1 * (1 - 2)

       = -1

Second iteration:

polx = -1

Loop starts and checks first element in the list roots which is 3

Then computes p(x) as:        

polx = polx * (x - r)

       = -1 * ( 1 - 3)

       = 2

Third iteration:

polx = 2

Loop starts and checks first element in the list roots which is 5

Then computes p(x) as:        

polx = polx * (x - r)

       = 2 * ( 1 - 5)

       = -8

Now that the loop has moved through all the values of the list so the loop breaks and return polx statement executes which prints the value of polx

Output:

-8

The program with output is given in attached screen shot.

Ver imagen mahamnasir
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE