PARTIAL DERIVATIVES AND LOCAL EXTREMA OF FUNCTIONS OF
TWO VARIABLES
Aim: To write MATLAB codes to compute the Par al Deriva ves of a given func on f (x, y) at a
speci ed point (x0, y0) and visualize the result. To determine the local Maximum and Minimum
(Extreme) values of the given func on f (x, y) using MATLAB.
PARTIAL DERIVATIVES
Definition: Let f (x, y) be a function of two variables. Its partial derivatives are the functions fx and fy
defined by
fx(x,y)= lim [ f (x + h, y) − f (x, y)] /h
h→0
fy(x,y)= lim [ f (x, y + h) − f (x, y)] /h
h→0
Notation: Let z = f (x, y).
• The partial derivative fx (x, y) can also be written as
∂f ∂f
(x, y) or
∂x ∂x
• Similarly, fy(x, y) can also be written as
∂f ∂f
(x, y) or
∂y ∂y
• The partial derivative fx (x, y) evaluated at the point (x0, y0 ) can be expressed in several ways:
∂f ∂f
fx (x0, y0 ), |(x ,y ) or (x0, y0 )
∂x 0 0 ∂x
There are analogous expressions for fy(x0, y0 ).
Geometrical Meaning:
• The graph of z = f (x, y) is a surface. Consider the partial derivative of f with respect to x at a point
(x0, y0 ).
• Holding y constant and varying x , we trace out a curve that is the intersection of the surface with the
plane y = y0.
• The partial derivative fx (x0, y0 ) measures the change in z per unit increase in x along this curve. That is,
fx (x0, y0 ) is just the slope of the curve at (x0, y0 ).
• The geometrical interpretation of fy(x0, y0 ) is analogous.
fi
ti
ti
ti
ti
MATLAB Syntax Used:
diff(f, x) Differentiate the function f with respect
to x symbolically.
R = subs(S, old, new) Replaces old value with new value in the
symbolic expression S.
line(X, Y, Z) Creates a line object in the current axes
with default values x = [0 1] and y = [0 1].
You can specify the color, width, line
style, and marker type, as well as other
characteristics.
Y = ones(n) Returns an n-by-n matrix of 1’s. An error
message appears if n is not a scalar.
set(H, ‘PropertyName’, PropertyValue,…) Sets the named properties to the
specified values on the object(s) identified
by H. H can be a vector of handles, in
which case set sets the properties' values
for all the objects
MATLAB Code: Partial derivatives of functions of two variables
clc; clear; close all; % Clear environment
format compact; % Compact output
% Define symbolic variables
syms x y;
% User Input
z = input('Enter a function f(x,y): ');
x1 = input('Enter x value at which derivative has to be evaluated: ');
y1 = input('Enter y value at which derivative has to be evaluated: ');
% Slope Calculation
z1 = double(subs(z, [x, y], [x1, y1]));
fsurf(z, [x1-2, x1+2, y1-2, y1+2]);
xlabel('x');
ylabel('y');
zlabel('z');
title('Surface Plot of f(x,y)');
f1 = diff(z, x); % Partial derivative w.r.t. x
slopex = subs(f1, [x, y], [x1, y1]);
% Visualization of the Plane in which the partial derivative is sought
[x2, z2] = meshgrid(x1-2:0.25:x1+2, z1-2:0.5:z1+2);
y2 = y1 * ones(size(x2));
hold on;
h1 = surf(x2, y2, z2);
set(h1, 'FaceColor', [0.7, 0.7, 0.7], 'EdgeColor', 'none');
% The Tangent Line
t = linspace(-1, 1);
x3 = x1 + t;
y3 = y1 * ones(size(t));
z3 = z1 + slopex * t;
line(x3, y3, z3, 'Color', 'blue', 'LineWidth', 2);
hold off;
Practice Problems:
1) Find the par al deriva ves of f (x, y) = x 3 + y 3 + 6x y − 1 with respect to x at the point (1, 1).
2) Find the par al deriva ve of f (x, y) = 4 − x 2 − 2y 2 with respect to y at the point (1, 1).
LOCAL MAXIMA AND MINIMA
Mathematical Form:
Let z = f (x, y) be the given function. Critical points are points in the xy-plane where the tangent plane is
horizontal. The tangent plane is horizontal, if its normal vector points in the z direction. Hence, critical
points are solutions of the equations: fx (x, y) = 0 and fy(x, y) = 0.
Procedure for finding the maximum or minimum values of f(x,y):
(1) For the given function f (x, y) find fx & fy and equate them to zero and solve them to find the roots
(x1, y1), (x 2, y2 ), … These points may be maximum or minimum points.
∂2 f ∂2 f ∂2 f
(2) Find the values r = ,s = , t = 2 at these points.
∂x 2 ∂x∂y ∂y
(3) (a) If r t − s 2 > 0 and r < 0 at a certain point, then the function is maximum at that point.
(b) If r t − s 2 > 0 and r > 0 at a certain point , then the function is minimum at that point.
(c) If r t − s 2 < 0 for a certain point, then the function is neither maximum nor minimum at that
point. This point is known as saddle point.
(d) If r t − s 2 = 0 at a certain point, then nothing can be said whether the function is maximum or
minimum at that point. In this case further investigation are required.
ti
ti
ti
ti
Example: Obtain the maximum and minimum values of f (x, y) = 2(x 2 − y 2 ) − x 4 + y 4.
Solution: f(x,y)= 2(x2-y2)-x4+y4
∂f
= 4 x − 4 x3
∂x
∂f
= 4 y3 − 4 y
∂y
∂2 f
r= 2
= 4 − 12 x 2
∂x
∂2 f
s= =0
∂x∂y
∂2 f
t= 2
= 12 y 2 − 4 y
∂y
∂f ∂f
= 0 ⇒ 4 x − 4 x3 = 0, = 0 ⇒ 4 y 3 − 4 y = 0
∂x ∂y
S.No Critical Points r D=rt-s2 Remarks
1 (0, 0) 4 -16 < 0 Saddle Point
2 (0, 1) 4 32 Minimum
3 (0, -1) 4 32 Minimum
4 (1, 0) -8 32 Maximum
5 (1, 1) -8 -64 < 0 Saddle Point
6 (1, -1) -8 -64 < 0 Saddle Point
7 (-1, 0) -8 32 Maximum
8 (-1, 1) -8 -64 < 0 Saddle Point
9 (-1, -1) -8 -64 < 0 Saddle Point
The Minimum value of f(x,y) is -1 at (0, 1) & (0, -1) and the Maximum value for f(x,y) is +1 at (1, 0) &
(-1, 0)
MATLAB Syntax used:
diff diff(expr) differentiates a symbolic expression expr with
respect to its free variable as determined by symvar.
solve Symbolic solution of algebraic equations, The input to solve
can be either symbolic expressions or strings
size Dimensions of data and model objects and to access a specific
size output.
imag Imaginary part of complex number, Y=imag(Z) returns the
imaginary part of the elements of array Z.
figure Create figure graphics object, Figure objects are the
individual windows on the screen in which the MATLAB
software displays graphical output
double Convert to double precision, double(x) returns the double-
precision value for X. If X is already a double-precision array,
double has no effect.
sprint Format data into string. It applies the format to all elements of
array A and any additional array arguments in column order,
and returns the results to string str.
ezsurf Easy-to-use 3-D colored surface plotter, ezsurf(fun) creates a
graph of fun(x,y) using the surf function. fun is plotted over the
default domain: -2π <x< 2π, -2π <y< 2π.
plot3 The plot3 function displays a three-dimensional plot of a set of
data points.
Enter the function f(x,y)
input
Output Maxima , Minima values for the given function and Graph for
visualizing its solution
MATLAB Code:
clc; clear
% Symbolic variables
syms x y
% Prompt user to enter the function f(x, y)
f(x, y) = input('Enter the function f(x, y): ');
% Differentiate f(x, y) with respect to x and y
p = diff(f, x);
q = diff(f, y);
% Solve for the critical points (ax, ay)
[ax, ay] = solve(p, q);
% Convert symbolic solutions to numerical values
ax = double(ax);
ay = double(ay);
% Calculate the second-order partial derivatives
r = diff(p, x);
s = diff(p, y);
t = diff(q, y);
% Calculate the discriminant D
D = r * t - s^2;
% Create a new figure for plotting
figure
% Plot the function surface
fsurf(f);
% Initialize legend string
legstr = {'Function Plot'};
% Iterate over each critical point
for i = 1:size(ax)
% Calculate the values of T1, T2, and T3 at the current critical point
T1 = D(ax(i), ay(i));
T2 = r(ax(i), ay(i));
T3 = f(ax(i), ay(i));
% Check the nature of the critical point
if (double(T1) == 0)
% Further investigation is required
fprintf('At (%f, %f) further investigation is required \n\n', ax(i), ay(i))
legstr = [legstr, {'Case of Further Investigation'}]; % Updating Legend
mkr = 'ko'; % Marker
elseif (double(T1) < 0)
% The point is a saddle point
fprintf('The point (%f, %f) is a saddle point \n\n', ax(i), ay(i))
legstr = [legstr, {'Saddle Point'}]; % Updating Legend
mkr = 'bv'; % Marker
else
if (double(T2) < 0)
fprintf('The point (%f, %f) is a local maximum point \n', ax(i), ay(i))
% The maximum value of the function
fprintf('The local maximum value of the function is f(%f, %f)=%f\n\n',
ax(i), ay(i), T3)
legstr = [legstr, {'Maximum value of the function'}]; % updating Legend
mkr = 'g+'; % marker
else
fprintf('The point (%f, %f) is the local minimum point \n', ax(i), ay(i))
% The minimum value of the function
fprintf('The local minimum value of the function is f(%f, %f)=%f \n\n',
ax(i), ay(i), T3)
legstr = [legstr, {'Minimum value of the function'}]; % Updating Legend
mkr = 'r*'; % Marker
end
end
% Plot the critical point on the function surface
hold on
plot3(ax(i), ay(i), T3, mkr, 'Linewidth', 4);
end
% Add legend to the plot
legend(legstr, 'Location', ‘Best');
Practice Problems:
1) Find the maximum and minimum value of f (x, y) = 2x 3 + x y 2 + 5x 2 + y 2.
2) Find the local maximum and minimum values of f (x, y) = 2(x 2 − y 2 ) − x 4 + y 4.