Archived

This topic is now archived and is closed to further replies.

Differentiation in C++

This topic is 5669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you differentiate a mathematical expression in C++? For instance this: Function: f(x) = 2x^2 +6x + 7 Function differentiated: f''(x) = 4x + 6 And by the way; can you integrate a function in C++?

Share this post


Link to post
Share on other sites
Well, you can do it numericly quite easy, but i see thats not what you want. AFAIK C++ has no built in functions for this, so you should probably look for some code done by others, or just write it yourself.

Share this post


Link to post
Share on other sites
differentiation : f'(x) = lim(h->0) (f(x+h)-f(x))/h
Thus

const double epsilon = 0.00001;
fprime = (f(x*(1+epsilon))-f(x))/(x*epsilon)

Be careful of the possible loss of precision in 1+epsilon.
I think there actually is a constant in or
in that defines the smallest number which can be added to 1 and make the value change. Browse through the headers for the actual constant.

Integration: See Numerical Recipes, chapters 4 and 16.

Symbolic integration and differentiation : not unless you code it in. Use Mathematica, Maple or MathCad instead...

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 4, 2002 12:14:38 PM]

Share this post


Link to post
Share on other sites
Do a web search on "symbolic math". Also, you can look for the codes "Maple", "Macsyma", and "ADIFOR", among others. The ADIFOR code actually takes entire FORTRAN functions and differentiates the returned value. I realize its not C, and its not freely available, but its interesting.

By the way, this is not another "why don''t you try google first?" post. I simply don''t really have an answer readily available, and I''d rather you spend time searching for yourself.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post


Link to post
Share on other sites
quote:
Original post by jarod83
How do you differentiate a mathematical expression in C++?


With a token parser, an abstact expression graph, and a calculus engine. Boost can help you with the first two...



Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

[Look for information | GDNet Start Here | GDNet Search Tool | GDNet FAQ | MSDN RTF[L] | SGI STL Docs | STFW | Asking Smart Questions ]

[Free C++ Libraries | Boost | ACE | Loki | MTL | Blitz++ | wxWindows]

Share this post


Link to post
Share on other sites