This example shows how to find the extremum of a multivariate function and its approximation near the extremum point. This example uses symbolic matrix variables to represent the multivariate function and its derivatives. Symbolic matrix variables are available starting in R2021a.
Consider the multivariate function , where is a 2-by-1 vector and is a 2-by-2 matrix. To find a local extremum of this function, compute a root of the derivative of . In other words, find the solution of the derivative .
Create the vector and the matrix as symbolic matrix variables. Define the function .
syms x [2 1] matrix syms A [2 2] matrix f = sin(x.'*A*x)
Compute the derivative
D of the function with respect to the vector . The derivative
D is displayed in compact matrix notation in terms of and .
D = diff(f,x)
The symbolic matrix variables
symmatrix objects. These objects represent matrices, vectors, and scalars in compact matrix notation. To show the components of these variables, convert the
symmatrix objects to
sym objects using
xsym = symmatrix2sym(x)
Asym = symmatrix2sym(A)
fsym = symmatrix2sym(f)
Dsym = symmatrix2sym(D)
Suppose you are interested in the case where the value of is
[2 -1; 0 3]. Substitute this value into the function
fsym = subs(fsym,Asym,[2 -1; 0 3])
Substitute the value of into the derivative
Dsym = subs(Dsym,Asym,[2 -1; 0 3])
Then, apply the symbolic function
solve to get a root of the derivative.
[xmin,ymin] = solve(Dsym,xsym,'PrincipalValue',true); x0 = [xmin; ymin]
Plot the function together with the extremum solution . Set the plot interval to and as the second argument of
fplot3 to plot the coordinates of the extremum solution.
fsurf(fsym,[-1 1 -1 1]) hold on fplot3(xmin,ymin,subs(fsym,xsym,x0),'ro') view([-68 13])
You can approximate a multivariate function around a point with a multinomial using the Taylor expansion.
Here, the term is the gradient vector, and is the Hessian matrix of the multivariate function calculated at .
Find the Hessian matrix and return the result as a symbolic matrix variable.
H = diff(f,x,x.')
Convert the Hessian matrix to the
sym data type, which represents the matrix in its component form. Use
subs to evaluate the Hessian matrix for
= [2 -1; 0 3] at the minimum point .
Hsym = symmatrix2sym(H); Hsym = subs(Hsym,Asym,[2 -1; 0 3]); H0 = subs(Hsym,xsym,x0)
Evaluate the gradient vector at .
D0 = subs(Dsym,xsym,x0)
Compute the Taylor approximation to the function near its minimum.
fapprox = subs(fsym,xsym,x0) + D0*(xsym-x0) + 1/2*(xsym-x0).'*H0*(xsym-x0)
Plot the function approximation on the same graph that shows and .
hold on fsurf(fapprox,[-1 1 -1 1]) zlim([-1 3])