Wednesday, 18 December 2024

Day 45: Python Program to Compute a Polynomial Equation

 


def compute_polynomial(coefficients, x):

    result = 0

    n = len(coefficients)

    for i in range(n):

        result += coefficients[i] * (x ** (n - 1 - i))

    return result

coefficients = [1, -3, 2]  

x_value = 5  

result = compute_polynomial(coefficients, x_value)

print(f"The value of the polynomial at x = {x_value} is: {result}")

#source code --> clcoding.com 

Code Explanation:

Function Definition:
def compute_polynomial(coefficients, x):
This defines a function named compute_polynomial which takes two parameters:
coefficients: A list of coefficients for the polynomial.
x: The value at which the polynomial will be evaluated.

Initialize the result:
    result = 0
Here, the variable result is initialized to 0. This will store the final computed value of the polynomial as the function evaluates it.
Get the length of the coefficients list:

    n = len(coefficients)
n holds the number of coefficients in the list coefficients. This helps in determining the degree of the polynomial.

Loop through the coefficients:
    for i in range(n):
        result += coefficients[i] * (x ** (n - 1 - i))
This loop iterates over each coefficient in the coefficients list. For each coefficient:
i is the loop index (ranging from 0 to n-1).
The term coefficients[i] * (x ** (n - 1 - i)) computes the contribution of the current term in the polynomial.
The polynomial is evaluated using the following formula:

result=i=0n1coefficients[i]×xn1i

coefficients[i] is the coefficient of the ๐‘–
i-th term.
x ** (n - 1 - i) raises x to the power of 

n-1-i, which represents the degree of each term in the polynomial (starting from the highest degree).
The result is updated with each term's value as the loop proceeds.

Return the result:
    return result
After the loop has completed, the function returns the value stored in result, which is the final value of the polynomial evaluated at x.

Call the function and print the result:
result = compute_polynomial(coefficients, x_value)
print(f"The value of the polynomial at x = {x_value} is: {result}")
The function compute_polynomial is called with coefficients = [1, -3, 2] and x_value = 5. The result is stored in the result variable, and then the value is printed.

Output:

The value of the polynomial at x = 5 is: 12

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses