Question on Converting a String into an Algorithm / Equation

This is a discussion on Question on Converting a String into an Algorithm / Equation within the C++ Programming forums, part of the General Programming Boards category; I am considering writing a personal program that uses Newton's Method to compute the second and third approximations of a ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Lightbulb Question on Converting a String into an Algorithm / Equation

    I am considering writing a personal program that uses Newton's Method to compute the second and third approximations of a root.

    The formula is x2=x1-(f(x))/(f'(x))

    I would like the prompt to be "Enter the equation/expression: "

    I know how to program everything, except for how to turn a string of numbers and letters (user-defined variables) into an equation the program understands, to replace f(x) in my pre-defined formula.

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    maybe this will help

    Expression Trees

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    Thank you for the link. I understand that I will have to program loops in a way that imitates the order of operations and search arrays. If anyone has any example code, that would be helpful.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    If the only information the program has is f(x) and it needs f ' (x), the program will have to find the derivative itself .. the only way that I know of to do this is to use a better data structure to contain the equation, as I linked to (press next on the page to continue to code examples). I think its gonna be a bit more complicated than 'program loops'

    (or look for numerical solutions to derivatives)

    on the other hand there probably are libraries for handling expressions, handling derivatives, etc. But then again there probably are libraries which can do newtons method as well, which defeats the purpose of programming this (assuming your trying to learn c++)
    Last edited by rodrigorules; 06-28-2010 at 12:58 PM.

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    Since parsing general equations (as well as finding derivatives) is much more complicated than what the program actually does, I would suggest breaking the user input up like so:

    enter f(x):
    enter f'(x):

    etc...

    That saves a lot of parsing problems. As for parsing the expressions, by far the easiest way is to enter them in RPN (reverse polish notation). Another help is to have the terms of the expressions be whitespace separated. Then you can easily tokenize them.
    GCC on Fedora Core 10

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Converting String to Math Equation
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2005, 06:59 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21