Thread Subject: Numerical gradient

Subject: Numerical gradient

From: David Doria

Date: 23 Nov, 2009 14:26:19

Message: 1 of 6

All the examples in the gradient() documentation seem to be interested in computing the gradient over a grid. Is there a way to find the gradient at a single point? I.e.

Rather than this:
v = -2:0.2:2;
[x,y] = meshgrid(v);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);

Can you do something like:
F = x * exp(-x^2 - y^2);
g = GradientAtPoint(F,5,6);

To evaluate the gradient of F at (5,6)?
g(1) would simply be :
(F(5+epsilon, 6) - F(5,6) ) / epsilon
and g(2):
(F(5, 6+epsilon) - F(5,6) ) / epsilon

Is there an included function to do this?

Thanks,
Dave

Subject: Numerical gradient

From: John D'Errico

Date: 23 Nov, 2009 14:52:20

Message: 2 of 6

"David Doria" <daviddoria@gmail.com> wrote in message <hee62b$h6$1@fred.mathworks.com>...
> All the examples in the gradient() documentation seem to be interested in computing the gradient over a grid. Is there a way to find the gradient at a single point? I.e.
>
> Rather than this:
> v = -2:0.2:2;
> [x,y] = meshgrid(v);
> z = x .* exp(-x.^2 - y.^2);
> [px,py] = gradient(z,.2,.2);
>
> Can you do something like:
> F = x * exp(-x^2 - y^2);
> g = GradientAtPoint(F,5,6);
>
> To evaluate the gradient of F at (5,6)?
> g(1) would simply be :
> (F(5+epsilon, 6) - F(5,6) ) / epsilon
> and g(2):
> (F(5, 6+epsilon) - F(5,6) ) / epsilon
>
> Is there an included function to do this?

You could use the symbolic toolbox if you have it.

Or you could use my derivest tools, as found on
the file exchange.

F = @(xy) xy(1).* exp(-xy(1).^2 - xy(2).^2);
[gr,err] = gradest(F,[.5 -1])
gr =
         0.143252398430092 0.286504796860189
err =
      5.14977511641761e-14 3.5599128999048e-14

Find it here:

http://www.mathworks.com/matlabcentral/fileexchange/13490

HTH,
John

Subject: Numerical gradient

From: David Doria

Date: 23 Nov, 2009 15:10:17

Message: 3 of 6

> You could use the symbolic toolbox if you have it.
>
> Or you could use my derivest tools, as found on
> the file exchange.
>
> F = @(xy) xy(1).* exp(-xy(1).^2 - xy(2).^2);
> [gr,err] = gradest(F,[.5 -1])
> gr =
> 0.143252398430092 0.286504796860189
> err =
> 5.14977511641761e-14 3.5599128999048e-14
>
> Find it here:
>
> http://www.mathworks.com/matlabcentral/fileexchange/13490
>
> HTH,
> John

Exactly what I was looking for - thanks and good work John!

Dave

Subject: Numerical gradient

From: David Doria

Date: 23 Nov, 2009 15:16:18

Message: 4 of 6


> F = @(xy) xy(1).* exp(-xy(1).^2 - xy(2).^2);
> [gr,err] = gradest(F,[.5 -1])
> gr =
> 0.143252398430092 0.286504796860189
> err =
> 5.14977511641761e-14 3.5599128999048e-14
>
> Find it here:
>
> http://www.mathworks.com/matlabcentral/fileexchange/13490
>
> HTH,
> John

Actually I have one comment/suggestion. Is there a way to wrap this in a function? If you have a 10-D function, this would be a syntax nightmare:

fun = @(x,y,z,a,b,c,....) x.^2 + y.^2 + z + a + b + c + ...;
xy = [2 3];
gradvec = [derivest(@(x) fun(x,xy(2)),xy(1),'d',1), ...
           derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
           derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
           derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
           derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
           derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
]

I didn't actually write that out correctly, but you see what I mean? Could it be something more like
fun = @(x,y,z,a,b,c,....) x.^2 + y.^2 + z + a + b + c + ...;
gradvec = derivestND(fun)

?

Thanks,

Dave

Subject: Numerical gradient

From: John D'Errico

Date: 23 Nov, 2009 16:42:19

Message: 5 of 6

"David Doria" <daviddoria@gmail.com> wrote in message <hee902$6qq$1@fred.mathworks.com>...
>
> > F = @(xy) xy(1).* exp(-xy(1).^2 - xy(2).^2);
> > [gr,err] = gradest(F,[.5 -1])
> > gr =
> > 0.143252398430092 0.286504796860189
> > err =
> > 5.14977511641761e-14 3.5599128999048e-14
> >
> > Find it here:
> >
> > http://www.mathworks.com/matlabcentral/fileexchange/13490
> >
> > HTH,
> > John
>
> Actually I have one comment/suggestion. Is there a way to wrap this in a function? If you have a 10-D function, this would be a syntax nightmare:
>
> fun = @(x,y,z,a,b,c,....) x.^2 + y.^2 + z + a + b + c + ...;
> xy = [2 3];
> gradvec = [derivest(@(x) fun(x,xy(2)),xy(1),'d',1), ...
> derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
> derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
> derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
> derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
> derivest(@(y) fun(xy(1),y,z,a,b,c),xy(2),'d',1),
> ]
>
> I didn't actually write that out correctly, but you see what I mean? Could it be something more like
> fun = @(x,y,z,a,b,c,....) x.^2 + y.^2 + z + a + b + c + ...;
> gradvec = derivestND(fun)
>
> ?
>
> Thanks,
>
> Dave

I'm not really sure what you are looking for. I
thought I understood that you wanted a gradient
vector for a function of N variables. I've already
given you gradest to do that in that same package.
It does what you are looking for, I think.

John

Subject: Numerical gradient

From: David Doria

Date: 23 Nov, 2009 16:59:04

Message: 6 of 6

> I'm not really sure what you are looking for. I
> thought I understood that you wanted a gradient
> vector for a function of N variables. I've already
> given you gradest to do that in that same package.
> It does what you are looking for, I think.
>
> John

I see. I was looking through the derivest_demo.m and saw this example:
% Compute the numerical gradient vector of a 2-d function

But as you said, gradest does exactly this but in a much more compact form.

I'm all set now :)

Thanks,

Dave

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com