A Beginner’s Guide to Non-linear Equations and Constraints

Non Linear Equations With Constraints

Let’s start with a real-world example – Cars are mostly graded on the mileage they can handle. Good mileage cars are preferred due to rising fuel costs and hence it is a headache for car manufacturers as they have to take into account factors like the aerodynamics of the car, engine performance, etc. for fuel optimization. How do we solve this problem?

This issue is resolved with the help of optimization of non-linear equations. Non-linear equations are those equations in which the power of a variable is greater than 1.

Non-linear equations have variables with exponents and represent non-straight line relationships. Constraints restrict variables to certain values. This guide teaches you non-linear equations, constraints, and how to solve a non-linear polynomial equation in Python with SciPy by minimizing error under inequality constraint.

In this article, we will learn about non-linear equations and constraints and then we will also learn to implement them in Python. The area where constraints with linear equations are present is known as Linear Programming.

Recommended: Introduction To Linear Algebra

Understanding Non-linear Equations and Constraints

As mentioned above non-linear equations are equations in which the relationships of variables when plotted are not a straight line. They have some powers or exponents associated with them. Given below are some non-linear equations.

  1. Quadratic: x2+5x+6
  2. Exponential: ex
  3. Trigonometric : sin(x) + cos(x)

Constraints are restrictions placed on the variables. These variables are not allowed to assume certain values. Some examples of constraints are given below.

  1. Inequality constraints: x > 3 (x must be greater than 3)
  2. Equality constraints:  x + y = 5 (x and y must add up to 5)
  3. Bounds: 0 ≤ x ≤ 3 (x must be between 0 and 3)

Let us now move on to understand how to solve non-linear equations with constraints present.

Solving a Non-linear Equation with Python

In the equation given below, we have built a function to return a non-linear equation (x3-2x2+x-1). We have also built a constraint function of (x >1). Now we will try to minimize the given equation keeping in mind the constraint. Let us now look at the code below and the output. We will look at its plot as well.

import matplotlib.pyplot as plt
from scipy.optimize import minimize

# Define the non-linear equation
def equation(x):
    return x**3 - 2*x**2 + x - 1

# Define the constraint
def constraint(x):
    return x - 1  # x must be greater than or equal to 1

# Set bounds for the variable
bounds = ((1, None),)  # x between 1 and positive infinity

# Initial guess for the solution
guess = 1.5

# Solve the optimization problem
result = minimize(equation, guess, method="SLSQP", constraints=[{"type": "ineq", "fun": constraint}], bounds=bounds)

# Print the solution and function value
if result.success:
    solution_x = result.x[0]
    solution_y = equation(solution_x)
    print("Solution:", solution_x)
    print("Function value at solution:", solution_y)
    print("Optimization failed:", result.message)

# Generate x values for plotting the function
x_values = range(-1, 5)  # Adjust the range as needed

# Calculate corresponding y values using the equation
y_values = [equation(x) for x in x_values]

# Plot the function and the solution point
plt.plot(x_values, y_values, label="Function")
plt.axvline(x=solution_x, color="red", linestyle="--", label="Solution")
plt.axhline(y=solution_y, color="red", linestyle="--", label="Solution")

# Add labels and title
plt.title("Solution of Non-Linear Equation with Constraint")

# Add legend

# Show the plot

Non Linear Equation Output
Non-Linear Equation Output
Non Linear Equation Plot
Non-Linear Equation Plot

As we can observe from the output the minimum value of the function is at -1 at the value of 1. This concept of maxima and minima is solved with the concept of calculus, especially differentiation. Hence, we get the solution as (-1,1). Please keep in mind to cross-check your solution concerning the constraint.


You now understand non-linear equations, constraints, and solving optimization problems with Python. What real-world applications can you apply these concepts to? Hope you enjoyed reading it!!

Recommended: Python NumPy: Solving Coupled Differential Equations

Recommended: Numpy’s linalg.tensorsolve: Solving Tensor Equations in Python