Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How does the gradient work?
Date: Mon, 15 Dec 2008 17:13:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 30
Message-ID: <gi636u$qm9$1@fred.mathworks.com>
References: <49467e39$0$90269$14726298@news.sunsite.dk>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1229361182 27337 172.30.248.37 (15 Dec 2008 17:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 15 Dec 2008 17:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:507128


"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