4. Optimization for Two Variables and Its Applications
4. Optimization for Two Variables and Its Applications
• Critical Points
• Second Derivative Test (D-Test) and Optimization
• Applications of Optimization
----------------------------------------------------------------------
Critical Points
• A critical point is a point on the graph where the slope changes direction,
or where the first derivative of a function is zero or undefined.
• For functions of two or more variables, the concept is essentially the same,
except for the fact that we are now working with partial derivatives.
then:
1) If 𝑫 > 𝟎 and 𝒇𝒙𝒙 (𝒙𝟎 , 𝒚𝟎 ) > 𝟎, then 𝒇(𝒙, 𝒚) has a local minima or
relative minima at (𝒙𝟎 , 𝒚𝟎 ).
2) If 𝑫 > 𝟎 and 𝒇𝒙𝒙 (𝒙𝟎 , 𝒚𝟎 ) < 𝟎, then 𝒇(𝒙, 𝒚) has a local maxima or
relative maxima at (𝒙𝟎 , 𝒚𝟎 ).
3) If 𝑫 < 𝟎, then 𝒇(𝒙, 𝒚) has a saddle point (neither maximum nor
minimum) at (𝒙𝟎 , 𝒚𝟎 ).
4) If 𝑫 = 𝟎, then test is inconclusive. (Test Fails)
Example
OPTIMIZATION
To optimize a function means to find the largest or smallest value of the function.
If the function represents profit, we may want to find the conditions that maximize
profit. On the other hand, if the function represents cost, we may we may want to
find the conditions that minimize cost.
For the procedure of optimization we will follow the following steps in calculation:
Let
𝟐
𝑫 = 𝒇𝒙𝒙 (𝒙𝒐 , 𝒚𝒐 )𝒇𝒚𝒚 (𝒙𝒐 , 𝒚𝒐 ) − (𝒇𝒙𝒚 (𝒙𝒐 , 𝒚𝒐 ))
• If 𝑫 > 0 & 𝒇𝒙𝒙 (𝒙𝒐 , 𝒚𝒐 ) > 0, then f has a local minima at (𝒙𝒐 , 𝒚𝒐 ).
• If 𝑫 > 0 & 𝒇𝒙𝒙 (𝒙𝒐 , 𝒚𝒐 ) < 0, then f has a local maxima at
(𝒙𝒐 , 𝒚𝒐 ).
• If 𝑫 < 0 then 𝒇 has neither maxima nor minima at (𝒙𝒐 , 𝒚𝒐 ). It is
saddle point.
• If 𝑫 = 𝟎 then test is inconclusive. (Test fails).
Example 1:
Optimize the given function by determining if it has a local maxima,
local minima or neither.
𝒇(𝒙, 𝒚) = 𝒙𝟐 − 𝟐𝒙 + 𝒚𝟐 − 𝟒𝒚 + 𝟓
Solution:
The given function is
𝑓(𝑥, 𝑦) = 𝑥 2 − 2𝑥 + 𝑦 2 − 4𝑦 + 5
𝝏𝒇
Find
𝝏𝒙
𝝏𝒇 𝝏 𝟐 𝝏 𝝏 𝟐 𝝏 𝝏
𝒇𝒙 (𝒙, 𝒚) = = (𝒙 ) − (𝟐𝒙) + (𝒚 ) − (𝟒𝒚) + (𝟓)
𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙
𝝏𝒇
𝒇𝒙 (𝒙, 𝒚) = = 𝟐𝒙 − 𝟐 + 𝟎 + 𝟎 + 𝟎 = 𝟐𝒙 − 𝟐
𝝏𝒙
𝝏𝒇
𝒇𝒙 (𝒙, 𝒚) = = 𝟐𝒙 − 𝟐
𝝏𝒙
𝝏𝒇
Find
𝝏𝒚
𝝏𝒇 𝝏 𝟐 𝝏 𝝏 𝟐 𝝏 𝝏
𝒇𝒚 (𝒙, 𝒚) = = (𝒙 ) − (𝟐𝒙) + (𝒚 ) − (𝟒𝒚) + (𝟓)
𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = = 𝟎 + 𝟎 + 𝟐𝒚 − 𝟒 + 𝟎 = 𝟐𝒚 − 𝟒
𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = = 𝟐𝒚 − 𝟒
𝝏𝒚
𝒇𝒙 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎 𝒇𝒚 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎
⇒ 𝟐𝒙 − 𝟐 = 𝟎 ⇒ 𝟐𝒚 − 𝟒 = 𝟎
⇒ 𝟐𝒙 = 𝟐 ⇒ 𝟐𝒚 = 𝟒
𝟐 𝟒
⇒𝒙= =𝟏 ⇒𝒚= =𝟐
𝟐 𝟐
⇒𝒙=𝟏 ⇒𝒚=𝟐
Hence, the critical point is
(𝒙, 𝒚) = (𝟏, 𝟐)
Find Second Derivatives
𝝏𝟐 𝒇
Find 𝒇𝒙𝒙 (𝒙, 𝒚) =
𝝏𝒙𝟐
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = 𝟐 (𝒙) − (𝟐) = 𝟐(𝟏) − 𝟎 = 𝟐
𝝏𝒙 𝝏𝒙 𝝏𝒙
𝝏𝟐 𝒇
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = 𝟐
𝝏𝒙
𝝏𝟐 𝒇
Find 𝒇𝒚𝒚 (𝒙, 𝒚) =
𝝏𝒚𝟐
𝝏𝟐 𝒇 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = = (𝟐𝒚 − 𝟒)
𝝏𝒚𝟐 𝝏𝒚
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟐 = 𝟐 (𝒚) − (𝟒) = 𝟐(𝟏) − 𝟎 = 𝟐
𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒚𝒚 (𝒙, 𝒚) = =𝟐
𝝏𝒚𝟐
𝝏𝟐 𝒇
Find 𝒇𝒙𝒚 (𝒙, 𝒚) =
𝝏𝒚 𝝏𝒙
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒚 (𝒙, 𝒚) = =𝟐 ( 𝒙) − (𝟐) = 𝟐(𝟎) − 𝟎 = 𝟎
𝝏𝒚 𝝏𝒙 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒙𝒚 (𝒙, 𝒚) = =𝟎
𝝏𝒚 𝝏𝒙
𝑫 = [𝟐][𝟐] − [ 𝟎 ]𝟐
⇒𝑫=𝟒−𝟎
⇒𝑫=𝟒>𝟎
---------------------------------------------------------------------------------------
Example 2:
Determine whether the function has a local maxima, local minima or
neither.
𝒇(𝒙, 𝒚) = 𝒚𝟑 − 𝟑𝒙𝒚 + 𝟔𝒙
Solution:
The given function is
𝒇(𝒙, 𝒚) = 𝒚𝟑 − 𝟑𝒙𝒚 + 𝟔𝒙
𝝏𝒇
Step 1: Find
𝝏𝒙
𝝏𝒇
Step 2: Find
𝝏𝒚
𝝏𝒇 𝝏 𝟑 𝝏 𝝏
𝒇𝒚 (𝒙, 𝒚) = = (𝒚 ) − (𝟑𝒙𝒚) + (𝟔𝒙)
𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = = 𝟑𝒚𝟐 − 𝟑𝒙(𝟏) + 𝟎 = 𝟑𝒚𝟐 − 𝟑𝒙
𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = 𝝏𝒚 = 𝟑𝒚𝟐 − 𝟑𝒙 -------------(2)
𝒇𝒙 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎 𝒇𝒚 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎
⇒ −𝟑𝒚 + 𝟔 = 𝟎 ⇒ 𝟑𝒚𝟐 − 𝟑𝒙 = 𝟎
⇒ −𝟑𝒚 = −𝟔 ⇒ −𝟑𝒙 = −𝟑𝒚𝟐
−𝟔 −𝟑𝒚𝟐
⇒𝒚= =𝟐 ⇒𝒙= = 𝒚𝟐
−𝟑 −𝟑
⇒ 𝒚 = 𝟐 -------------(3) ⇒ 𝒙 = 𝒚𝟐 -------------(4)
From equation (3), substitute
value of 𝒚 in equation (4), we get
⇒ 𝒙 = (𝟐)𝟐 = 𝟒
⇒ 𝒙 = 𝟒 -------- (5)
Hence,
𝒙 = 𝟒, 𝒚 = 𝟐
(𝒙, 𝒚) = (𝟒, 𝟐)
Step 4: Find Second Derivatives
𝝏𝟐 𝒇
Find 𝒇𝒙𝒙 (𝒙, 𝒚) =
𝝏𝒙𝟐
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = −𝟑 (𝒚) + (𝟔) = −𝟑(𝟎) + 𝟎 = 𝟎
𝝏𝒙 𝝏𝒙 𝝏𝒙
𝝏𝟐 𝒇
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = 𝟎
𝝏𝒙
𝝏𝟐 𝒇
Find 𝒇𝒚𝒚 (𝒙, 𝒚) =
𝝏𝒚𝟐
𝝏𝟐 𝒇 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = = (𝟑𝒚𝟐 − 𝟑𝒙)
𝝏𝒚𝟐 𝝏𝒚
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟐 = 𝟑 (𝒚𝟐 ) − 𝟑 (𝒙) = 𝟑(𝟐𝒚) − 𝟑(𝟎) = 𝟔𝒚
𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟐 = 𝟔𝒚
𝝏𝒚
𝝏𝟐 𝒇
Find 𝒇𝒙𝒚 (𝒙, 𝒚) =
𝝏𝒚 𝝏𝒙
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒚 (𝒙, 𝒚) = = −𝟑 ( 𝒚) + (𝟔) = −𝟑(𝟏) + 𝟎 = −𝟑
𝝏𝒚 𝝏𝒙 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒙𝒚 (𝒙, 𝒚) = = −𝟑
𝝏𝒚 𝝏𝒙
𝑫 = [𝟎][𝟏𝟐] − [−𝟑 ]𝟐
⇒𝑫=𝟎−𝟗
⇒ 𝑫 = −𝟗 < 𝟎
Step 7: Check the function has local maxima/minimum or
neither
Since 𝑫 = −𝟗 < 𝟎, so by using 2nd derivative test, the given function
𝒇(𝒙, 𝒚) has neither local maxima nor local minima at critical point (𝟒, 𝟐). The
function 𝒇(𝒙, 𝒚) has saddle point at critical point (𝟐, −𝟏).
---------------------------------------------------------------------------------------
Example:
Determine whether the function has a local maxima, local minima or
neither.
𝒇(𝒙, 𝒚) = 𝒙𝟑 + 𝒚𝟑 − 𝟑𝒙𝟐 − 𝟑𝒚 + 𝟏𝟎
Solution:
The given function is
𝒇(𝒙, 𝒚) = 𝒙𝟑 + 𝒚𝟑 − 𝟑𝒙𝟐 − 𝟑𝒚 + 𝟏𝟎
𝝏𝒇
Step 1: Find
𝝏𝒙
𝝏𝒇 𝝏 𝟑 𝝏 𝟑 𝝏 𝝏 𝝏
𝒇𝒙 (𝒙, 𝒚) = = (𝒙 ) + (𝒚 ) − (𝟑𝒙𝟐 ) − (𝟑𝒚) + (𝟏𝟎)
𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙 𝝏𝒙
𝝏𝒇
𝒇𝒙 (𝒙, 𝒚) = = 𝟑𝒙𝟐 + 𝟎 − 𝟑(𝟐𝒙) − 𝟎 + 𝟎 = 𝟑𝒙𝟐 − 𝟔𝒙
𝝏𝒙
𝝏𝒇
𝒇𝒙 (𝒙, 𝒚) = = 𝟑𝒙𝟐 − 𝟔𝒙
𝝏𝒙
𝝏𝒇
Step 2: Find
𝝏𝒚
Partially differentiate 𝒇(𝒙, 𝒚) w.r.t variable 𝒚, we have
𝝏𝒇 𝝏
𝒇𝒚 (𝒙, 𝒚) = = (𝒙𝟑 + 𝒚𝟑 − 𝟑𝒙𝟐 − 𝟑𝒚 + 𝟏𝟎)
𝝏𝒚 𝝏𝒚
𝝏𝒇 𝝏 𝝏 𝟑 𝝏 𝝏 𝝏
𝒇𝒚 (𝒙, 𝒚) = = (𝒙𝟑 ) + (𝒚 ) − (𝟑𝒙𝟐 ) − (𝟑𝒚) + (𝟏𝟎)
𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = = 𝟎 + 𝟑𝒚𝟐 − 𝟎 − 𝟑 + 𝟎 = 𝟑𝒚𝟐 − 𝟑
𝝏𝒚
𝝏𝒇
𝒇𝒚 (𝒙, 𝒚) = = 𝟑𝒚𝟐 − 𝟑
𝝏𝒚
𝒇𝒙 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎 𝒇𝒚 (𝒙𝟎 , 𝒚𝟎 ) = 𝟎
⇒ 𝟑𝒙𝟐 − 𝟔𝒙 = 𝟎 ⇒ 𝟑𝒚𝟐 − 𝟑 = 𝟎
⇒ 𝟑𝒙(𝒙 − 𝟐) = 𝟎 ⇒ 𝟑(𝒚𝟐 − 𝟏) = 𝟎
⇒ 𝒙(𝒙 − 𝟐) = 𝟎 ⇒ 𝒚𝟐 − 𝟏 = 𝟎
⇒ 𝒙 = 𝟎, 𝒙−𝟐=𝟎 ⇒ (𝒚 + 𝟏)(𝒚 − 𝟏) = 𝟎
⇒ 𝒙 = 𝟎, 𝒙=𝟐 ⇒ 𝒚 = −𝟏, 𝒚=𝟏
Since we obtained the values of 𝒙 and 𝒚 as below
• Now, fix second value of 𝒙 and make ordered pairs will all values of 𝒚, that is
(𝟐, −𝟏), (𝟐, 𝟏)
Hence, we obtain the four (4) critical points. The critical points are as follows:
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = 𝟑 (𝒙𝟐 ) − 𝟔 (𝒙) = 𝟑(𝟐𝒙) − 𝟔(𝟏) = 𝟔𝒙 − 𝟔
𝝏𝒙 𝝏𝒙 𝝏𝒙
𝝏𝟐 𝒇
𝒇𝒙𝒙 (𝒙, 𝒚) = 𝟐 = 𝟔𝒙 − 𝟔
𝝏𝒙
𝝏𝟐 𝒇
Find 𝒇𝒚𝒚 (𝒙, 𝒚) =
𝝏𝒚𝟐
𝝏𝟐 𝒇 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = = (𝟑𝒚𝟐 − 𝟑)
𝝏𝒚𝟐 𝝏𝒚
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟐 = 𝟑 (𝒚𝟐 ) − (𝟑) = 𝟑(𝟐𝒚) − 𝟎 = 𝟔𝒚
𝝏𝒚 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟐 = 𝟔𝒚
𝝏𝒚
𝝏𝟐 𝒇
Find 𝒇𝒙𝒚 (𝒙, 𝒚) =
𝝏𝒚 𝝏𝒙
𝝏𝟐 𝒇 𝝏 𝝏
𝒇𝒙𝒚 (𝒙, 𝒚) = =𝟑 ( 𝒙𝟐 ) − 𝟔 (𝒙) = 𝟑(𝟎) − 𝟔(𝟎) = 𝟎
𝝏𝒚 𝝏𝒙 𝝏𝒚 𝝏𝒚
𝝏𝟐 𝒇
𝒇𝒙𝒚 (𝒙, 𝒚) = =𝟎
𝝏𝒚 𝝏𝒙
𝒇𝒚𝒚 (𝒙, 𝒚) = 𝟔𝒚
𝒇𝒙𝒚 (𝒙, 𝒚) = 𝟎
Since
𝟐
𝑫 = 𝒇𝒙𝒙 (𝟎, −𝟏) 𝒇𝒚𝒚 (𝟎, −𝟏) − [ 𝒇𝒙𝒚 (𝟎, −𝟏) ]
𝑫 = [−𝟔][−𝟔] − [ 𝟎 ]𝟐
⇒ 𝑫 = 𝟑𝟔 − 𝟎
⇒ 𝑫 = 𝟑𝟔 > 𝟎
For critical point (𝒙, 𝒚) = (𝟎, 𝟏)
• 𝒇𝒙𝒙 (𝟎, 𝟏) = −𝟔 < 𝟎
• 𝒇𝒚𝒚 (𝟎, 𝟏) = 𝟔
• 𝒇𝒙𝒚 (𝟎, 𝟏) = 𝟎
Since
𝟐
𝑫 = 𝒇𝒙𝒙 (𝟎, 𝟏) 𝒇𝒚𝒚 (𝟎, 𝟏) − [ 𝒇𝒙𝒚 (𝟎, 𝟏) ]
𝑫 = [−𝟔][𝟔] − [ 𝟎 ]𝟐
⇒ 𝑫 = −𝟑𝟔 − 𝟎
⇒ 𝑫 = −𝟑𝟔 < 𝟎
For critical point (𝒙, 𝒚) = (𝟐, −𝟏)
• 𝒇𝒙𝒙 (𝟐, −𝟏) = 𝟔 > 𝟎
• 𝒇𝒚𝒚 (𝟐, −𝟏) = −𝟔
• 𝒇𝒙𝒚 (𝟐, −𝟏) = 𝟎
Since
𝟐
𝑫 = 𝒇𝒙𝒙 (𝟐, −𝟏) 𝒇𝒚𝒚 (𝟐, −𝟏) − [ 𝒇𝒙𝒚 (𝟐, −𝟏) ]
𝑫 = [𝟔][−𝟔] − [ 𝟎 ]𝟐
⇒ 𝑫 = −𝟑𝟔 − 𝟎
⇒ 𝑫 = −𝟑𝟔 < 𝟎
For critical point (𝒙, 𝒚) = (𝟐, 𝟏)
• 𝒇𝒙𝒙 (𝟐, 𝟏) = 𝟔 > 𝟎
• 𝒇𝒚𝒚 (𝟐, 𝟏) = 𝟔
• 𝒇𝒙𝒚 (𝟐, 𝟏) = 𝟎
Since
𝟐
𝑫 = 𝒇𝒙𝒙 (𝟐, 𝟏) 𝒇𝒚𝒚 (𝟐, 𝟏) − [ 𝒇𝒙𝒚 (𝟐, 𝟏) ]
𝑫 = [𝟔][𝟔] − [ 𝟎 ]𝟐
⇒ 𝑫 = 𝟑𝟔 − 𝟎
⇒ 𝑫 = 𝟑𝟔 > 𝟎
Since 𝑫 = −𝟑𝟔 < 𝟎, so by using 2nd derivative test, the given function
𝒇(𝒙, 𝒚) has neither local maxima nor local minima at critical point (𝟎, 𝟏). The
function 𝒇(𝒙, 𝒚) has saddle point at critical point (𝟎, 𝟏).
For critical point (𝒙, 𝒚) = (𝟐, −𝟏)
Since 𝑫 = −𝟑𝟔 < 𝟎, so by using 2nd derivative test, the given function
𝒇(𝒙, 𝒚) has neither local maxima nor local minima at critical point (𝟐, −𝟏). The
function 𝒇(𝒙, 𝒚) has saddle point at critical point (𝟐, −𝟏).
Conclusion:
𝑾 = 𝒇(𝑻, 𝒗)
The wind-chill index 𝑾 compiled by the National Weather Service of the US and the
Meteorological Service of Canada is given in the form of the function.
𝒇(𝒙, 𝒚) = 𝒙𝟑 𝒚 + 𝟏𝟐𝒙𝟐 − 𝟖𝒚
Minimize the total quantity of fuel required to achieve a specified terminal velocity.
Question 2:
𝟑
𝒚𝟑
𝒇(𝒙, 𝒚) = 𝟗𝒙 + − 𝟒𝒙𝒚
𝟑
Find the optimal solutions and minimizes the user wait in the video streaming
application.
Practice Questions:
Determine whether the following functions have a local maxima, local
minima or neither.
1) 𝒇(𝒙, 𝒚) = 𝒙𝟐 + 𝟒𝒙 + 𝒚𝟐
2) 𝒇(𝒙, 𝒚) = 𝒙𝟐 + 𝒙𝒚 + 𝟑𝒚
3) 𝒇(𝒙, 𝒚) = 𝒙𝟐 + 𝒚𝟐 + 𝟔𝒙 − 𝟏𝟎𝒚 + 𝟖
4) 𝒇(𝒙, 𝒚) = 𝒙𝟐 − 𝟐𝒙𝒚 + 𝟑𝒚𝟐 − 𝟖𝒚
5) 𝒇(𝒙, 𝒚) = 𝒙𝟑 − 𝟑𝒙 + 𝒚𝟑 − 𝟑𝒚
6) 𝒇(𝒙, 𝒚) = 𝒙𝟑 + 𝒚𝟐 − 𝟑𝒙𝟐 + 𝟏𝟎𝒚 + 𝟔
7) 𝒇(𝒙, 𝒚) = 𝒙𝟑 + 𝒚𝟑 − 𝟔𝒚𝟐 − 𝟑𝒙 + 𝟗
8) 𝒇(𝒙, 𝒚) = 𝒙𝟑 + 𝒚𝟑 − 𝟑𝒙𝟐 − 𝟑𝒚 + 𝟏𝟎
9) 𝒇(𝒙, 𝒚) = 𝟒𝟎𝟎 − 𝟑𝒙𝟐 − 𝟒𝒙 + 𝟐𝒙𝒚 − 𝟓𝒚𝟐 + 𝟒𝟖𝒚
Optimization Using Gradient-Based
Algorithms in PYTHON
Example: Optimize the cost function 𝒇(𝒙, 𝒚) = 𝒙𝟐 + 𝒚𝟐 + 𝟓 using
gradient descent.
Python Code:
import numpy as np
import matplotlib.pyplot as plt
# Define a multivariate function (e.g., a simple quadratic
function)
def cost_function(x, y):
return x**2 + y**2 + 5
# Partial derivatives of the cost function
def gradient(x, y):
df_dx = 2 * x
df_dy = 2 * y
return np.array([df_dx, df_dy])
# Gradient Descent Algorithm
def gradient_descent(starting_point, learning_rate, iterations):
point = np.array(starting_point, dtype=float)
path = [point.copy()]
for _ in range(iterations):
grad = gradient(*point)
point -= learning_rate * grad
path.append(point.copy())
return np.array(path)
# Parameters
starting_point = [10, 10]
learning_rate = 0.1
iterations = 50
# Execute gradient descent
path = gradient_descent(starting_point, learning_rate, iterations)
# Plotting
x_vals = np.linspace(-12, 12, 100)
y_vals = np.linspace(-12, 12, 100)
X, Y = np.meshgrid(x_vals, y_vals)
Z = cost_function(X, Y)
plt.contour(X, Y, Z, levels=50)
plt.plot(path[:, 0], path[:, 1], marker='o', color='red')
plt.title('Gradient Descent Optimization')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
Output:
Note: Now find the critical points of the given exercises and
verify using the gradient descent code.
Applications of Optimization
Example 1:
A manufacturing company produces two products which are sold in
two separate markets. The company’s economists analyze the two
markets and determine that the two quantities 𝒒𝟏 and 𝒒𝟐 demanded by the
consumers and prices 𝑷𝟏 and 𝑷𝟐 (in $) of each item are related by the
equation
Price of 1st product = 𝒑𝟏 (𝒒𝟏 , 𝒒𝟐 ) = 𝟔𝟎𝟎 − 𝟎. 𝟑 𝒒𝟏
Price of 2nd product =𝒑𝟐 (𝒒𝟏 , 𝒒𝟐 ) = 𝟓𝟎𝟎 − 𝟎. 𝟐 𝒒𝟐
If the price for either item increases, the demand for it decreases. The
company’s total production cost is given by
𝑪𝒐𝒔𝒕 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 = 𝑪(𝒒𝟏 , 𝒒𝟐 ) = 𝟏𝟔 + 𝟏. 𝟐 𝒒𝟏 + 𝟏. 𝟓 𝒒𝟐 + 𝟎. 𝟐 𝒒𝟏 𝒒𝟐
a) If the company wants to maximize its total profit, how much of
each product should it produce?
b) What is the maximum profit?
Solution:
Given information:
𝑷(𝒒𝟏 , 𝒒𝟐 ) = −𝟎. 𝟑𝒒𝟐𝟏 − 𝟎. 𝟐𝒒𝟐𝟐 + 𝟓𝟗𝟖. 𝟖𝒒𝟏 + 𝟒𝟗𝟖. 𝟓𝒒𝟐 − 𝟎. 𝟐𝒒𝟏 𝒒𝟐 − 𝟏𝟔 ----(8)
Note: This is your simple function. You can apply 2nd derivative test on this
profit function given in equation (8).
𝝏𝑷
Find
𝝏𝒒𝟏
𝝏𝑷
𝑷𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟑(𝟐𝒒𝟏 ) − 𝟎 + 𝟓𝟗𝟖. 𝟖(𝟏) + 𝟎 − 𝟎. 𝟐𝒒𝟐 (𝟏) − 𝟎
𝝏𝒒𝟏
𝝏𝑷
𝑷𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟔 𝒒𝟏 + 𝟓𝟗𝟖. 𝟖 − 𝟎. 𝟐𝒒𝟐
𝝏𝒒𝟏
𝝏𝑷
𝑷𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟔 𝒒𝟏 − 𝟎. 𝟐 𝒒𝟐 + 𝟓𝟗𝟖. 𝟖
𝝏𝒒𝟏
𝝏𝑷
𝑷𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟔 𝒒𝟏 − 𝟎. 𝟐 𝒒𝟐 + 𝟓𝟗𝟖. 𝟖 ----------(9)
𝝏𝒒𝟏
𝝏𝑷
Find
𝝏𝒒𝟐
𝝏𝑷
𝑷𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = 𝟎 − 𝟎. 𝟐(𝟐𝒒𝟐 ) + 𝟎 + 𝟒𝟗𝟖. 𝟓(𝟏) − 𝟎. 𝟐𝒒𝟏 (𝟏) − 𝟎
𝝏𝒒𝟐
𝝏𝑷
𝑷𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟒 𝒒𝟐 + 𝟒𝟗𝟖. 𝟓 − 𝟎. 𝟐𝒒𝟏
𝝏𝒒𝟐
𝝏𝑷
𝑷𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟐 𝒒𝟏 − 𝟎. 𝟒 𝒒𝟐 + 𝟒𝟗𝟖. 𝟓
𝝏𝒒𝟐
𝝏𝑷
𝑷𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟐 𝒒𝟏 − 𝟎. 𝟒 𝒒𝟐 + 𝟒𝟗𝟖. 𝟓 ----------(10)
𝝏𝒒𝟐
We obtained two equations in two variables. These are two simultaneous equations.
𝟎. 𝟔 𝒒𝟏 + 𝟎. 𝟐 𝒒𝟐 = 𝟓𝟗𝟖. 𝟖 -------(11)
𝟎. 𝟐 𝒒𝟏 + 𝟎. 𝟒 𝒒𝟐 = 𝟒𝟗𝟖. 𝟓----------(12)
Multiply equation (12) by “𝟑” & subtract from equation (11), we get
𝑒𝑞(11) ⇒ 𝟎. 𝟔 𝒒𝟏 + 𝟎. 𝟐 𝒒𝟐 = 𝟓𝟗𝟖. 𝟖
[𝑒𝑞(12) × 3] ⇒ 𝟎. 𝟔 𝒒𝟏 + 𝟏. 𝟐 𝒒𝟐 = 𝟏𝟒𝟗𝟓. 𝟓
⇒ 𝟎. 𝟔 𝒒𝟏 + 𝟏. 𝟐 𝒒𝟐 − 𝟎. 𝟔 𝒒𝟏 − 𝟎. 𝟐 𝒒𝟐 = 𝟏𝟒𝟗𝟓. 𝟓 − 𝟓𝟗𝟖. 𝟖
⇒ 𝟏. 𝟐 𝒒𝟐 − 𝟎. 𝟐 𝒒𝟐 = 𝟖𝟗𝟕
⇒ 𝒒𝟐 = 𝟖𝟗𝟕 ---------(13)
𝟎. 𝟔 𝒒𝟏 + 𝟎. 𝟐 𝒒𝟐 = 𝟓𝟗𝟖. 𝟖
⇒ 𝟎. 𝟔 𝒒𝟏 + 𝟎. 𝟐 (𝟖𝟗𝟕) = 𝟓𝟗𝟖. 𝟖
⇒ 𝟎. 𝟔 𝒒𝟏 + 𝟏𝟕𝟗. 𝟒 = 𝟓𝟗𝟖. 𝟖
⇒ 𝟎. 𝟔 𝒒𝟏 = 𝟓𝟗𝟖. 𝟖 − 𝟏𝟕𝟗. 𝟒
⇒ 𝟎. 𝟔 𝒒𝟏 = 𝟒𝟏𝟗. 𝟒
𝟒𝟏𝟗.𝟒
⇒ 𝒒𝟏 =
𝟎.𝟔
⇒ 𝒒𝟏 = 𝟔𝟗𝟗 ---------(14)
Hence,
• 𝒒𝟏 = 𝟔𝟗𝟗
• 𝒒𝟐 = 𝟖𝟗𝟕
𝝏𝟐 𝑷 𝝏
𝑷𝒒𝟏 𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = 𝟐
= (−𝟎. 𝟔 𝒒𝟏 − 𝟎. 𝟐 𝒒𝟐 + 𝟓𝟗𝟖. 𝟖)
𝝏𝒒𝟏 𝝏𝒒𝟏
𝝏𝟐 𝑷
𝑷𝒒𝟏 𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟔(𝟏) − 𝟎 + 𝟎 = −𝟎. 𝟔
𝝏𝒒𝟏 𝟐
𝝏𝟐 𝑷
𝑷𝒒𝟏 𝒒𝟏 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟔
𝝏𝒒𝟏 𝟐
𝝏𝟐 𝑷
Find 𝑷𝒒𝟐 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) =
𝝏𝒒𝟐 𝟐
𝝏𝟐 𝑷 𝝏
𝑷𝒒𝟐 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = 𝟐
= (−𝟎. 𝟐 𝒒𝟏 − 𝟎. 𝟒 𝒒𝟐 + 𝟒𝟗𝟖. 𝟓)
𝝏𝒒𝟐 𝝏𝒒𝟐
𝝏𝟐 𝑷
𝑷𝒒𝟐 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = 𝟎 − 𝟎. 𝟒(𝟏) = −𝟎. 𝟒
𝝏𝒒𝟐 𝟐
𝝏𝟐 𝑷
𝑷𝒒𝟐 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟒
𝝏𝒒𝟐 𝟐
𝝏𝟐 𝑷
Find 𝑷𝒒𝟏 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) =
𝝏𝒒𝟐 𝝏𝒒𝟏
𝝏𝟐 𝑷 𝝏
𝑷𝒒𝟏 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = (−𝟎. 𝟔 𝒒𝟏 − 𝟎. 𝟐 𝒒𝟐 + 𝟓𝟗𝟖. 𝟖)
𝝏𝒒𝟐 𝝏𝒒𝟏 𝝏𝒒𝟐
𝝏𝟐 𝑷
𝑷𝒒𝟏 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = 𝟎 − 𝟎. 𝟐(𝟏) = −𝟎. 𝟐
𝝏𝒒𝟐 𝝏𝒒𝟏
𝝏𝟐 𝑷
𝑷𝒒𝟏𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟐
𝝏𝒒𝟐 𝝏𝒒𝟏
𝝏𝟐 𝑷
𝑷𝒒𝟐 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟒
𝝏𝒒𝟐 𝟐
𝝏𝟐 𝑷
𝑷𝒒𝟏 𝒒𝟐 (𝒒𝟏 , 𝒒𝟐 ) = = −𝟎. 𝟐
𝝏𝒒𝟐 𝝏𝒒𝟏
⇒ 𝑷(𝒒𝟏 , 𝒒𝟐 ) = 𝟒𝟑𝟐𝟕𝟗𝟕
----------------------------------------------------------------------------------------------------
Example:
A company operates two plants which manufacture the same item and whose total
functions are
where 𝑞1 and 𝑞2 are the quantities produced by each plant. The company is a
monopoly. The total quantity demanded, 𝑞 = 𝑞1 + 𝑞2 , is related to the price, 𝑃, by
𝑃 = 60 − 0.04𝑞.
How much should each plant produce in order to maximize the company’s profit?
Solution:
= 60𝑞 − 0.04𝑞 2
= 60(𝑞1 + 𝑞2 ) − 0.04(𝑞1 + 𝑞2 )2
−0.14𝑞1 − 0.08𝑞2 + 60 = 0
−0.16𝑞2 − 0.08𝑞1 + 60 = 0
Multiply equation (1) by 0.08 & equation (2) by 0.14 then subtract it from equation
(1).
−0.016𝑞2 = −3.6
𝑞2 = 225
Put in (1)
0.14𝑞1 + 0.08(225) = 60
0.14𝑞1 = 60 − 18
42
𝑞1 = = 300
0.14
𝑞1 = 300
As, 𝐷 > 0 and 𝑓𝑞1𝑞1 = −0.14 < 0 so, 𝑓 has a local maxima at (300, 225)
Practice Problems
Question: An automobile manufacturer sells cars in America and Europe, charging
different prices in the two markets. The price function for cars sold in America is
𝒑 = 𝟐𝟎 − 𝟎. 𝟐𝒙 thousand dollars (for 𝟎 ≤ 𝒙 ≤ 𝟏𝟎𝟎), and the price function
for cars sold in Europe is 𝒒 = 𝟏𝟔 − 𝟎. 𝟏𝒚 thousand dollars (for 𝟎 ≤ 𝒚 ≤ 𝟏𝟔𝟎),
where 𝒙 is the quantity of cars sold per day in America and 𝒚 is the quantity of cars
sold per day in Europe, respectively. The company’s cost function in per thousand
dollars is 𝑪 = 𝟐𝟎 + 𝟒(𝒙 + 𝒚).
a) Find the company’s profit function. [Hint: Profit is revenue from America
plus revenue from Europe minus costs, where each revenue is price times
quantity.]
b) Find how many cars should be sold in each market to maximize profit.
Also find the price for each market.