reCAPTCHA WAF Session Token
Data Science and ML

How to Use NumPy to Solve Systems of Nonlinear Equations

Thank you for reading this post, don't forget to subscribe!

Image by Author

 

Nonlinear equation is a very interesting aspect of mathematics, with applications that stretch across science, engineering, and everyday life. While I was in school it took a while before I could have a strong grasp of its concept. Unlike linear equations, which form straight lines when graphed, nonlinear equations create curves, spirals, or more complex shapes. This makes them a bit trickier to solve but also incredibly valuable for modeling real-world problems.

Simply put, nonlinear equations involve variables raised to powers other than one or embedded in more complex functions. Here are a few common types:

  • Quadratic Equations: These involve squared terms, like ax2 + bx + c = 0. Their graphs form parabolas, which can open upwards or downwards.
  • Exponential Equations: Examples include ex = 3x, where variables appear as exponents, leading to rapid growth or decay.
  • Trigonometric Equations: Such as sin(x) = x/2, where variables are inside trigonometric functions, creating wave-like patterns.

These equations can produce a variety of graphs, from parabolas to oscillating waves, making them versatile tools for modeling various phenomena. Here are a few examples of where nonlinear equations come into play:

  • Physics: Modeling the motion of planets, the behavior of particles, or the dynamics of chaotic systems.
  • Engineering: Designing systems with feedback loops, such as control systems or circuit behavior.
  • Economics: Analyzing market trends, predicting economic growth, or understanding complex interactions between different economic factors.

NumPy can be used to simplify the process of solving systems of nonlinear equations. It provides tools to handle complex calculations, find approximate solutions, and visualize results, making it easier to tackle these challenging problems.

In the following sections, we’ll explore how to leverage NumPy to solve these intriguing equations, turning complex mathematical challenges into manageable tasks.

Before diving into the technicalities of solving systems of nonlinear equations with NumPy, it’s important to understand how to formulate and set up these problems effectively. To formulate a system, follow these steps:

  1. Identify the Variables: Determine the variables that will be part of your system. These are the unknowns you’re trying to solve for.
  2. Define the Equations: Write down each equation in the system, ensuring it includes the identified variables. Nonlinear equations include terms like x2, ex, or xy.
  3. Arrange the Equations: Organize the equations clearly, translating them into a format NumPy can handle more easily.

 

Step-by-Step Solution Process

 

In this section, we will break down the solving of nonlinear equations into manageable steps to make the problem more approachable. Here’s how you can systematically tackle these problems using NumPy and SciPy.

 

Defining the Functions

The first step is to translate your system of nonlinear equations into a format that can be handled by Python. This involves defining the equations as functions.

In Python, you represent each equation as a function that returns the value of the equation given a set of variables. For nonlinear systems, these functions often include terms like squares, exponents, or products of variables.

For example, you have a system of two nonlinear equations:

  • f1​ (x, y) = x2 + y2 − 4
  • f2 (x, y) = x2 − y − 1

Here’s how you’d define these functions in Python:

<code>def equations(vars):
    x, y = vars
    eq1 = x**2 + y**2 - 4
    eq2 = x**2 - y - 1
    return [eq1, eq2]</code>

 

In this function, vars is a list of variables you want to solve for. Each equation is defined as a function of these variables and returns a list of results.

 

Setting Initial Guesses

Before finding the solution, you must provide initial guesses for the variables. These guesses are essential because iterative methods, like those used by fsolve, rely on them to start the search for a solution.

Good initial guesses help us converge to a solution more effectively. Poor guesses might lead to convergence issues or incorrect solutions. Think of these guesses as starting points for finding the roots of your equations.

Tips for Choosing Effective Initial Guesses:

  • Domain Knowledge: Use prior knowledge about the problem to make educated guesses.
  • Graphical Analysis: Plot the equations to get a visual sense of where the solutions might lie.
  • Experimentation: Sometimes, trying a few different guesses and observing the results can help.

For our example equations, you might start with:

<code>initial_guesses = [1, 1]  # Initial guesses for x and y</code>

 

Solving the System

With your functions defined and initial guesses set, you can now use scipy.optimize.fsolve to find the roots of your nonlinear equations. fsolve is designed to handle systems of nonlinear equations by finding where the functions are zero.

Here’s how you can use fsolve to solve the system:

<code>from scipy.optimize import fsolve
# Solve the system
solution = fsolve(equations, initial_guesses)
print("Solution to the system:", solution)</code>

 

