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

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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com