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've read all the equations into an array of character arrays and now I don't know what to do once I've stored them into the array. I really don't need help writing the equations into another file; I know I can figure that out.
What I need help on is finding a way to taking the derivative of the functions. The type of equations that are going to be read are not that complicated; they're going to be polynomials that don't need the chain rule or quotient rule. There will be, however, sin x, cos x and tan x. Some sample equations that would be read are.
-2x^2+2x-3
-2x+sinx-3
-x+sin2x-tanx
The trig functions will not have parenthesis and the variable will always be "x". I just need a push in the right direction, please.
What you're really asking for is a parser. A parser is basically a set of rules to read those equations and change/read (parse) each of them. I'd try to iterate over each line of the file, and differentiate it considering you have a specific character set (i.e ^ means power, x is the parameter, etc.);
For example, some pseudo code:
If you need help translating that into C, just ask for it; I'll happily help you.
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:
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
wheren
andm
are optional numbers. To simplify things, you could add in those terms if they're not there, such assinx
becoming1sin1x
(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 you1sin4x
which you can then split into term-multiplier1
, functionsin
and x-multiplier4
. Then using standard derivative knowledgensinmx => (n*m)cosmx
, this would become4cos(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:
x^0
(multiply by 1).x
, append^1
, so4x
becomes4x^1
.x
, prefix it with1
, sox^3
becomes1x^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:x
is^0
, remove the wholex^0
.x
is^1
, remove the^1
.x
is1
, remove it.x
is0
, remove the entire term (and preceding term separator, if any).So, taking a complex combination of your test data:
which can be treated as:
The following actions happen to each term:
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 :-)This function will parse the text, cut it in to different parts identified by
type[i]
, stores in a structure. It recognizesx
, +, -, and numbers. It can be expand it to include other operators etc.