we consider the function f(x,y,z)=2*x*y-z^2 compute the gradient of f at point (x,y,z)=(3,-1,1) in matlab
To compute the gradient of function f(x,y,z)=2*x*y-z^2 at point (x,y,z)=(3,-1,1) in MATLAB, we can use MATLAB's gradient function as follows:
main.m
syms x y z % Declare symbolic variablesf = 2*x*y - z^2; % Define the functiongradf = [diff(f,x); diff(f,y); diff(f,z)]; % Compute the gradientgradf_evaluated = subs(gradf, [x y z], [3-11]); % Evaluate the gradient at (3,-1,1)
231 chars
5 lines
The output of gradf_evaluated will be [6; -2; -2].