Results 1 to 5 of 5
 06242009, 05:30 PM #1
How to best design a calculus solver
I have loads of free time and nothing better to do so I wanted to write a program that could do simple calculus problems for me(derivation, integration, eventually u substitution, chain rule, l'hospital's rule, etc) but was having an issue on how to implement this. Right now I have it set up to accept an input in this format 2x^3 + 3x^2 + 4x + 5 and I'm using converting it to postfix notation(however very poorly converting it seems to be broken also). The converted form of that equation ends up being 233245x^+x^+x+
I am currently using a Stack<Integer> and a Stack<Character> to store the values but I don't think this is the proper way to go about it.
Can anyone lead me in the right direction or lend some insight? If you're unfamiliar with calculus the equation I entered should end up being
6x^2 + 6x + 4 after finding the derivative and 12x + 6 after finding the second derivative.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
 06242009, 11:41 PM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
pemdas? just kidding. i haven't tried anything like this myself (and might actually give it a go on the weekend), so i can't say something too concrete, especially since i haven't touched calculus in a few years.
anyways, i don't know if you need a stack or need to convert to post fix as you aren't actually evaluating the expressions. split by + and  first, then identify * and /, do something recursively, hocus pocus, something will happen?
 06252009, 04:39 AM #3Senior Member
 Join Date
 Dec 2008
 Posts
 526
 Rep Power
 0
2x^3 + 3x^2 + 4x + 5
 06252009, 05:02 AM #4Member
 Join Date
 Feb 2009
 Posts
 92
 Rep Power
 0
I have an HP calculator and RPN notation is the only way to go. Post fix seems unnatural, but once you get use to the thought process, it actually is intuitive and avoids parens.
To me, your code seems obviously wrong. Here are the keystrokes I entered into my HP to solve the problem. The contents of the stack are extended to the right. I am using 2 as my value for X. I think it will be easier for you to see what is happening The first item is the accumulator or lowest line on the display.
^
4 (X^2) 3(a2) 16
*
The sequence of keystrokes is
2 2 3 ^ * 3 2 2 ^ * + 4 2 1 ^ * + 5 +
To minimize calculating an exponential (accuracy and speed considerations) factor your formula like this
X*(2*X^2 + 3 *X +4) +5 =
X*(X*(2*X +3) +4) + 5
now, using say 6 s for X to keep everything , the key sequence is
2 6 * 3 + 6 * 4 + 6 * 5 + = 569 as we calculate from the inside out.
This was checked against calculating the partial sums by a normal infix method.
the key strokes on an infix calulator are
2 * 6 = + 3 = * 6 = + 4 = * 6 = + 5 or 5 extra keystrokes for the ='s
While the thought process for the collapsing calculation is hardly intuitive, examining the keystrokes show that a3, a2, a1, and a0 are in exactly in order with an alteration of the *,+ operators. This formula can be calculated very quickly manually on a calculator by rote. You don't have to figure out where to put parens. RPN calculations require no parens.
Ok, so the whole point of all of this is that I could see that what seems to be your operation sequence is clearly wrong. When I enter the sequence in to my calculator, the first calculation is (4*5)^2 or 400. Or maybe you meant X^5, again, clearly wrong.
IIRC, Strouskoup (the creator of C++) in his book The C++ Programming language, has an example of programming a calculator using postfix (RPN) notation. Realizing that I have the advantage of age and experience, let me extend my pedagogy by explaining that RPN means Reverse Polish Notation because of a Pole that introduce prefix notation (polish notation).
The Wikopedia article has a reference to algorithms for parsing to RPN notation. Your cookie now. Don't reenvent the wheel, I hope I have pointed you in the right direction for your researchl
 06252009, 05:27 AM #5Member
 Join Date
 Feb 2009
 Posts
 92
 Rep Power
 0
Somehow, I lost a whole length post showing the key strokes and the state of the stack for an rpn calculator on this formula. At this point, I'm going to leave it to the rest of you to set down with a piece of paper and figure it out. Remember that RPN uses a stack, so there will be data on the stack waiting for the next operator.
substitute your own number for X.
2 X 3 ^ * 3 X 2 ^ * + 4 X 1 ^ * + 5 +
quick check X = 2 > 41
X = 6 > 569
I am omitting the ENTER to push each value onto the top of the stack.
i.e 2 enter X enter 3 ^ * 3 enter etc.
Technically, you would enter a number into the accumulator after pushing the accumulator onto the stack. Each operand works on the value in the accumulator and the top of the stack.
stack 2
stack 3
accumulator 4
enter *
stack 2
accumulator 12
enter 3
stack 2
stack 12
accumulator 3
+
stack 2
accumulator 15

accumulator 13
Notice that the X 1 ^ could be substituted with simply X, but is consistent with which you would wind up with in a looping calculationLast edited by rdtindsm; 06262009 at 07:39 AM. Reason: error in stack state, chnge sign on 13
Similar Threads

For the people who love Calculus
By tim in forum Forum LobbyReplies: 9Last Post: 12012009, 12:53 PM 
JSP Design.
By makpandian in forum NetBeansReplies: 0Last Post: 04202009, 01:21 PM 
Aircraft gui design
By bozovilla in forum AWT / SwingReplies: 6Last Post: 09302008, 03:08 PM 
Rubiks Cube Solver
By sufs2000 in forum Advanced JavaReplies: 0Last Post: 06032008, 03:20 PM 
Design patterns
By Freddie in forum New To JavaReplies: 2Last Post: 05122007, 06:21 AM
Bookmarks