Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Algebraic Equation

Status
Not open for further replies.

Kckronic21

Programmer
Jul 29, 2001
81
US
Hi, I want to know what is the best wat to set up a query for calculating simple algebraic equations like the this one for example: 3X + 15 = 45. Thanks!
 
Are you saying you want Access to solve for 'X'?

Holy cow! I suppose you could set this up, but I don't think you'll do it in a query. Are you saying that you'd like to actually enter the algebraic equation in a single field and have Access parse the expression and solve for the variable? I don't think I've ever heard of a request like that before. This will require some serious consideration.
 
What I am saying is if I had a field for each value of the equation, could I then solve for X?
 
so you mean you have the variables in a table already? or you want to type them into a form one at a time whenever you need the calc done? either way, you'd have to figure out how the equation would be solved by a human first:

X = (Field3 - Field2)/Field1

if you already have these fields and all the values in a table your expression in a query would look like:

Code:
x:(Field3 - Field2)/Field1

if you have a form where you'd enter the values as you need the calc done:put four unbound text boxes called for example Field1, Field2, Field3, Field4 where you'd type in values each time you want to calculate this in the first three fields, then in Field4 the Control Source would be
Code:
=(Field3 - Field2)/Field1

you'd wanna make your labels indicate which of the numbers you're entering tho so you can keep it all straight....
 
In your case, 3X+5=45, to solve for X the equation would be:
X = (45-5)/3
X = 40/3
X = 13.33333333.....

I guess I was looking beyond just this equation and was thinking along the line of allowing the user to enter ANY algebraic expression they wanted and having Access solve for it. If every expression is identical except for the constants, then you could solve for it easily using GingerR's formulae. Is this what you're after?
 
Thanks! You have been very helpful. I just have one more question to ask. I have 4 fields in a Table. Field1(X) Field2 Field3 = Field4. I want Field2 to be the operation like +,-,*,or /. This way I could solve more equations like 3X + 15 = 45 and also 3X - 15 = 45. Is this possible to set this up in a query as well?
 
Hmmmmmmmmmm,

Doesn't SEEM very hard. Long away and far ago, there was a "school work" problem to build an "RPN" notation calculator. I ALMOST went out to retrieve it from the dusty Archives (Floppy Disc?) - but then I recalled the great strides in arcania since those days in WoeBegoneBasic. Hence, the following "offerings". Even though I mostly agree w/ Jerry. I DID force a way to use a query, but after actually 'doing it', thte whole afair is to trivial to really be worth the effort:


As may be seen in this sample, you could go abi 'round the barn JUST to enter the information - but the actual "Evaluation" is trivial. Further, almost any "Algebraic" calculation you want to make could be handled in this manner - as long as the "function" was properly set into a proper format - as Ginger noted.

Code:
Public Function basStacMath(ParamArray FctList() As Variant) As Double

    Dim Idx As Long
    Dim MyStr As String

    'SAMPLE Usage:
    '? basStacMath("(", "45", "+", "15", ")", "/", "3")
    '20

    Idx = 0
    Do While Idx <= UBound(FctArray())
    
        MyStr = MyStr & FctArray(Idx) & &quot; &quot;
        Idx = Idx + 1
    Loop

    basStacMath = Eval(FctList)

End Function


