|
-
May 1st, 2006, 06:44 AM
#1
Thread Starter
Addicted Member
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
-
May 1st, 2006, 02:55 PM
#2
Fanatic Member
Re: Simultaneous Equations
If one of the equations is linear, it seems to me substitution would work. What are the 2 equations?
-
May 2nd, 2006, 09:55 AM
#3
Thread Starter
Addicted Member
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
-
May 2nd, 2006, 10:12 AM
#4
Re: Simultaneous Equations
You can graph both solutions then look for where they intersect, but i don't recomend that
-
May 2nd, 2006, 10:16 AM
#5
Fanatic Member
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?
-
May 2nd, 2006, 11:03 AM
#6
Thread Starter
Addicted Member
Re: Simultaneous Equations
This is my function, works splendind, but is there a better way to do it?
VB Code:
Public Function chordCircleIntersection(chordPoint1 As POINT, chordPoint2 As POINT, radius As Long, center As POINT) As ROOT
Dim units2 As Long, m As Long, units As Long, Xco As Long, xSqrco As Long
m = (chordPoint2.Y - chordPoint1.Y) / (chordPoint2.X - chordPoint1.X)
units = -chordPoint1.X * m + (chordPoint1.Y)
xSqrco = (m ^ 2) + 1
Xco = (-chordPoint1.X * 2) + ((-chordPoint1.Y + units) * 2)
units2 = (chordPoint1.X ^ 2) + (((-chordPoint1.Y) + units) ^ 2) + (-radius ^ 2)
Dim discriminant As Long, root1 As Double, root2 As Double, minusB As Long
minusB = Xco * -1
discriminant = (Xco ^ 2) - (4 * xSqrco * units2)
If discriminant >= 0 Then
chordCircleIntersection.root1.X = (minusB + (discriminant ^ 0.5)) / (xSqrco * 2)
chordCircleIntersection.root2.X = (minusB - (discriminant ^ 0.5)) / (xSqrco * 2)
'caculate the Y coords
chordCircleIntersection.root1.Y = (chordCircleIntersection.root1.X * m) + units
chordCircleIntersection.root2.Y = (chordCircleIntersection.root2.X * m) + units
Else
MsgBox "The Chord Doesn't Pass through the circle" & vbNewLine & "chordCircleIntersection", vbCritical, "Organic Molecule"
End If
End Function
Rich
-
May 2nd, 2006, 02:57 PM
#7
Fanatic Member
Re: Simultaneous Equations
 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.
-
May 2nd, 2006, 03:04 PM
#8
Thread Starter
Addicted Member
Re: Simultaneous Equations
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|