By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

C code to calculate gradient of a function

P: n/a
Olw
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?

Thanks,

Olw
Oct 25 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Olw wrote:
Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Google knows all. I bet "Numerical Recipes in C" might also be a safe
bet for a start, but it is not handy at the moment.
Oct 25 '06 #2

P: n/a
Olw
Clever Monkey wrote:
Olw wrote:
>Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Google knows all. I bet "Numerical Recipes in C" might also be a safe
bet for a start, but it is not handy at the moment.
Well, as far as I can see Numerical Recipes in C asks you to write the
command yourself (dfunc?). Also, googling does not seem to yield any
immediate answers.

This should be such a common task, that I bet some C users out there has
some nice code to share? Anyone ;)

Thanks,

Olw
Oct 25 '06 #3

P: n/a

"Olw" <an****@kommtek.comwrote in message
news:eh**********@orkan.itea.ntnu.no...
Hello,

Does anyone have a nice link to some code for calculating the gradient of
a function in C?

Thanks,

Olw
Try in comp.programming
Oct 25 '06 #4

P: n/a

"Olw" <an***********@kommtek.comwrote in message
news:45******@news.broadpark.no...
Clever Monkey wrote:
>Olw wrote:
>>Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Google knows all. I bet "Numerical Recipes in C" might also be a safe
bet for a start, but it is not handy at the moment.

Well, as far as I can see Numerical Recipes in C asks you to write the
command yourself (dfunc?). Also, googling does not seem to yield any
immediate answers.

This should be such a common task, that I bet some C users out there has
some nice code to share? Anyone ;)

Thanks,

Olw
If you know your function, you should be able to determine its gradient.
For example, if
f(x,y,z) = 2*x + 3*y**2 -sin(z)
then the gradient is the vector
( 2, 6*y, -cos(z) )

If you only have a bunch of points and do not explicitly know
the function, you have to decide how you are going to compute
the gradient. You have to decide how to create the approximation
of the data fit - linear, piecewise linear, least squares, polynomial,
cubic spline, quaternary spline, piecewise continuous multivariate
spline interpolation, ... there are literally an infinite number of
ways you could choose to fit the data. But YOU have to choose.

Check the Linpack, Mathlib, etc., libraries for some possibilities.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 25 '06 #5

P: n/a

Olw wrote:
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Most C functions aren't even differentiable.

--
Bill Pursell

Oct 25 '06 #6

P: n/a
Olw wrote:
>
Does anyone have a nice link to some code for calculating the gradient
of a function in C?
If the longest line in the function is 60 chars, and the function
takes 30 lines, the gradient is height/width, or 30/60, i.e. 1 in
2.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 25 '06 #7

P: n/a

Olw wrote:
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?
news:sci.math.num-analysis is probably the best place for this post.

Maybe:
http://www.google.com/search?hl=en&l...erentiation%22

Oct 25 '06 #8

P: n/a
On Wed, 25 Oct 2006 20:39:24 +0200, in comp.lang.c , Olw
<an***********@kommtek.comwrote:
>This should be such a common task, that I bet some C users out there has
some nice code to share? Anyone ;)
Quite possibly. However this isn't comp.sources.wanted, and the
question isn't a C question, its an algorithm question. You need to
ask in comp.programming or somewhere similar. And do a websearch.

And there quite probably isn't a single generic solution.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 25 '06 #9

P: n/a

Olw wrote:
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?

Thanks,

Olw
This is only an amateur work.

http://www.geocities.com/xhungab/calculus.html

16 Partial differentiation..
c16a.zip : Plot the functions f(x,y)
c16f.zip : Partial derivatives.
c16g.zip : Newton's method for the system of two equations in two
unknowns.
c16h.zip : Implicit differentiation.
c16i.zip : The gradient. The directionnal derivative.
c16j.zip : The gradient. The directionnal derivative. [II]
c16k.zip : Tangent plane, Normal line, level curve.
c16l.zip : Local maximum, minimum, saddle point.
c16m.zip : Local maximum, minimum, saddle point. (II)

Oct 26 '06 #10

P: n/a

Olw wrote:
Clever Monkey wrote:
Olw wrote:
Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Google knows all. I bet "Numerical Recipes in C" might also be a safe
bet for a start, but it is not handy at the moment.

Well, as far as I can see Numerical Recipes in C asks you to write the
command yourself (dfunc?). Also, googling does not seem to yield any
immediate answers.

This should be such a common task, that I bet some C users out there has
some nice code to share? Anyone ;)
I suggest you ask in sci.math.nun-analysis.

I also suggest that you ask about the problem you are trying to solve,
rather than what you think you need to do to solve the problem
(you may or may not be right).

-William Hughes

Hint. Do you think this makes sense? "The reason that Numerical
Recipes does not provide a general gradient function is that the
authors are incompetent and/or lazy."

Oct 26 '06 #11

P: n/a
Olw
Olw wrote:
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?

Thanks,

Olw
Thanks for all the nice inputs.

I will try to find a way based on this.

Olw
Oct 26 '06 #12

P: n/a
Olw
William Hughes wrote:
Olw wrote:
>Clever Monkey wrote:
>>Olw wrote:

Does anyone have a nice link to some code for calculating the gradient
of a function in C?

Google knows all. I bet "Numerical Recipes in C" might also be a safe
bet for a start, but it is not handy at the moment.
Well, as far as I can see Numerical Recipes in C asks you to write the
command yourself (dfunc?). Also, googling does not seem to yield any
immediate answers.

This should be such a common task, that I bet some C users out there has
some nice code to share? Anyone ;)

I suggest you ask in sci.math.nun-analysis.
A scientific analysis of nuns seems quite interesting ;)
>
I also suggest that you ask about the problem you are trying to solve,
rather than what you think you need to do to solve the problem
(you may or may not be right).

-William Hughes

Hint. Do you think this makes sense? "The reason that Numerical
Recipes does not provide a general gradient function is that the
authors are incompetent and/or lazy."
Well, I guess not. But still calculating the gradient should be a quite
common task, and I am also sure that there are plenty of ways doing it
as suggested by Fred L. Kleinschmidt, some of which suit the problem at
hand better than others. Ideally, what I would like to choose from is
some different implementations of this.

Olw
Oct 26 '06 #13

P: n/a

"Olw" <an****@kommtek.comwrote in message
news:eh**********@orkan.itea.ntnu.no...
Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?
Do you mean calculate the gradient at a point, or to differenciate a
function..

Thanks,

Olw

Oct 28 '06 #14

P: n/a
Olw
David Wade wrote:
"Olw" <an****@kommtek.comwrote in message
news:eh**********@orkan.itea.ntnu.no...
>Hello,

Does anyone have a nice link to some code for calculating the gradient
of a function in C?

Do you mean calculate the gradient at a point, or to differenciate a
function..
Sorry about not being precise enough. But it is the gradient at a point
I'm interested in.

Olw
Oct 30 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.