Star
Star

Reputation: 2299

Numerical gradient of a Matlab function evaluated at a certain point

I have a Matlab function G(x,y,z). At each given (x,y,z), G(x,y,z) is a scalar. x=(x1,x2,...,xK) is a Kx1 vector.

Let us fix y,z at some given values. I would like your help to understand how to compute the derivative of G with respect to xk evaluated at a certain x.

For example, suppose K=3

function  f= G(x1,x2,x3,y,z)
          f=3*x1*sin(z)*cos(y)+3*x2*sin(z)*cos(y)+3*x3*sin(z)*cos(y);
end

How do I compute the derivative of G(x1,x2,x3,4,3) wrto x2 and then evaluate it at x=(1,2,6)?

Upvotes: 0

Views: 447

Answers (1)

xvan
xvan

Reputation: 4855

You're looking for the partial derivative of dG/dx2

So the first thing would be getting rid of your fixed variables

G2 = @(x2) G(1,x2,6,4,3);

The numerical derivatives are finite differences, you need to choose an step h for your finite difference, and an appropriate method

The simplest one is

(G2(x2+h)-G2(x2))/h

You can make h as small as your numeric precision allows you to. At the limit h -> 0 the finite difference is the partial derivative

Upvotes: 2

Related Questions