How to I compute partial derivatives of a function

Suppose I have a function z=z(x,y), how do I numerically (not symbolically) compute the partial derivatives?
I know of the function gradient(f,dx) which computes general derivatives in one dimension, but what is I want to compute the function:
\frac{\partial^{4}z}{\partial x^{4}}+\frac{\partial^{2}z}{\partial y^{2}}
for example? So I would need to compute them separately. I would rather not do a finite difference solution as that would be a faff. Is there a way of using the gradient function at all?

Answers (2)

Compute the derivatives symbolically using "diff" and turn the result in a function handle using "matlabFunction".
Best wishes
Torsten.

6 Comments

Hi, you didn't see the thing about not symbolically did you?
Let be make it more clear for you. Suppose I have the code:
x=linspace(-6,6,30);y=linspace(-6,6,30);
[X,Y]=meshgrid(x,y);
Then if I have a function defined numerically, how do I compute dz_dx and dz_dy separately and higher derivatives.
You wrote you have a function z=z(x,y) - so I assumed the function is given as an analytical formula.
If you only have values of the function on a stuctured grid, there is no other way than to use finite difference approximations for the partial derivatives, I guess.
Best wishes
Torsten.
In 1D I simply used gradient(f,dx), is there no higher genaralisation to 2D or 3D?
Mat
Do you want to calculate mixed derivatives, e.g. d^2z/dxdy ?
As long as this is not the case, the "gradient" function should suffice also to compute higher-order derivatives. E.g.
d^2z/dx^2 = gradient(gradient(z,dx),dx)
Best wishes
Torsten.
No, mixed derivatives are not required this time, but I need to calculate a sixth order derivative in x and a second order derivative in y.
If I arrange Z as a meshgrid, I can look at doing gradient on separate rows and columns I suppose.
Yes, exactly, you will have to loop over the rows or columns of the z-matrix.
Best wishes
Torsten.

Sign in to comment.

High order partials can be difficult to estimate numerically, and to do so with full precision.
The tool derivest (found on the file exchange) can do a decent job though.
Consider this example function:
z = @(x,y) exp(-(x+2*y).^2);
z(-1,1)
ans =
0.36788
I'll define the variables x0 and y0 so that you can see how to use it. So we want to compute the 4 order partials around the point (x0,y0).
x0 = -1;
y0 = 1;
[dzdx4,ex] = derivest(@(x) z(x,y0),x0,'deriv',4)
dzdx4 =
-7.3576
ex =
3.4866e-08
[dzdy4,ey] = derivest(@(y) z(x0,y),y0,'deriv',4)
dzdy4 =
-117.72
ey =
1.7325e-06
The second returned argument is an error estimate that indicates how well it thinks it did the job. So I am getting roughly 8 significant digits of precision in each direction.
I did them separately before to see the error estimates also.
In one line do this:
D = derivest(@(x) z(x,y0),x0,'deriv',4) + ...
derivest(@(y) z(x0,y),y0,'deriv',4)
D =
-125.08

2 Comments

Hi John,
I am solving a PDE using the Newton method, so my function isn't symbolic, it's just a series of numbers (for ease I am considering writing the matrix as a vector), so I can't write it as a function as it's technically a variable. So I don't know if I can write it as function handle.
I NEVER said the problem needed to be symbolic, did I? In the example I showed, nothing was symbolic, just a function, z(x,y), as you said that you had. But you never said that all you really have is a series of numbers. Should I have known that? Oh, well. No. You cannot use derivest.
If you have no more than a list of numbers, then you need to generally need to use a finite difference approximation. Or you can use finite elements. There are lots of classic ways to solve PDES. Books of them, even.

Sign in to comment.

Asked:

on 11 Aug 2016

Commented:

on 12 Aug 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!