derivative of a polynomial function in C and ...

Discussion in 'Programmer's Corner' started by Eric007, Oct 3, 2015.

  1. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    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
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
  3. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Ok. Writing that function would be more interesting anyway. That a good question. Thinking... any suggestions are welcome as well.
     
    Last edited: Oct 3, 2015
  4. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    an example would be x^3 + 5x^2 - 1. This is just the form but it could be anything.
     
  5. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    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: Oct 3, 2015
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,393
    497
    There is a reason we have Matlab, Maple and other math oriented packages.
     
    Eric007 likes this.
  7. vpoko

    Member

    Jan 5, 2012
    258
    47
    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.
     
    Eric007 likes this.
  8. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    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.
     
    Eric007 likes this.
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    If you limit yourself to polynomials, differentials are trivial. Easier than arbitrary algebra for sure, only one rule to apply.
     
  10. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    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!
     
  11. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    Hahahahah... I like complicated things...

    I know you are a genius :) and you also like complex things too.

    I want to work with very simple polynomial functions...
     
  12. vpoko

    Member

    Jan 5, 2012
    258
    47
    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.
     
    Eric007 likes this.
  13. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,910
    2,172
    thumb2 likes this.
  14. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    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: Oct 4, 2015
  15. MrAl

    Well-Known Member

    Jun 17, 2014
    2,438
    492
    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.
     
    Eric007 and vpoko like this.
  16. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
    Eric007 likes this.
  17. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,044
    33
    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
     
  18. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
  19. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    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.
     
  20. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
Loading...