+ Reply to Thread
Results 1 to 11 of 11

Solving equation - using different variables

  1. #1
    Registered User
    Join Date
    09-16-2015
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    1

    Solving equation - using different variables

    Hey guys,

    I am trying to automatically solve an equation, which allow me to enter any of the variables and have the equation automatically solve itself. As an example,

    A + B = C

    What I want to be able to do is to work out a, b, c according to the given values. IE.

    - If given A and B, I will enter these to have C automatically generated
    - If given B and C, I will enter these values to have A automatically generated
    - If given A and C, I will enter these values to have B automatically generated

    I am planning to apply this concept to far more difficult equations to solve automatically. It would be great if someone could give me a bit of guidance on this one.

    Kind Regards.

  2. #2
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    846

    Re: Solving equation - using different variables

    Hi, finding the values - what equations are in use?

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Solving equation - using different variables

    I think the easiest way to do this looks something like this:

    1) Decide on a "toggle" input that will tell the spreadsheet which quantity is the unkown. Assuming "leave the cell blank" is the toggle used, I would set it up something like this:
    Please Login or Register  to view this content.
    I have obviously assumed that the user will be smart enough not to leave 2 cells blank, or not to use empty string "" or 0 to indicate blank.

    I find that this is the easiest approach, because it separates inputs and outputs to different cells, since a spreadsheet cannot store both a formula and an input in the same cell. It often seems that users who post this kind of question want (demand) the entire thing (input and output) to occur in the same range of cells. If this is the case for your question, you should know that having both input and output in the same range will require VBA (probably in change and or calculate events) to perform the same task (analyze the input for the "solve for this variable" toggle, perform the computations, and output the result to the range). If you are already skilled at VBA programming and using the change/calculate events, this should be straightforward.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Contributor
    Join Date
    08-07-2015
    Location
    AB, Canada
    MS-Off Ver
    Excel 2013 x64
    Posts
    132

    Re: Solving equation - using different variables

    I put together a set of macros that should solve most problems, as long as their results are convergent (if C = 12, but when C = 10 you're farther away from a solution than if C = 7 it is divergent and this solution won't be reliable)

    Just put both sides of the equation into the cells E2 and G2 :

    ex: A + 2B = C would be input as:

    Please Login or Register  to view this content.
    Notice the variables are surrounded by underscores. They have to be entered that way simply because that's the way I named them. Go to the name manager if you'd like them to have a different name.

    For those who are interested in the code without wanting to dl a file:

    NOT THE MOST RECENT VERSION
    Please Login or Register  to view this content.
    Hope that helps! (PS. I know there are really well built equation solvers online... you might have more luck there...)
    Attached Files Attached Files
    Last edited by ThirdFret; 09-16-2015 at 12:57 PM.

  5. #5
    Forum Contributor
    Join Date
    08-07-2015
    Location
    AB, Canada
    MS-Off Ver
    Excel 2013 x64
    Posts
    132

    Re: Solving equation - using different variables

    I changed the macro a little bit so it won't keep finding additional decimals if it finds a perfect solution:

    NOT THE MOST RECENT VERSION
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by ThirdFret; 09-16-2015 at 12:57 PM.

  6. #6
    Forum Contributor
    Join Date
    08-07-2015
    Location
    AB, Canada
    MS-Off Ver
    Excel 2013 x64
    Posts
    132

    Re: Solving equation - using different variables

    Sorry, one more edit. I did some more testing. This version seems to work best.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by ThirdFret; 09-16-2015 at 01:30 PM.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Solving equation - using different variables

    I use MrShorty's suggestion -- e.g., for cost, price, & margin.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Solving equation - using different variables

    Because a numerical algorithm might be needed with your real equations, since you indicate they are much more complicated than the simple linear function you use to represent them, a comment on ThirdFret's proposed solution. It appears that he is using a basic "brute force" kind of root finding algorithm.

    When I decide that I need a numerical rather than an algebraic solution to a problem like this, I first look to the built in Goal Seek/Solver utilities that are built into Excel. These utilities use a Newton Raphson type algorithm to find roots of equations. If you set up the spreadsheet correctly (something like Thirdfret has done where you are looking for the place where A+B-C=0), these utilities should be able to find the root(s) of your equations fairly readily. If you are unfamiliar with NR type algorithms, I would suggest this tutorial http://www.cs.utah.edu/~zachary/isp/...ot/Newton.html to understand how the algorithm works, when it has difficulty and when it fails.

    If you decide that you need to program your own root finding algorithm like ThirdFret did, I would suggest a bisection or Newton Raphson type algorithm rather than a brute force algorithm.

  9. #9
    Forum Contributor
    Join Date
    08-07-2015
    Location
    AB, Canada
    MS-Off Ver
    Excel 2013 x64
    Posts
    132

    Re: Solving equation - using different variables

    Quote Originally Posted by MrShorty View Post
    I would suggest a bisection or Newton Raphson type algorithm rather than a brute force algorithm.
    Would you mind elaborating a little bit? I only chose this brute force method because I couldn't figure out how to get the solver and goalseek tools to work correctly when solving for a variable that appeared on both sides of the equation. (ex. A + B = B*C; A and C are known; solve for B). Is there a way to use either of these tools when both sides change when you change the input?

    I'm interested in learning more about sorting and other algorithms. Do you have any resources that explain some simple cases?

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Solving equation - using different variables

    Properly formulated, there should be no special consideration for a variable on "both" sides of the equal sign. This is because in your "objective function" (the actual function that you have Solver/Goal Seek look at), all variables should be on one side of the equation. For your example, the OF is rearranged to be OF=A+B-B*C (Using the template of your sample sheet, I would probably add a formula =E2-G2). Then, you instruct Solver/goal seek to "Set target cell" containing the OF, "to a value of 0" "by changing" the cell containing B.

  11. #11
    Forum Contributor
    Join Date
    08-07-2015
    Location
    AB, Canada
    MS-Off Ver
    Excel 2013 x64
    Posts
    132

    Re: Solving equation - using different variables

    edit: I Tried your solution of simply using a third cell to equal one side of the equation subtracted from the other and used the solver to change one variable with a target of zero in that 'difference' cell. That works really well. Thanks for drawing attention to it.
    Last edited by ThirdFret; 09-17-2015 at 08:48 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. solving a 3rd degree equation
    By Mimouscha in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-11-2013, 07:23 AM
  2. Problem with solving LN equation
    By Hrki87 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 12-01-2012, 07:10 AM
  3. Solving an equation with eight variables
    By grauniadangel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-06-2012, 12:17 PM
  4. VBA Equation Solving Problem
    By encherahim in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-23-2012, 06:18 PM
  5. Solving non-linear equation
    By Lagrene in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-16-2011, 06:56 PM
  6. Solving an equation
    By sydmil in forum Excel General
    Replies: 6
    Last Post: 05-13-2009, 12:29 AM
  7. Replies: 6
    Last Post: 12-09-2007, 11:09 AM
  8. Please help me solving this equation ...: )
    By Joe Satriani in forum Excel General
    Replies: 3
    Last Post: 02-22-2006, 01:50 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1