In this code, fsolve takes two arguments: the function representing the system of equations and the initial guesses. It returns the values of the variables that satisfy the equations.

After solving, you might want to interpret the results:

<code># Print the results
x, y = solution
print(f"Solved values are x = {x:.2f} and y = {y:.2f}")

# Verify the solution by substituting it back into the equations
print("Verification:")
print(f"f1(x, y) = {x**2 + y**2 - 4:.2f}")
print(f"f2(x, y) = {x**2 - y - 1:.2f}")</code>

 

Result showing that the values are close to zero.Result showing that the values are close to zero.
 

This code prints the solution and verifies it by substituting the values back into the original equations to ensure they are close to zero.

 

Visualizing Solution

 

Once you’ve solved a system of nonlinear equations, visualizing the results can help you understand and interpret them better. Whether you’re dealing with two variables or three, plotting the solutions provides a clear view of how these solutions fit within the context of your problem.

Let’s use a couple of examples to illustrate how to visualize the solutions:
 

 

2D Visualization

Suppose you have solved equations with two variables x and y. Here’s how you can plot these solutions in 2D:

<code>import numpy as np
import matplotlib.pyplot as plt

# Define the system of equations
def equations(vars):
    x, y = vars
    eq1 = x**2 + y**2 - 4
    eq2 = x**2 - y - 1
    return [eq1, eq2]

# Solve the system
from scipy.optimize import fsolve
initial_guesses = [1, 1]
solution = fsolve(equations, initial_guesses)
x_sol, y_sol = solution

# Create a grid of x and y values
x = np.linspace(-3, 3, 400)
y = np.linspace(-3, 3, 400)
X, Y = np.meshgrid(x, y)

# Define the equations for plotting
Z1 = X**2 + Y**2 - 4
Z2 = X**2 - Y - 1

# Plot the contours
plt.figure(figsize=(8, 6))
plt.contour(X, Y, Z1, levels=[0], colors="blue", label="x^2 + y^2 - 4")
plt.contour(X, Y, Z2, levels=[0], colors="red", label="x^2 - y - 1")
plt.plot(x_sol, y_sol, 'go', label="Solution")
plt.xlabel('x')
plt.ylabel('y')
plt.title('2D Visualization of Nonlinear Equations')
plt.legend()
plt.grid(True)
plt.show()</code>

 

Here is the output:

 
2D Visualization2D Visualization
 

The blue and red contours in this plot represent the curves where each equation equals zero. The green dot shows the solution where these curves intersect.

 

3D Visualization

For systems involving three variables, a 3D plot can be more informative. Suppose you have a system with variables x, y, and z. Here’s how you can visualize this:

<code>from mpl_toolkits.mplot3d import Axes3D

# Define the system of equations
def equations(vars):
    x, y, z = vars
    eq1 = x**2 + y**2 + z**2 - 4
    eq2 = x**2 - y - 1
    eq3 = z - x * y
    return [eq1, eq2, eq3]

# Solve the system
initial_guesses = [1, 1, 1]
solution = fsolve(equations, initial_guesses)
x_sol, y_sol, z_sol = solution

# Create a grid of x, y, and z values
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.sqrt(4 - X**2 - Y**2)

# Plotting the 3D surface
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, alpha=0.5, rstride=100, cstride=100, color="blue")
ax.plot_surface(X, Y, -Z, alpha=0.5, rstride=100, cstride=100, color="red")

# Plot the solution
ax.scatter(x_sol, y_sol, z_sol, color="green", s=100, label="Solution")

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3D Visualization of Nonlinear Equations')
ax.legend()
plt.show()</code>

 

Output:

 
3D Visualization3D Visualization
 

In this 3D plot, the blue and red surfaces represent the solutions to the equations, and the green dot shows the solution in 3D space.

 

Conclusion

 

In this article, we explored the process of solving systems of nonlinear equations using NumPy. We made complex mathematical concepts approachable and practical by breaking down the steps, from defining the problem to visualizing the solutions.

We started by formulating and defining nonlinear equations in Python. We emphasized the importance of initial guesses and provided tips for choosing effective starting points. Then, we utilized scipy.optimize.solve to find the roots of our equations. Finally, we demonstrated how to visualize the solutions using matplotlib, making interpreting and verifying the results easier.
 
 

Shittu Olumide is a software engineer and technical writer passionate about leveraging cutting-edge technologies to craft compelling narratives, with a keen eye for detail and a knack for simplifying complex concepts. You can also find Shittu on Twitter.



Back to top button
Consent Preferences
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock