Thread: Entering a user equation to a source function ?

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Smile Entering a user equation to a source function ?

    Good day and thanks in advance for any help and ideas with my question...

    I am a newbie programmer primarily interested in writing simple numerical integrators and root finders and evaluators...

    Such as, Euler's Method for approximating integrals and Newton's method for finding roots...Horner's Method for evaluating a polynomail....

    So I have some books on the topic of writing the code for the functions (class objects)
    which assume that the source function is provided.

    Is there a way to have the user input an equation and store it as a string or other, and have that string converted into code ?

    I suppose there must be since matlab and others have a way of doing it....
    I can only hope that it is not too outside of my understanding..

    peace
    dpc

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Parsing equations and formulae like "y=x+1" is very difficult, assuming that's what you mean. Very difficult. I don't think you would be up to the task . . . I'm not sure I would be, especially if I only had a few days to do it.

    I have a few suggestions: you could try to find a library that will do this for you. I don't know of any offhand, but I'm sure they exist. Even if you did, however, it would require some degree of programming skill to get them working -- have you ever used external libraries before?

    Or you could take up another programming language . . . in Perl, for example, you can do this sort of thing quite easily. C++ simply isn't designed to do this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Hi,

    I have not used external libraries before, although that is something I would like to practice..
    I'm only working on this for my own edification btw, not home work .. luckily...

    Can you suggest some good places to start looking for useful analysis orientated libraries ?

    Parsing is what I want to do....doesn't have to be y=x^2, just the x^2 would do...



    thanks,
    d

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Parsing is what I want to do....doesn't have to be y=x^2, just the x^2 would do.
    If you want to evaluate an expression (instead of solve an equation), then check out the shunting yard algorithm by searching the Web.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    I will check it out...

    Just to clarify.....by evaluating an expression many times, then aggregating the sum, I can integrate and thus numerically 'solve' an equation (especially if it is converging on a constant solution)....such as a first order differential equation.

    thanks

    d

  6. #6
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by dcuneo
    Is there a way to have the user input an equation and store it as a string or other, and have that string converted into code ?
    Just yesterday I was googling about binary trees, when I read that one of their application is right about expressions parsing.
    To give you a simple example, the expression A + B * C will be stored this way:
    Code:
        +
       / \
      A   *
         / \
        B   C
    If you want more go here:
    http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Tree/

    Wikipedia offers a good start point about binary trees (if you don't know what they are)
    good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:15 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM