NumPy - Evaluating Polynomials



Evaluating Polynomials in NumPy

Evaluating polynomials in NumPy means calculating the value of the polynomial at a specific point. You can do this using the numpy.polyval() function in NumPy.

The polynomial is defined by its coefficients, starting with the highest degree term and ending with the constant term. The function evaluates the polynomial at the given value of x, and can handle multiple values of x at once, returning the corresponding results.

The numpy.polyval() Function

The numpy.polyval() function evaluates a polynomial for a given value (or array of values) of x. It takes two arguments: the coefficients of the polynomial and the value(s) at which the polynomial should be evaluated.

The polynomial is expressed in terms of its coefficients, starting with the highest power term and ending with the constant term.

Example: Evaluating Polynomial at a Single Point

Let us consider the polynomial f(x) = 2x - 3x + 4. We can evaluate this polynomial at x = 2

import numpy as np

# Define the coefficients of the polynomial 2x - 3x + 4
coefficients = np.array([2, -3, 4])

# Evaluate the polynomial at x = 2 using numpy.polyval
x_value = 2
result = np.polyval(coefficients, x_value)

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

The result of evaluating the polynomial at x = 2 is −

Value of the polynomial at x = 2: 6

Evaluating Polynomials at Multiple Points

In addition to evaluating the polynomial at a single point, the numpy.polyval() function can also evaluate the polynomial at multiple points at once. The function accepts an array of x values and returns an array of corresponding results.

Example

Let us evaluate the same polynomial f(x) = 2x - 3x + 4 at multiple values of x

import numpy as np

# Define the coefficients of the polynomial 2x - 3x + 4
coefficients = np.array([2, -3, 4])

# Define the x values for evaluation
x_values = np.array([-1, 0, 1, 2])

# Evaluate the polynomial at multiple points using numpy.polyval
results = np.polyval(coefficients, x_values)

print("Values of the polynomial at x = [-1, 0, 1, 2]:", results)

The result of evaluating the polynomial at the given points is as shown below −

Values of the polynomial at x = [-1, 0, 1, 2]: [9 4 3 6]

Evaluating Polynomials with Complex Roots

If a polynomial has complex roots or coefficients, the numpy.polyval() function can still evaluate the polynomial correctly. The function will return complex numbers if the result involves complex arithmetic.

Example

Consider the polynomial f(x) = (2 + 3i)x + (1 - 2i)x + (4 + i). We can evaluate this polynomial at x = 1 + i as shown below −

import numpy as np

# Define the coefficients of the polynomial with complex numbers
coefficients_complex = np.array([2 + 3j, 1 - 2j, 4 + 1j])

# Define the x value for evaluation
x_value_complex = 1 + 1j

# Evaluate the polynomial at x = 1 + 1j using numpy.polyval
result_complex = np.polyval(coefficients_complex, x_value_complex)

print(f"Value of the complex polynomial at x = {x_value_complex}: {result_complex}")

The result of evaluating the polynomial with complex coefficients is −

Value of the complex polynomial at x = (1+1j): (1+4j)

Polynomial Evaluation for Curve Plotting

Evaluating polynomials at a range of x values is commonly used in curve plotting, such as when plotting the graph of a polynomial function. This can be done by passing an array of x values to the numpy.polyval() function and plotting the resulting values.

Example: Plotting a Polynomial Curve

Let us plot the polynomial f(x) = 2x - 3x + 4 over the range of x = -5 to x = 5

import numpy as np
import matplotlib.pyplot as plt

# Define the coefficients of the polynomial 2x - 3x + 4
coefficients = np.array([2, -3, 4])

# Define the range of x values
x_values_plot = np.linspace(-5, 5, 100)

# Evaluate the polynomial at the x values using numpy.polyval
y_values_plot = np.polyval(coefficients, x_values_plot)

# Plot the polynomial curve
plt.plot(x_values_plot, y_values_plot, label='f(x) = 2x - 3x + 4')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Polynomial Curve Plot')
plt.legend()
plt.grid(True)
plt.show()

The output displayed is as shown below −

Polynomial Evaluation
Advertisements