derivative of a polynomial function in C and ...

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Hi All,

How y'all doing?

1. I need a to know if the there's a function in a C library that compute the derivative of a polynomial function?
2. Assuming there's no function that computes it, how would you write that? While you trying to answer me I am also trying to look at the analytical method that compute the derivative of a function.

Many thanks!
eric007
 

WBahn

Joined Mar 31, 2012
29,978
I don't believe that there is such a function, particularly not in the standard C libraries. C is focused much to much on the low level stuff to have something like that.

As for how to write such a function, that depends in part on how you intend to represent your polynomial function.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
I don't believe that there is such a function, particularly not in the standard C libraries. C is focused much to much on the low level stuff to have something like that.

As for how to write such a function, that depends in part on how you intend to represent your polynomial function.
Ok. Writing that function would be more interesting anyway. That a good question. Thinking... any suggestions are welcome as well.
 
Last edited:

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
So basically, at program startup you enter f(x). There should be a function, let's say, df_x, which is the derivative of f_x. We should be able to compute f_x (x0) and df_x(x0) as well where x0 is an integer constant.
 
Last edited:

vpoko

Joined Jan 5, 2012
267
You just need to break the string up into terms, isolate the exponent and coefficients, subtract one from the exponent, and the coefficient becomes the old coefficient times the original exponent.
 

joeyd999

Joined Jun 6, 2011
5,237
Symbolic derivative in C?

Do you ever do anything easy?

When I was about 12, I wrote a BASIC program that symbolically evaluated algebraic equations. That was hard enough.
 

vpoko

Joined Jan 5, 2012
267
Symbolic derivative in C?

Do you ever do anything easy?

When I was about 12, I wrote a BASIC program that symbolically evaluated algebraic equations. That was hard enough.
If you limit yourself to polynomials, differentials are trivial. Easier than arbitrary algebra for sure, only one rule to apply.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
You just need to break the string up into terms, isolate the exponent and coefficients, subtract one from the exponent, and the coefficient becomes the old coefficient times the original exponent.
Of course yes but I was thinking of being able to input a polynomial of any degree and compute the above. Well maybe for simplicity, I can only process polynomial of degree equal to 2 or 3 and that would be easy to parse it (excuse my french)... well any suggestions... this can actually become a complex thing but I want to keep it very simple. It would be so easy if the function is know already but that would be a useless program.

Thx!
 

vpoko

Joined Jan 5, 2012
267
Of course yes but I was thinking of being able to input a polynomial of any degree and compute the above. Well maybe for simplicity, I can only process polynomial of degree equal to 2 or 3 and that would be easy to parse it (excuse my french)... well any suggestions... this can actually become a complex thing but I want to keep it very simple. It would be so easy if the function is know already but that would be a useless program.

Thx!
It's not much easier if you limit the degree. You just need to tokenize the string, put each term into a linked list or other structure, and then apply the power law of derivatives to each term. Should be fairly easy.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
It's not much easier if you limit the degree. You just need to tokenize the string, put each term into a linked list or other structure, and then apply the power law of derivatives to each term. Should be fairly easy.
Sounds good. Will try to come up with something... it's quite late here and need to sleep. Hope to see more comment when I wake up.
 
Last edited:

MrAl

Joined Jun 17, 2014
11,389
Hello,

Regular real valued polynomials already have a general form:
y=a0+a1*x+a2*x^2+a3*x^3+...+aN*x^N

The derivative with respect to x is:
y'=a1+2*a2*x+3*a3*x^2+...+N*aN*x^(N-1)

The main thing to do is store the coefficients, which the user could enter, they dont have to enter the real string.
For a poly like:
y=1+5*x+6*x^2

they would enter 1,5,6

and for:
y=1+6*x^2

which is missing the 'x' term, they would enter 1,0,6
and that's all you really need.
The coefficients would be stored in an array.
 

WBahn

Joined Mar 31, 2012
29,978
an example would be x^3 + 5x^2 - 1. This is just the form but it could be anything.
Again, it comes down to how you want to represent the information.

If you keep an array of the coefficients then you can compute the coefficients of the derivative very easily.

You have a couple of options. The first is to maintain an array in which the index is the exponent -- though this only works if the exponents are non-negative integers. But if the order of your polynomial is large and/or arbitrary, then it might be better to maintain parallel arrays with the coefficients in one and the exponents in the other.

If you want the person to enter the polynomial, then you have to decide how you want them to enter it. They could enter it as two lists containing coefficients in one and exponents in the other. Another would be to provide a list of coefficient/exponent pairs. Yet a third would be to enter the polynomial as a string and then parse it to generate your internal representation.
 

Thread Starter

Eric007

Joined Aug 5, 2011
1,158
Entering the polynomial as string then parsing is interesting (I like parsing) but for now as I want to keep everything simple and basic I think I will go for the option where the user enters only the coefficients as mentioned by @MrAI
 

WBahn

Joined Mar 31, 2012
29,978
Entering the polynomial as string then parsing is interesting (I like parsing) but for now as I want to keep everything simple and basic I think I will go for the option where the user enters only the coefficients as mentioned by @MrAI
Be sure to think about the limitations of that approach.

From a practical standpoint, what if the person wants to enter f(x) = x^27 - 1 ?

Then consider if you want to limit yourself to strict polynomial functions requiring that the exponents be a set of the whole numbers (non-negative integers). If you do (and if the order of the polynomial is going to be reasonably small) then this is a good way to go.
 

Papabravo

Joined Feb 24, 2006
21,159
You can "evaluate" the value of the derivative of a polynomial without using symbolic differentiation:

\(f'(x)= \frac{f(x+\Delta h) - f(x)} {\Delta h}\)

with tiny values of \(\Delta h\)

1e-7 for single precision and 1e-15 four double precision for example.
 

WBahn

Joined Mar 31, 2012
29,978
You can "evaluate" the value of the derivative of a polynomial without using symbolic differentiation:

\(f'(x)= \frac{f(x+\Delta h) - f(x)} {\Delta h}\)

with tiny values of \(\Delta h\)

1e-7 for single precision and 1e-15 four double precision for example.
I don't think he is wanting to do numeric estimation of the derivative. But even if he is, setting the delta to that value is inviting disaster. If x is greater than 1 then you will get zero for the numerator.
 
Top