This example is just done to 'force' the use of data (as in a table) and a query. After tinking this one through a bit, changing the table design (Normalizing ?) appears to make the whole process easier - for the user. In practice, (if one were to make this a PRACTICE), I would probably have a single text box where the User entered the Right side of the equation, which would be parsed to the individual 'tokens' - each placed in a record. For any pratical use, the concept would need to include 'Variables' (e.g. = M * X + B, where &quot;X&quot; is (somehow denoted) as a 'variable' and &quot;M&quot; and &quot;B&quot; are denoted as 'Constants'. My thought would be to add LoRange, HiRange and Incr Fields to the table and adopt the convention that 'tokens' with only a LoRange are constants, while those w/ both are variables. the process would then need to set up Loop(s) for each of the variables and evaluate the espr foreach set of variables/incrments.

Code:
Public Function basStackEval(ProbId As Long) As Double

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset

    Dim strSql As String
    Dim Quo As String
    Dim MyStr As Variant

    'Sample Usage:
    '? basStackEval(1)
    ' 10

    'Assumes:tblStackOper ~ as Below.
    'StackOper   PrbStep Problem
    '(              1       1
    '45             2       1
    '-              3       1
    '15             4       1
    ')              5       1
    '/              6       1
    '3              7       1

    Quo = Chr(34)

    strSql = &quot;Select StackOper, PrbStep From tblStackOper &quot;
    strSql = strSql & &quot;Where Problem = &quot; & ProbId & &quot; &quot;
    strSql = strSql & &quot;Order By PrbStep &quot; & &quot;;&quot;

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset(strSql, dbOpenDynaset)

    Do While Not rst.EOF
        MyStr = MyStr & rst!StackOper & &quot; &quot;
        rst.MoveNext
    Loop

    basStackEval = Eval(MyStr)

End Function

So, the 'original' issue appears to be 'do-able' in a number of ways, while the broader issue of WHY anyone would want to go this far 'round the barn in persuit of this 'original' problem remains a mystery (after all, you could easily just &quot;plug&quot; the properly formatted expression directl into Eval). Going beyond the stated problem would need SOME more 'study', but could also 'be done' - if one actually had a use for it.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
do something like this:
make a field called Operand.

for your X put in

=IIf([Operand]=&quot;*&quot;,([Field3]/[Field2])/[Field1],IIf([Operand]=&quot;+&quot;,([Field3]-[Field2])/[Field1],IIf([Operand]=&quot;/&quot;,([Field3]*[Field2])/[Field1],([Field3]+[Field2])/[Field1])))

tweek it as you need it.....

g
 
Oh, well, Still no real understanding of WHY Kckronic21 (or anyone) would want to go very far into this arcania(sp?), but it does serve as a puzzle to solve as a demo of some Functions, so I did one more implementation, just to illustrate that Eval can be coerced into returning a discrete set ov values of a function. My example is limited to &quot;functions&quot; of a single variable, mostly because I do not want to take the time to think my way through the process of setting up a process to seperate the coefficients of the seperate variables and the multiple nesting of loops to use the various combinations of coefficients and step sizes. It SEEMs to me that this would be a several day process, which I simply do not want to do as an academic execrise. Anyway, here is a function which returns the values of y = F(X) for a (Specified) range of &quot;X&quot; with a specified interval (or step). the second function is provided simply to test and illustrate the first.

Code:
Public Function basEvalMulti(Fct As String, _
                             MinX As Single, _
                             MaxX As Single, _
                             StepIncr As Single) As Variant

    'Sample Usage:
    'm * (X) + b = 2 * X + 5
    'Where: 3.21 <= X <= 7.67, Incr @ 0.25

    Dim MyX As Single
    Dim MyVals() As Variant
    Dim MyFct As String
    Dim strMyX As String
    Dim FctParts As Variant

    ReDim MyVals(((MaxX - MinX) / StepIncr) + 1)

    FctParts = basSplit(Fct, &quot;X&quot;)

    Idx = 0
    MyX = MinX
    Do While MyX <= MaxX
        strMyX = CStr(MyX)
        MyFct = Join(FctParts, strMyX)
        'MyFct = Replace(Fct, &quot;X&quot;, strMyX)
        MyVals(Idx) = Eval(MyFct)
        MyX = MyX + StepIncr
        Idx = Idx + 1
    Loop

    basEvalMulti = MyVals

End Function
Public Function TestMultiRepl()

    RtnVal = basEvalMulti(&quot;2 * X + 5&quot;, 3.21, 7.67, 0.25)

    Idx = 0
    While Idx <= UBound(RtnVal)
        Debug.Print Idx, RtnVal(Idx)
        Idx = Idx + 1
    Wend

End Function
[code] MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Still more variations on the theme:

Code:
    'Declaration
    Type MyFctType
        MyFct As String
        MinX As Single
        MaxX As Single
        StepIncr As Single
    End Type
    Dim MyFct(10) As MyFctType
[code]
'__________________________________________________
[code]
     'Function
Public Function TestMultiEvalLoop()

    MyFct(0).MyFct = &quot;2 * X + 5&quot;
    MyFct(0).MinX = 3.21
    MyFct(0).MaxX = 7.67
    MyFct(0).StepIncr = 0.25

    MyFct(1).MyFct = &quot;2 * X * X + 5 * X + 3&quot;
    MyFct(1).MinX = 1.23
    MyFct(1).MaxX = 8.88
    MyFct(1).StepIncr = 0.22

    Jdx = 0
    Do While Jdx < UBound(MyFct)

        If (MyFct(Jdx).MyFct <> &quot;&quot;) Then

            RtnVal = basEvalMulti(MyFct(Jdx).MyFct, MyFct(Jdx).MinX, MyFct(Jdx).MaxX, MyFct(Jdx).StepIncr)

            Debug.Print &quot;Evaluating &quot; & MyFct(Jdx).MyFct
            Idx = 0
            While Idx <= UBound(RtnVal)
                Debug.Print Idx, RtnVal(Idx)
                Idx = Idx + 1
            Wend
            Debug.Print
        End If
        Jdx = Jdx + 1
    Loop

End Function

Still seems like a neat TOY, but no production application which I can think of.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top