# 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.

## 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 on other sites
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 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 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.

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013665
×