Example
Nonlinear optimization deals with optimizing an objective function where the relationship between variables is nonlinear.
In this example, we’ll solve a constrained nonlinear optimization problem using the scipy.optimize library.
Problem: Minimize a Nonlinear Function
Minimize the following objective function:
$$
f(x, y) = (x - 1)^2 + (y - 2)^2
$$
subject to the nonlinear constraint:
$$
x^2 + y^2 \leq 2
$$
and the bounds:
$$
0 \leq x \leq 1, \quad 0 \leq y \leq 2.
$$
Objective
- Find the values of $x$ and $y$ that minimize $f(x, y)$.
- Ensure the solution satisfies the constraints.
- Visualize the results with a contour plot.
Python Implementation
1 | import numpy as np |
Explanation
Objective Function:
The function $(x - 1)^2 + (y - 2)^2$ measures the distance between $(x, y)$ and the point $(1, 2)$.
The goal is to minimize this distance.Constraint:
The nonlinear constraint $x^2 + y^2 \leq 2$ restricts the feasible region to within a circle of radius $\sqrt{2}$.Bounds:
$x$ and $y$ are restricted to specific ranges: $(0 \leq x \leq 1)$ and $(0 \leq y \leq 2)$.Solver:
We use the SLSQP method, which is well-suited for constrained nonlinear optimization problems.
Results and Visualization

Optimal Solution: x = 0.63, y = 1.26 Objective Function Value = 0.68
- The contour plot shows the levels of the objective function.
- The feasible region (light blue) is defined by the nonlinear constraint.
- The optimal solution is marked with a red dot, and its coordinates are displayed on the graph.
This example demonstrates how to solve a nonlinear optimization problem with constraints and visualize the results effectively!










