0

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)?

TEX
  • 2,249
  • 20
  • 43

1 Answers1

2

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

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
xvan
  • 4,554
  • 1
  • 22
  • 37
  • As an addendum, these answers provide some insight into choosing the right *h*: https://stackoverflow.com/questions/54049833/trying-to-implement-the-difference-formula-in-matlab/54050959 – Cris Luengo Jun 30 '20 at 00:50