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:
How does the gradient work?

Subject: How does the gradient work?

From: mlt

Date: 15 Dec, 2008 15:56:14

Message: 1 of 3

I have made this in matlab:


x=0:9*pi/2;

% Using matlab's gradient
f = sin(x);
FX = gradient(f);

% Using the definition that df/dx for sin(x) is cos(x)
fderv = cos(x);


But when I print them I get:


FX =

    0.8415 0.4546 -0.3502 -0.8330 -0.5500 0.2387 0.8080
0.6344 -0.1224 -0.7667 -0.7061 0.0037 0.7101 0.7636 0.5704


fderv =

    0.5000 0.2702 -0.2081 -0.4950 -0.3268 0.1418 0.4801
0.3770 -0.0728 -0.4556 -0.4195 0.0022 0.4219 0.4537 0.0684



Why are the result different?

Subject: How does the gradient work?

From: Roger Stafford

Date: 15 Dec, 2008 17:13:02

Message: 2 of 3

"mlt" <asdf@asd.com> wrote in message <49467e39$0$90269$14726298@news.sunsite.dk>...
> I have made this in matlab:
>
> x=0:9*pi/2;
>
> % Using matlab's gradient
> f = sin(x);
> FX = gradient(f);
>
> % Using the definition that df/dx for sin(x) is cos(x)
> fderv = cos(x);
>
> But when I print them I get:
>
> FX =
> 0.8415 0.4546 -0.3502 -0.8330 -0.5500 0.2387 0.8080
> 0.6344 -0.1224 -0.7667 -0.7061 0.0037 0.7101 0.7636 0.5704
>
> fderv =
> 0.5000 0.2702 -0.2081 -0.4950 -0.3268 0.1418 0.4801
> 0.3770 -0.0728 -0.4556 -0.4195 0.0022 0.4219 0.4537 0.0684
>
> Why are the result different?

  The gradient function attempts to approximate the derivative using discretely spaced points, but your points are so far apart this approximation must necessarily be a very poor one.

  By the way, the values you give to fderv don't agree with the x you defined. For example acos(0) is 1, not 0.5. What is the actual x you used?

Roger Stafford

Subject: How does the gradient work?

From: Roger Stafford

Date: 15 Dec, 2008 17:29:03

Message: 3 of 3

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gi636u$qm9$1@fred.mathworks.com>...
> .....
> By the way, the values you give to fderv don't agree with the x you defined. For example acos(0) is 1, not 0.5. What is the actual x you used?
> .....

  I meant cos(0) is 1.

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