Results 1 to 8 of 8

Thread: Simultaneous Equations

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Simultaneous Equations

    Whats the best way to to solve two equations simultaneouly? The method i use at the minute is to store each of the coeficients separatly. Then work on them. One equation is linear the other isn't.

    Rich

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Simultaneous Equations

    If one of the equations is linear, it seems to me substitution would work. What are the 2 equations?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Simultaneous Equations

    y=X+1 and (X-3)^2+(Y-4)^2 = 4

    Yeah substitution works fine but the code sucks, i was wondering if there was a better way than substitution.

    Rich

  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Simultaneous Equations

    You can graph both solutions then look for where they intersect, but i don't recomend that

  5. #5
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Simultaneous Equations

    Hmmmm, it's not clear what you are trying to do. No code is needed. This can readily be solved with algebra. The 2nd equation is a circle and the first is a line, so there are either no solutions (i.e. the line doesn't touch or cross the circle), one solution (i.e. line is tangent to the circle at one point), or 2 solutions (line crosses the circle at 2 points). In this case, there are 2 solutions. Using substitution, you get (x-3)^2 = 2, which yields x = 3 + sqrt(2) and x = 3 - sqrt(2).

    What are you trying to do in code? Maybe you can post it?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Simultaneous Equations

    This is my function, works splendind, but is there a better way to do it?

    VB Code:
    1. Public Function chordCircleIntersection(chordPoint1 As POINT, chordPoint2 As POINT, radius As Long, center As POINT) As ROOT
    2.  
    3.    
    4.     Dim units2 As Long, m As Long, units As Long, Xco As Long, xSqrco As Long
    5.    
    6.         m = (chordPoint2.Y - chordPoint1.Y) / (chordPoint2.X - chordPoint1.X)
    7.    
    8.    
    9.    
    10.     units = -chordPoint1.X * m + (chordPoint1.Y)
    11.     xSqrco = (m ^ 2) + 1
    12.     Xco = (-chordPoint1.X * 2) + ((-chordPoint1.Y + units) * 2)
    13.     units2 = (chordPoint1.X ^ 2) + (((-chordPoint1.Y) + units) ^ 2) + (-radius ^ 2)
    14.  
    15.  
    16.     Dim discriminant As Long, root1 As Double, root2 As Double, minusB As Long
    17.  
    18.     minusB = Xco * -1
    19.     discriminant = (Xco ^ 2) - (4 * xSqrco * units2)
    20.  
    21.     If discriminant >= 0 Then
    22.         chordCircleIntersection.root1.X = (minusB + (discriminant ^ 0.5)) / (xSqrco * 2)
    23.         chordCircleIntersection.root2.X = (minusB - (discriminant ^ 0.5)) / (xSqrco * 2)
    24.         'caculate the Y coords
    25.        
    26.         chordCircleIntersection.root1.Y = (chordCircleIntersection.root1.X * m) + units
    27.         chordCircleIntersection.root2.Y = (chordCircleIntersection.root2.X * m) + units
    28.     Else
    29.         MsgBox "The Chord Doesn't Pass through the circle" & vbNewLine & "chordCircleIntersection", vbCritical, "Organic Molecule"
    30.     End If
    31.  
    32.  
    33.  
    34. End Function

    Rich

  7. #7
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Simultaneous Equations

    Quote Originally Posted by Rich2189
    Yeah substitution works fine but the code sucks, i was wondering if there was a better way than substitution
    What sucks about it? Your function is a generalized one to find the intersection of a line and a circle. From the standpoint of the mathematical approach, I think what you have is about as simple as it gets. Quite frankly, I can't think of any other way to do it, except maybe some sort of iterative technique.

    If you are looking for a critique of coding style/elegance/efficiency (e.g. using UDT's as return values, etc.), other (more experienced coders) would need to reply.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Simultaneous Equations

    Quote Originally Posted by VBAhack
    What sucks about it? Your function is a generalized one to find the intersection of a line and a circle. From the standpoint of the mathematical approach, I think what you have is about as simple as it gets. Quite frankly, I can't think of any other way to do it, except maybe some sort of iterative technique.

    If you are looking for a critique of coding style/elegance/efficiency (e.g. using UDT's as return values, etc.), other (more experienced coders) would need to reply.
    Thanks that was what i was looking for, i just supposed their must be a better way to find those points Aparently not. . the thing that sucks is simulating the algebra, boggles the mind and very hard to debug.

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