Thread: mathematical equations

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    mathematical equations

    Hi,can anyone help me out how to accept mathematical equations involving log,cos,sin,e,etc. Whether I should use structure or array.Please let me know.I have been given a project to write a program for simpson's 1/3 rule and i am confused about how to accept those equations.
    THANKS........

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Post the whole description of the assignment. What you just posted gives very little information on what it is you need to do.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    My program is to write an algorithm for simpson's 1/3 rule and hence a computer program to find the integral of a equation

    integral from 4 to 5.2 for the equation (log(sin x))^2.

    I want to know how to take this equation i.e.(log(sin x))^2 as an input in the program.I need to take the input in a structure or array, i m confused.
    rest of the program i can do it but this initial part is compklex to me.....

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A few things:

    1) You seem to be confused about the difference between an algorithm and a computer program.

    2) In order to read equation input like you want you need to produce some sort of syntax parser which is a very difficult task. You can't just take the input in a "structure or array". You need to take it as a string, parse it and make sense of the operations involved in it and their precedence, typically by using a stack.

    I remember I wrote something similar to this some years back and it only accepted parantheses, basic operations and logarithms and it was a pain in the back to write. If you want to write something that parses any mathematical equation in C you are going to be coding for a loooong time.

    If you are not restricted to using C perhaps Matlab or Mathematica would be more helpful here.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    What is a syntax parser, can you explain me a bit?
    And also you mean programs involving mathematical equations are very complex and not possible.........

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by rakeshkool27 View Post
    What is a syntax parser, can you explain me a bit?
    And also you mean programs involving mathematical equations are very complex and not possible.........
    I am certainly not implying it's impossible. But, it takes a lot of work to get there.

    First of all you have to be familiar with grammars and how they work, preferably with BNF/EBNF notation, parse trees, etc. You should perhaps explore these topics first, before starting such a huge project.

    Good luck

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by rakeshkool27 View Post
    What is a syntax parser, can you explain me a bit?
    And also you mean programs involving mathematical equations are very complex and not possible.........
    I seriously want to know where people come up with this crap. REALLY.

    "I want to write a program to do X. Is it possible?"

    "I can't make the computer do Y. It must be impossible?"

    Yeah man. It's the computer that's limited. Has nothing to do with the fact that you don't know anything yet. We landed robots on the planet Mars, which is amazing considering that programs "involving mathematical equations" are so complex that they simply aren't possible.

    The Mars rovers shipped with basically a really good random number generator. Whenever the rover wants to do something it rolls the dice and prays (yes, we attached two mechanical hands to the back of it so that it can pray properly)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Wink

    Quote Originally Posted by brewbuck View Post
    I seriously want to know where people come up with this crap. REALLY.

    "I want to write a program to do X. Is it possible?"

    "I can't make the computer do Y. It must be impossible?"

    Yeah man. It's the computer that's limited. Has nothing to do with the fact that you don't know anything yet. We landed robots on the planet Mars, which is amazing considering that programs "involving mathematical equations" are so complex that they simply aren't possible.

    The Mars rovers shipped with basically a really good random number generator. Whenever the rover wants to do something it rolls the dice and prays (yes, we attached two mechanical hands to the back of it so that it can pray properly)
    LOL. Good one!

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by rakeshkool27 View Post
    What is a syntax parser, can you explain me a bit?
    And also you mean programs involving mathematical equations are very complex and not possible.........
    If you want a nice intro to parsing, check this out:

    Parsing

    I wrote that as part of a discussion last summer. The final example (under "binary tree" parsing) is in C, less than 100 lines and will process any normal arithmetic, eg:

    [root~/C] ./a.out "100-(2+21)*(3/2)+5"
    lex 100-(2+21)*(3/2)+5
    lex 100-(2+21)*(3/2)
    lex 100
    lex (2+21)*(3/2)
    lex (2+21)
    lex 2+21
    lex 2
    lex 21
    lex (3/2)
    lex 3/2
    lex 3
    lex 2
    lex 5
    parse 2.000000 + 21.000000
    parse 3.000000 / 2.000000
    parse 23.000000 * 1.500000
    parse 100.000000 - 34.500000
    parse 65.500000 + 5.000000
    The answer is 70.500000




    HOWEVER, while we are on the topic of "where people come up with this crap", the OP probably does NOT need to actually write any kind of syntax parser if the program is just to demonstrate a single algorithm: it just has to be able to input the appropriate variables. For example, a program that calculates the area of a circle might take the radius as an input -- it does not really need to parse the equation "pi*r^2", that will be hardcoded.

    So while parsing equations is an interesting topic, I think we may have been leading someone slightly astray
    Last edited by MK27; 03-13-2010 at 12:19 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The OP stated several times that he needs to take as input an equation (actually a mathematical function) so he is not solving a particular one. That requires parsing one way or another.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by claudiu View Post
    The OP stated several times that he needs to take as input an equation (actually a mathematical function) so he is not solving a particular one.
    No, he does not say that at all, except once hesitantly. In fact, what he does say quite clearly several times is that he needs to "write an algorithm for simpson's 1/3 rule" -- which would be a particular equation.

    That does not requiring parsing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, regardless what he wants, he has not responded as of yet, so let's wait for his reply and I guess we will see which of the two he is actually referring to.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    No, he does not say that at all, except once hesitantly. In fact, what he does say quite clearly several times is that he needs to "write an algorithm for simpson's 1/3 rule" -- which would be a particular equation.

    That does not requiring parsing.
    Simpson's rule is most emphatically NOT a particular equation. It is an algorithm that takes a function as input -- hence the "I need to input a function" requirement -- and outputs an estimate of the integral of that function over a region. (The prototype would generally be something like
    Code:
    double simpson(double f(double), double start, double end, double tolerance);
    although if you're feeling lucky you can make those floats.)

    Now, when I assign this program, I make the students write some functions to integrate as sample (and of course I have mine that I use to test), so perhaps those auxiliary functions are all that is needed. But functions must be provided, one way or another.

  15. #15
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105
    I seriously want to know where people come up with this crap. REALLY.

    "I want to write a program to do X. Is it possible?"

    "I can't make the computer do Y. It must be impossible?"

    Yeah man. It's the computer that's limited. Has nothing to do with the fact that you don't know anything yet. We landed robots on the planet Mars, which is amazing considering that programs "involving mathematical equations" are so complex that they simply aren't possible.

    The Mars rovers shipped with basically a really good random number generator. Whenever the rover wants to do something it rolls the dice and prays (yes, we attached two mechanical hands to the back of it so that it can pray properly)
    Everyone is not a born genius and you are also not an exception to this, brewbuck.
    And I need not land robots on Mars now. If you dont wanna help please dont, but dont comment unnecessarily.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on Detecting Errors in Mathematical Equations
    By sangken in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 07:11 PM
  2. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:08 AM
  3. Manipulation and analysis of Mathematical Equations
    By WebmasterMattD in forum C++ Programming
    Replies: 13
    Last Post: 06-07-2003, 10:08 PM
  4. Mathematical equations
    By EvBladeRunnervE in forum Game Programming
    Replies: 12
    Last Post: 04-02-2003, 04:14 PM
  5. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 04:38 AM