Results 1 to 9 of 9

Thread: [Serious] Programatically solve equation for x

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    [Serious] Programatically solve equation for x

    I am trying to make an application that can solve or isolate an equation. I'm not really sure which language I should proceed with, and I am looking for ideas and suggestions.

    By solving, I mean: Let's say we have an equation A=B*C.. and we want to find C.. and we do that by dividing B on both sides. And this is basically the task I want to perform, only the amount of variables and operators will vary, since the equation will be given to the application by the user in form of a string.

    I have only tried using PHP for this task so far, but I am stuck, and I haven't found any library or api or guides that I can use without having to pay more than I can afford for it. And as it seems, it's so tricky, there are so many things to consider... like: The rules for solving the equation, the number of variables in the equation, the priority of the operators while solving the variables and so on...

    So basically, what I am asking, is: what would be the most preferred language for this task, and why?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [Serious] Programatically solve equation for x

    English.

    Frankly, I'd say that you should use whichever language you are most comfortable with, because the language is going to be the LEAST of your problems, so you might as well make it not a problem at all. I don't believe that any mainstream language will make this job any easier than any other mainstream language. The problem itself is one that I looked at and decided against tackling. What I was doing was coming up with a set of models (equations) relating various variables to predict some known pattern. The models were generated via evolution using a genetic algoorithm, so there was no guarantee what they would look like. For example, I could easily have an equation that looked like this: ? = 2x + 3y + 4x. Obviously, this can be reduced, so what I wanted to do was to reduce the equations and organize them such that the similarities/differences between two equations shown side by side would be more apparent to the viewer. I quickly realized that, once you get beyond the trivial example like the one given, the problem becomes pretty nearly undefinable. A good solution is only in the eye of the beholder, so I abandoned the attempt.

    What you are trying to do is not much less involved. Maybe no less at all. So, I wouldn't be worrying too much about which language makes it slightly easier.
    My usual boring signature: Nothing

  3. #3
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [Serious] Programatically solve equation for x

    BTW - The baffle-buzzphrase "genetic algorithm" can be unpacked into plain language giving "throwing things at the wall randomly until something seems to stick."

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [Serious] Programatically solve equation for x

    No, it most certainly can not. The search space, in this instance, was infinite, though I've also written some where the search space was bounded (though it was 40^100, so it was pretty big). A random search of such a space would take somewhere longer than the expected lifetime of the universe even using every computer in existence today. If you feel like taking that long, go right ahead and try a random approach.
    My usual boring signature: Nothing

  5. #5
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: [Serious] Programatically solve equation for x

    x = 42

  6. #6
    Lively Member
    Join Date
    Mar 2015
    Posts
    104
    Last edited by CreativeDreamer; Mar 7th, 2015 at 12:18 PM. Reason: Additional info

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: [Serious] Programatically solve equation for x

    Quote Originally Posted by dilettante View Post
    BTW - The baffle-buzzphrase "genetic algorithm" can be unpacked into plain language giving "throwing things at the wall randomly until something seems to stick."
    Yep I thought that brute forcing would be easier than explaining the whole logic in solving the equations, and well, it wasn't easiest, because there are too many possible combinations, and the possibilities increase exponentially for each symbol in the equation, will require too many resources and with the path I am following, it has to be light..

    I ended up continuing with php because this will allow people to use it without having to download anything, and basically has the most options. I found some physics equations so I had something to experiment with, and I am trying to explain the math rules for isolation,. It is actually more difficult than I initially expected... but I learned where to start at and how to keep going from there, I am writing the whole thing from scratch, because it is a hobby and I want the thing to be mine. (just to satisfy my ego)

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: [Serious] Programatically solve equation for x

    Take a look at my codebank contribution here. I haven't expanded it to include variables because I wanted it to be really all about the basics, but including variables would not be difficult at all.
    I realized that the thing I have been working on for the past two weeks, is my own expression evaluator, and since Monday, I have been trying to isolate the formula, after I got the expression evaluator set up. I have successfully managed to implement some of the basic rules while isolating a formula.

    I noticed that you are using potents are percentages in your evaluator, but I decided to stick to the four most basic operators (* / + -) and implement the other operators and functions like sinus and cosines later, since that makes sense to me. Because, If we take the formula for kinetic energy (E=1/2*m*v^2) you would basically isolate v^2 before getting the root of it. Also, I am happy to share that I successfully isolated each variable on the mentioned formula, using what I have programmed so far.

    I have already managed to get Excel integrating and differentiating, so I am looking forward to getting that far with my equation solver/isolator.
    Last edited by dday9; May 27th, 2026 at 09:44 AM.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: [Serious] Programatically solve equation for x

    I never ran into that problem with the parentheses, however I am going to test unary operators some time this week. I split the formula symbols, and operators in relation to the symbols into different arrays, and the parentheses too. This allowed me to have more control over basically everything. The way I describe the rules may even be similar to the reverse polish, so I guess I am doing something right.

Posting Permissions

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



Click Here to Expand Forum to Full Width