Need an advice about how to develop a derivatives solver

Hi, I'm not a C++ expert, but I was given the assignment of developing a C++ derivatives solver...

I really have no idea about how to start... so I'd like you to guide me.

The user is supposed to type the function and the program will then show the derivative of the expression...

For example I type 2x^3, the program should return "6x^2"...

Thanks for the ideas... if you can help me with some code of a similar procedure I'd be happy :D.
You'll need to enter this input as a string. Then you'll have to "parse" the expression. This means that you will need to identify what is a coeffecient, what is a variable, what is an exponent, etc...

Then you just need to apply your derivative algorithm:
1
2
coeffecient*=exponent;
exponent--;



Ussually when I need to calculate a derivative, I do not evaluate the new expression. I take the change in the value and divide it by the change in time. Sometimes I do this every iteration, and sometimes I do a moving average over a period of time.
Setup a structure like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// these you can populate using simple I/O and for loop
int size = 5;
float coef [size] = {1,2,3,4,5};
float exponent[size] = {8,7,6,5,4};

// New function coefficient and exponent
float coef_new[size];
float exponent_new[size];

for (int i = 0; i < size; i++)
{
	coef_new[i] = coef[i] * exponent[i];
	exponent_new[i] = exponent[i] - 1;
}

// Print function
for (i = 0; i < size; i++)
{
	cout << coef_new[i] << "x^" << exponent_new[i] << " ";
}
Last edited on
Is your program supposed to derivate any functions or just those of the form ax^b?
If so, thepedestian solution work fine.
If not, you have to learn about parsing to analyse the function and AST(Abstract Syntax Tree) to store your function.
Here is a link to understand LL parsers which can be silmply implemented by hand: http://en.wikipedia.org/wiki/LL_parser
A link to AST: http://en.wikipedia.org/wiki/Abstract_syntax_tree
Here your AST will represent functions so a node could be:
-a constant
-a variable
-a multiplication
...
thepedestrian is good for ax^b, but i need a general application, for expressions that could be as complex as

f(x)=(3x+3)(3x)/(x^2+3)

I was reading about expression trees, can those be useful for this program?
expression tree is more or less the same thing as AST so yes it is useful. Read the links i gave in my precedent post, i think it is useful for your program.
Whats the difference between a LR and a LL?
LL means the parser parse Left to right and construct the tree by Leftmost derivation which means the first nonterminal symbol is rewriten with a grammar rule.
LR means the parser parse Left to right and construct the tree by Rightmost derivation which means the last nonterminal symbol is rewriten with a grammar rule.
To put it simply, LL parsers are easy to implement by hand as opposite to LR which are very difficult to implement by hand and are genarated with tools like Bison
Topic archived. No new replies allowed.