Results 1 to 5 of 5
  1. #1
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Houston, TX & Flint, MI
    Rep Power

    Unhappy 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 post-fix 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.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Rep Power


    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?

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    2x^3 + 3x^2 + 4x + 5
    Why you want to use x? You mean you write some methods to analyze the String math expression?

  4. #4
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Rep Power


    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

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Rep Power


    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 calculation
    Last edited by rdtindsm; 06-26-2009 at 07:39 AM. Reason: error in stack state, chnge sign on 13

Similar Threads

  1. For the people who love Calculus
    By tim in forum Forum Lobby
    Replies: 9
    Last Post: 12-01-2009, 01:53 PM
  2. JSP Design.
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 04-20-2009, 01:21 PM
  3. Aircraft gui design
    By bozovilla in forum AWT / Swing
    Replies: 6
    Last Post: 09-30-2008, 03:08 PM
  4. Rubiks Cube Solver
    By sufs2000 in forum Advanced Java
    Replies: 0
    Last Post: 06-03-2008, 03:20 PM
  5. Design patterns
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts