Jump to content
  • Advertisement
Sign in to follow this  
Pete Michaud

Algorithm for Differentiation

This topic is 3968 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

I'd like to write a program to differentiate a function, and I'm wondering what technique is most appropriate to compute it. I'm thinking maybe using matrix math could work, but I'm not sure -- does anyone know any resources out there that could give me an overview of a process I could write that could take, for example: 2x^2+x+1 And return: 4x+1 Even though I can differentiate by hand, I just can't seem to wrap my mind around how I could write a method to do it for me without degrading into a mess of special cases. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I would do it by simply asking the user to input the degree of the polynomial
i.e. 2
show them what the equation should look like
i.e. ax^2+bx+c
then just create an array to hold all the variables and simply multiply the first value a by n (n being the degree of the polynomial).
This is obviously going to be more complicated with different types of differentiation but this is what I did for the power rule :).

Share this post


Link to post
Share on other sites
You want to split a function into its terms and express them using a recursive grammar. Each term has a coefficient, an exponent, and an inner expression. You recurse on the inner expression in order to implement the chain rule. So for some term "A*fb(x)", the derivative is A*b*fb-1(x)*diff(f(x))", where the diff() function represents the recursive step. You'd only have to special case a few functions like log(), sin()/cos(), etc. It gets a bit more complicated if you want to handle multivariate functions, but still not too bad.

Be glad you're not trying to do integration as well :)

Share this post


Link to post
Share on other sites
What type of function do you want to differentiate? If you want to differentiate only polynomials than you can simply use a vector to represent the coefficients of the polynomial. If the function is more general than the method presented by Zipster is better.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!