![]() |
symbolic differentiation/ tree problem C++
Hello everybody,
For 4 days now I am trying to finish my program but it seems that I am missing something. The way it is now it is supposed to work but it doesn’t. There are no compilation errors but I cannot find where I am missing something. Basically, you should input a expression of the sort (2*x)+3-1 and you should get the derivative of the expression but I cannot even get it to calculate 2+4-2*3. I am hitting my head in the wall for some time now and I need help. here is the source code... PLS I will really appreciate it.:freak::freak::freak: :confused::confused::confused: Code: Cpp
|
Re: symbolic differentiation/ tree problem C++
Code:
...Use one statement per line, and use sensible variable names (i.e. meaningful). Then you can step through it with the debugger and see exactly where it goes wrong. And use correct formatting too, although I appreciate that posting code without code blocks loses the formatting. Repost correctly formatted code and I'll have a go. BTW, the derivative of 2x+3-1 is just 2. In general, d/dx x^n = nx^(n-1) Here's an interesting puzzle for you if you're into calculus. d/dx x^2 = 2x. But d/dx (x+x+x...) (with x x's, so if x=5 then this is d/dx (x+x+x+x+x) ) which is 1+1+1...(x times) = x. Why the difference? Both equations are absolutely correct; when faced with this contradiction, people will often say something like "well d/dx x^2 isn't 2x", or "well d/dx (x+x+x) isn't 1+1+1", but neither of those objections is valid. |
Re: symbolic differentiation/ tree problem C++
OK, against my own better judgement I have stepped through the code, although it's not easy to see what it's supposed to be doing.
What exactly are you expecting it to do? Currently all it does is to get an expression from the user, create an expression tree from that, then output the same expression. It doesn't attempt to differentiate the expression before displaying it. The zero output after the expression tree has been printed seems to be an attempt to evaluate the expression, but as all operations are done on v, and this is always initialised to zero, that probably explains why you don't get a non-zero result. If I add a line like this: Code:
printf("term() creating pt(tn=%d,v=%d,op=%c)\n",pt->tn,pt->v,pt->op);Code:
Enter f(x) = (2*x)+3-1 |
| All times are GMT +5.5. The time now is 18:11. |