Differentiation in C++

Started by
4 comments, last by jarod83 21 years, 10 months ago
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++?
Advertisement
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.
there was a code of the day about this on flipcode if I remember correctly.
-----The scheduled downtime is omitted cause of technical problems.
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]
"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 W. Kernighan
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.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
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]
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement