Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • Goal
  • Step 1: Understand the Problem
  • Step 2: Mathematical Steps for Finding Critical Points
  • Summary Table
  • Keywords
  1. Calculus 2

Optimization Problems (Maxima/Minima) in Multivariable Functions

Nerd Cafe

Goal

To find maximum or minimum values of a multivariable function, often subject to certain conditions (e.g., within a region or constraint).

Step 1: Understand the Problem

For a function f(x,y), you want to find points where the function reaches:

  • Maximum value (peak)

  • Minimum value (valley)

  • Saddle point (neither max nor min, like a horse saddle)

These occur where the gradient vector is zero:

∂f∂x=0    and    ∂f∂y=0\frac{\partial f}{\partial x}=0 \;\;and\;\; \frac{\partial f}{\partial y}=0∂x∂f​=0and∂y∂f​=0

Step 2: Mathematical Steps for Finding Critical Points

Let’s say we have:

f(x,y)=x2+y2−4x−6y+13f(x,y)=x^{2}+y^{2}-4x-6y+13f(x,y)=x2+y2−4x−6y+13

Step A: Compute First-Order Partial Derivatives

fx=∂f∂x=2x−4f_{x}=\frac{\partial f}{\partial x}=2x-4fx​=∂x∂f​=2x−4

and

fy=∂f∂y=2y−6f_{y}=\frac{\partial f}{\partial y}=2y-6fy​=∂y∂f​=2y−6

Step B: Set the Partial Derivatives to Zero

2x−4=0⇒x=22y−6=0⇒y=3\begin{matrix} 2x-4=0\Rightarrow x=2 \\ \\ 2y-6=0\Rightarrow y=3 \end{matrix}2x−4=0⇒x=22y−6=0⇒y=3​

So the critical point is (2,3).

Step C: Use Second Derivative Test

Let’s define:

fxx=∂2f∂x2=2fyy=∂2f∂y2=2fxy=∂2f∂x∂y=0\begin{matrix} f_{xx}=\frac{\partial^{2}f }{\partial x^{2}}=2 \\ \\ f_{yy}=\frac{\partial^{2}f }{\partial y^{2}}=2 \\ \\ f_{xy}=\frac{\partial^{2}f }{\partial x \partial y}=0 \end{matrix}fxx​=∂x2∂2f​=2fyy​=∂y2∂2f​=2fxy​=∂x∂y∂2f​=0​

Now compute the discriminant:

D=fxxfyy−(fxy)2=(2×2)−0=4D=f_{xx}f_{yy}-\left( fxy \right)^{2}=(2\times 2)-0=4D=fxx​fyy​−(fxy)2=(2×2)−0=4

Interpretation:

  • If D>0 and fxx>0 → local minimum

  • If D>0 and fxx<0 → local maximum

  • If D<0 → saddle point

In our case:

Since D=4>0 and fxx=2>0, it's a local minimum at (2,3).

Python Code Using SymPy

import sympy as sp

# Define variables
x, y = sp.symbols('x y')

# Define the function
f = x**2 + y**2 - 4*x - 6*y + 13

# First-order partial derivatives
fx = sp.diff(f, x)
fy = sp.diff(f, y)

# Solve for critical points
critical_points = sp.solve([fx, fy], (x, y))
print("Critical Points:", critical_points)

# Second-order partial derivatives
fxx = sp.diff(fx, x)
fyy = sp.diff(fy, y)
fxy = sp.diff(fx, y)

# Discriminant
D = fxx * fyy - fxy**2

# Evaluate at the first critical point
point = critical_points[0]  # it's a dict like {x: 2, y: 3}
D_val = D.subs(point)
fxx_val = fxx.subs(point)

print("D =", D_val)
print("f_xx =", fxx_val)

# Nature of critical point
if D_val > 0:
    if fxx_val > 0:
        print("Local minimum at", point)
    else:
        print("Local maximum at", point)
elif D_val < 0:
    print("Saddle point at", point)
else:
    print("Test inconclusive")

Outline

Critical Points: {x: 2, y: 3}

Visualize the Function in 3D

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X = Y = np.linspace(-1, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = X**2 + Y**2 - 4*X - 6*Y + 13

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
ax.set_title("f(x, y) = x² + y² - 4x - 6y + 13")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("f(x, y)")
ax.scatter(2, 3, f.subs({x:2, y:3}), color='r', s=50)  # Critical point
plt.show()

Outline

Summary Table

Step
Action

1

Compute𝑓𝑥,𝑓𝑦​

2

Solve𝑓𝑥=0,𝑓𝑦=0

3

Compute second-order partials: 𝑓 𝑥𝑥 , 𝑓 𝑦𝑦 , 𝑓 𝑥𝑦

4

Find discriminant 𝐷 = 𝑓 𝑥𝑥.𝑓 𝑦𝑦 − ( 𝑓 𝑥𝑦 )2

5

Determine max/min/saddle based on D and 𝑓 𝑥𝑥 ​

Keywords

optimization, multivariable calculus, critical points, maxima, minima, saddle point, gradient, partial derivatives, second derivative test, discriminant, local minimum, local maximum, unconstrained optimization, sympy, Python math, calculus with Python, function analysis, 3D plotting, mathematical optimization, surface plot, nerd cafe

PreviousPartial DerivativesNextGradient Vectors

Last updated 1 month ago