Finding derivative of a function stored in a character array

前端 未结 3 1922
后悔当初
后悔当初 2021-01-26 11:30

What I need to do is read a file which contains equations. I need to take the derivative of each equation and then write those derivative equations in a different .txt file. I\'

3条回答
  •  盖世英雄少女心
    2021-01-26 12:10

    What you need to do, by the looks of things, is separate the expression into individual terms so that you can find the derivative of each in turn.

    You can define a term as the largest sequence of characters not containing term separators such as (in your examples) + and -.

    Hence the terms for your examples are:

    -2x^2+2x-3     =>  2x^2  2x     3
    -2x+sinx-3     =>  2x    sinx   3
    -x+sin2x-tanx  =>  x     sin2x  tanx
    

    For each term, you then need to evaluate the form of the term. The form will dictate how you create the derivative.


    For example, you can detect if it contains a trigonometric function of the form [n]sin[m]x where n and m are optional numbers. To simplify things, you could add in those terms if they're not there, such as sinx becoming 1sin1x (I'll call this the full-form of the term). Being able to assume all subterms are present will greatly ease the derivative calculation.

    Let's say the term is sin4x. Expanding that will give you 1sin4x which you can then split into term-multiplier 1, function sin and x-multiplier 4. Then using standard derivative knowledge nsinmx => (n*m)cosmx, this would become 4cos(4x) and that term would be done.


    If it doesn't contain a trigonometric function, you can use the same full-form trick to cover all of the power/constant expressions with the following rules in turn:

    • if it's a constant (all numeric), append x^0 (multiply by 1).
    • if it ends with x, append ^1, so 4x becomes 4x^1.
    • if it starts with x, prefix it with 1, so x^3 becomes 1x^3.

    Once that's done, you will have a full-form of ax^b and you can then create the derivative (ab)x^(b-1) and post-process it:

    • if the bit after x is ^0, remove the whole x^0.
    • if the bit after x is ^1, remove the ^1.
    • if the bit before the x is 1, remove it.
    • if the bit before the x is 0, remove the entire term (and preceding term separator, if any).

    So, taking a complex combination of your test data:

    -2x^2 + 5x + 4sin3x - 3
    

    which can be treated as:

    0 - 2x^2 + 5x + 4sin3x - 3
    

    The following actions happen to each term:

    0      [0x^1]   (derives as) 0, remove it.
    2x^2   [2x^2]   (derives as) (2*2)x^(2-1) => 4x^1 => 4x
    5x     [5x^1]   (derives as) (5x1)x^(1-1) => 5x^0 => 5
    4sin3x [4sin3x] (derives as) 12cos3x
    3      [3x^0]   (derives as) 0, remove it and preceding '-'
    

    Thus you end up with - 4x + 5 + 12cos3x which, although my calculus education is some thirty years in the past (and I don't think I've used it since, though I will no doubt be using it next year when my eldest hits secondary school), Wolfram Alpha appears to agree with me :-)

提交回复
热议问题