VBA How to solve two equations with two unknowns
I am trying to calculate point on a line. I got the points of the edges and one distance between one edge to the point I want to find (which is B).
A(2,4)
B(x,y) C(4,32)The distance between A to B is 5.
How can I 开发者_如何学JAVAcalculate Bx and By? using the following equations:
d = Math.Sqr((Bx-Ax)^2 + (By-Ay)^2)
d = Math.Sqr((Cx-Bx)^2 + (Cy-By)^2)
and than compare the equations above.
Here is the equations with the points placed:
5 = Math.Sqr((Bx-2)^2 + (By-4)^2)
23.0713366 = Math.Sqr((4-Bx)^2 + (32-By)^2)
or
Math.Sqr((Bx-2)^2 + (By-4)^2) - 5 = Math.Sqr((4-Bx)^2 + (32-By)^2) - 23.0713377
How can I solve this using VBA?
Thank you!I won't solve your equations above because they are an unnecessarily complex way to state the problem (and the existence of a solution is questionable in the presence of rounding), but all the points on the line A=(Ax,Ay)
to C=(Cx,Cy)
can be described as B=(Ax,Ay) + t*(Cx-Ax,Cy-Ay)
with t
between 0
and 1
.
The distance between B
and A
is then given by d=t*Sqrt((Cx-Ax)^2+(Cy-Ay)^2)
, which you can invert to get the proper t
for a given d
- t=d/Sqrt((Cx-Ax)^2+(Cy-Ay)^2)
In your case, B(t) = (2,4) + t*(2,28)
, t=5/Sqrt(2^2+28^2) ~ 0.178
-> B ~ (2,4) + 0.178 * (2,28) ~ (2.356, 8.987)
.
VBA has no Symbolic Language capability. To solve this problem, there are different approach :
- Transform the equations to isolate one of the unknowns, most likely to use substitution, and compute it (I recommend this for your problem.)
- Transform your functions and derive them to use Newton's methods (don't do this, it's overkill.)
- Use a "brute force" convergence methods : Fix a min/max for each variable and use bisection methods to find what you want (I don't recommend this because you'll most likely "fall" into a local minimum/maximum in your case.)
So basically, I'd say you go with the first way. It requires 15mins of tinkering with mathematical equations, then you're set to go.
精彩评论