Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
gradient

Subject: gradient

From: mahshid

Date: 21 Oct, 2010 01:24:04

Message: 1 of 3

Hi there;

i need to obtain first and second derivative numerically in my code. can i use gradient in the following form:
[px,py] = gradient(qz,.25,.25);
 [pxx,pxy]=gradient(px,.25,.25);
 [pyx,pyy]=gradient(py,.25,.25);
 tnx

Subject: gradient

From: Roger Stafford

Date: 21 Oct, 2010 03:12:03

Message: 2 of 3

"mahshid " <mahshid770@yahoo.com> wrote in message <i9o4nk$kp9$1@fred.mathworks.com>...
> Hi there;
>
> i need to obtain first and second derivative numerically in my code. can i use gradient in the following form:
> [px,py] = gradient(qz,.25,.25);
> [pxx,pxy]=gradient(px,.25,.25);
> [pyx,pyy]=gradient(py,.25,.25);
> tnx
- - - - - - - - - - -
  Doing 'gradients' of 'gradients' may not result in an optimum approximation to second derivatives. For example, with a single variable, if vector v = [v1,v2,v3,v4,v5], the gradient at the 2nd, 3rd, and 4th positions will be the central differences (v3-v1)/(2*h), (v4-v2)/(2*h), and (v5-v3)/(2*h). If the gradient is taken again, this results in

 ((v5-v3)/(2*h)-(v3-v1)/(2*h))/(2*h) = (v5-2*v3+v1)/(4*h^2)

at the 3rd position. While this may be adequate for some purposes it is probably not optimum. The values v2 and v4 are not represented at all in this differencing of differences. If one is going as far out as v1 and v5, there are better expressions involving all five v values for approximating the second derivative at this 3rd position.

Roger Stafford

Subject: gradient

From: mahshid

Date: 21 Oct, 2010 03:38:03

Message: 3 of 3

Tanx Roger,

You are right, now could u pls tell me how i can obtain these 2nd derivatives numerically?do i have to fit a curve before? for instanse i tried "differentiate" but i seems that a curvfitting must be done before.

Regards,
Mahshid
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invlid> wrote in message <i9ob23$pcb$1@fred.mathworks.com>...
> "mahshid " <mahshid770@yahoo.com> wrote in message <i9o4nk$kp9$1@fred.mathworks.com>...
> > Hi there;
> >
> > i need to obtain first and second derivative numerically in my code. can i use gradient in the following form:
> > [px,py] = gradient(qz,.25,.25);
> > [pxx,pxy]=gradient(px,.25,.25);
> > [pyx,pyy]=gradient(py,.25,.25);
> > tnx
> - - - - - - - - - - -
> Doing 'gradients' of 'gradients' may not result in an optimum approximation to second derivatives. For example, with a single variable, if vector v = [v1,v2,v3,v4,v5], the gradient at the 2nd, 3rd, and 4th positions will be the central differences (v3-v1)/(2*h), (v4-v2)/(2*h), and (v5-v3)/(2*h). If the gradient is taken again, this results in
>
> ((v5-v3)/(2*h)-(v3-v1)/(2*h))/(2*h) = (v5-2*v3+v1)/(4*h^2)
>
> at the 3rd position. While this may be adequate for some purposes it is probably not optimum. The values v2 and v4 are not represented at all in this differencing of differences. If one is going as far out as v1 and v5, there are better expressions involving all five v values for approximating the second derivative at this 3rd position.
>
> Roger Stafford

Tags for this Thread

No tags are associated with this thread.

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.

Contact us