I'm struggling to figure out an algorithm to find the intersection of two linear equations like:
f(x)=2x+4
g(x)=x+2
I'd like to use the method where you set f (x)=g (x) and solve x, and I'd like to stay away from cross product.
Does anyone have any suggestion to how an algorithm like that would look like?
If your input lines are in slope-intercept form, an algorithm is an over-kill as there is a direct formula to calculate their point of intersection. It's given on a Wikipedia page and you can understand it as explained below.
Given the equations of the lines: The x and y coordinates of the
point of intersection of two non-vertical lines can easily be found
using the following substitutions and rearrangements.
Suppose that two lines have the equations y = ax + c and y = bx + d where a
and b are the slopes (gradients) of the lines and where c and d are
the y-intercepts of the lines. At the point where the two lines
intersect (if they do), both y coordinates will be the same, hence the
following equality:
ax + c = bx + d.
We can rearrange this expression in order to extract the
value of x,
ax - bx = d - c, and so,
x = (d-c)/(a-b).
To find the y coordinate, all we need to do is substitute the value of x into > either one of the two line equations. For example, into the first:
y=(a*(d-c)/(a-b))+c.
Hence, the Point of Intersection is {(d-c)/(a-b), (a*(d-c)/(a-b))+c}
Note: If a = b then the two lines are parallel. If c ≠ d as well, the lines
are different and there is no intersection, otherwise the two lines are
identical.
Given:
ax + b = cx + d
ax = cx + d - b
ax - cx = d - b
x(a - c) = d - b
Therefore, x = (d - b) / (a - c)
In your example, let a = 2, b = 4, c = 1 d = 2
x = (2 - 4) / (2 - 1)
x = -2 / 1
x = -2
General solution. Let
f(x) = a1x + b1 ....... g(x) = a2x + b2
Special cases:
a1 == a2 and b1 == b2 : lines coincide
a1 == a2 and b1 != b2 : lines are parallel, no intersection
General case: a1 != a2
X = (b2 - b1) / (a1 - a2) ....and... Y = (a1b2 - a2b1) / (a1 - a2)
I don't remember what cross products are in the context of equations.
One way to solve these is to set them equal to each other, solve for x, then use that value to solve for y:
2x + 4 = x + 2
2x + 2 = x
x = -2
y = f(x)
= g(x)
= x + 2
= -2 + 2
= 0
Solution: (-2, 0)
Related
I have an equation (parentheses are used because of VBA code)
Y=(P/(12E((bt^3)/12))*A
and i know every variables but not "b". Is there any way how to ask Wolfram Alpha to "redefine" (not solve) equation so I can see something like following: I tried to do it manually (but result is not OK)
b=((P/EY)*12A))/t^3
I wish to see how right equation will look.
Original equation is on picture below
where
equation in [,] I simplified by A
I'm not sure if there's a way to tell Wolfram|Alpha to rearrange for a particular variable; in general it will usually try to rearrange for x or y.
If I substitute b for x in your equation and use the following query:
solve Y - (P/(12E((xt^3)/12))*A) = 0
then Wolfram Alpha returns the result you're looking for: x (b) expressed in terms of the other variables. Specifically:
x = A P / (E t^3 Y) for tY != 0 and AP != 0
I know that your question was about Wolfram Alpha, that you do not want to "solve", but here is one way you could do it in Mathematica using your real question. I renamed I into J because I is a reserved symbol in Mathematica for the imaginary unit.
J = b t^3/12;
expr = (P / (12 E J) ) (4 L1^3 + 3 R ( 2 Pi L1^2 + Pi R^2 + 8 L1 R ) + 12 L2 (L1 + R)^2)
Solve[ Y == expr , b]
Result
{{b -> (P (4 L1^3 + 12 L1^2 L2 + 24 L1 L2 R + 6 L1^2 \[Pi] R + 24 L1 R^2 + 12 L2 R^2 + 3 \[Pi] R^3))/(E t^3 Y)}}
In Maxima, I want to change the following equation:
ax+b-c-d=0
into the following format
(ax+b)/(c+d)=1
Note:
something like ax+b-c-d+1=1 is not what I want.
Basically I want to have positive elements in one side and negative elements in another side, then divide the positive elements by the negative elements.
Here is a quick attempt. It handles some equations of the form you described, but it's probably easy to find some which it can't handle. Maybe it works well enough, or at least provides some inspiration.
ptermp (e) := symbolp(e) or (numberp(e) and e > 0)
or ((op(e) = "+" or op(e) = "*") and every (ptermp, args(e)));
matchdeclare (pterm, ptermp);
matchdeclare (otherterm, all);
defrule (r1, pterm + otherterm = 0, ratsimp (pterm/(-otherterm)) = 1);
NOTE: the catch-all otherterm must be precede pterm alphabetically! This is a useful, but obscure, consequence of the simplification of "+" expressions and the pattern-matching process ... sorry for the obscurity.
Examples:
apply1 (a*x - b - c + d = 0, r1);
a x + d
------- = 1
c + b
apply1 (a*x - (b + g) - 2*c + d*e*f = 0, r1);
a x + d e f
----------- = 1
g + 2 c + b
Some time ago I asked a question on math.stackexchange and got an answer. I have difficulties deriving an algorithm from that answer because my background is in design and hope some of you can help me.
The original question with visual sketch and possible answer are here:
https://math.stackexchange.com/questions/667432/triangle-with-two-constraints-each-corner-on-a-given-line
The question was: Given 3 3-dimensional lines (a, b and c) that coincide in a common point S and a given Point B on b, I'm looking for a point A on a and a point C on c where AB and BC have the same length and the angle ABC is 90 degrees.
I will have to implement this algorithm in an imperative language, any code in C++, Java, imperative pseudo-code or similar is fine.
Also, different approaches to this problem are equally welcome. Plus: Thanks for any hints, if the complete solution is indeed too time-consuming!
The two key formulas are
(I've replied the derivation for the formulas in the mathematics stack exchange site)
Substituting the first in the second gives in the end a 4th degree equation that is quite annoying to solve with a closed form. I've therefore used instead a trivial numerical solver in Python:
# function to solve (we look for t such that f(t)=0)
def f(t):
s = (t*cB - B2) / (t*ac - aB)
return s*s - 2*s*aB - t*t + 2*t*cB
# given f and an interval to search generates all solutions in the range
def solutions(f, x0, x1, n=100, eps=1E-10):
X = [x0 + i*(x1 - x0)/(n - 1) for i in xrange(n)]
Y = map(f, X)
for i in xrange(n-1):
if (Y[i]<0 and Y[i+1]>=0 or Y[i+1]<0 and Y[i]>=0):
xa, xb = X[i], X[i+1]
ya, yb = Y[i], Y[i+1]
if (xb - xa) < eps:
# Linear interpolation
# 0 = ya + (x - xa)*(yb - ya)/(xb - xa)
yield xa - ya * (xb - xa) / (yb - ya)
else:
for x in solutions(f, xa, xb, n, eps):
yield x
The search algorithm samples the function in the interval and when it finds two adjacent samples that are crossing the f=0 line repeats the search recursively between those two samples (unless the interval size is below a specified limit, approximating the function with a line and computing the crossing point in that case).
I've tested the algorithm generating random problems and solving them with
from random import random as rnd
for test in xrange(1000):
a = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
b = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
c = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
L = rnd() * 100
B = tuple(x*L for x in b)
aB = dot(a, B)
cB = dot(c, B)
B2 = dot(B, B)
ac = dot(a, c)
sols = list(solutions(f, -1000., 1000.))
And there are cases in which the solutions are 0, 1, 2, 3 or 4. For example the problem
a = (-0.5900900304960981, 0.4717596600172049, 0.6551614908475357)
b = (-0.9831451620384042, -0.10306322574446096, 0.15100848274062748)
c = (-0.6250439408232388, 0.49902426033920616, -0.6002456660677057)
B = (-33.62793897729328, -3.5252208930692497, 5.165162011403056)
has four distinct solutions:
s = 57.3895941365 , t = -16.6969433689
A = (-33.865027354189415, 27.07409541837935, 37.59945205363035)
C = (10.436323283003153, -8.332179814593692, 10.022267893763457)
|A - B| = 44.5910029061
|C - B| = 44.5910029061
(A - B)·(C - B) = 1.70530256582e-13
s = 43.619078237 , t = 32.9673082734
A = (-25.739183207076163, 20.5777215193455, 28.577540327140607)
C = (-20.606016281518986, 16.45148662649085, -19.78848391300571)
|A - B| = 34.5155582156
|C - B| = 34.5155582156
(A - B)·(C - B) = 1.13686837722e-13
s = -47.5886624358 , t = 83.8222109697
A = (28.08159526800866, -22.450411211385674, -31.17825902887765)
C = (-52.39256507303229, 41.82931682916268, -50.313918854788845)
|A - B| = 74.0747844969
|C - B| = 74.0747844969
(A - B)·(C - B) = 4.54747350886e-13
s = 142.883074325 , t = 136.634726869
A = (-84.31387768560096, 67.4064705656035, 93.61148799140805)
C = (-85.40270813540043, 68.1840435123674, -82.01440263735996)
|A - B| = 124.189861967
|C - B| = 124.189861967
(A - B)·(C - B) = -9.09494701773e-13
Write two quadratic equations for lambda, mu unknowns (just above matrix forms).
Solve this system with paper, pen and head, or with any mathematical software like Maple, Mathematica, Matlab, Derive etc. You will have 4th order equation. It has closed-form solution - apply Ferrari or Kardano method and get real roots, find mu, lambda, then point coordinates.
I have a vector X of 20 real numbers and a vector Y of 20 real numbers.
I want to model them as
y = ax^2+bx + c
How to find the value of 'a' , 'b' and 'c'
and best fit quadratic equation.
Given Values
X = (x1,x2,...,x20)
Y = (y1,y2,...,y20)
i need a formula or procedure to find following values
a = ???
b = ???
c = ???
Thanks in advance.
Everything #Bartoss said is right, +1. I figured I just add a practical implementation here, without QR decomposition. You want to evaluate the values of a,b,c such that the distance between measured and fitted data is minimal. You can pick as measure
sum(ax^2+bx + c -y)^2)
where the sum is over the elements of vectors x,y.
Then, a minimum implies that the derivative of the quantity with respect to each of a,b,c is zero:
d (sum(ax^2+bx + c -y)^2) /da =0
d (sum(ax^2+bx + c -y)^2) /db =0
d (sum(ax^2+bx + c -y)^2) /dc =0
these equations are
2(sum(ax^2+bx + c -y)*x^2)=0
2(sum(ax^2+bx + c -y)*x) =0
2(sum(ax^2+bx + c -y)) =0
Dividing by 2, the above can be rewritten as
a*sum(x^4) +b*sum(x^3) + c*sum(x^2) =sum(y*x^2)
a*sum(x^3) +b*sum(x^2) + c*sum(x) =sum(y*x)
a*sum(x^2) +b*sum(x) + c*N =sum(y)
where N=20 in your case. A simple code in python showing how to do so follows.
from numpy import random, array
from scipy.linalg import solve
import matplotlib.pylab as plt
a, b, c = 6., 3., 4.
N = 20
x = random.rand((N))
y = a * x ** 2 + b * x + c
y += random.rand((20)) #add a bit of noise to make things more realistic
x4 = (x ** 4).sum()
x3 = (x ** 3).sum()
x2 = (x ** 2).sum()
M = array([[x4, x3, x2], [x3, x2, x.sum()], [x2, x.sum(), N]])
K = array([(y * x ** 2).sum(), (y * x).sum(), y.sum()])
A, B, C = solve(M, K)
print 'exact values ', a, b, c
print 'calculated values', A, B, C
fig, ax = plt.subplots()
ax.plot(x, y, 'b.', label='data')
ax.plot(x, A * x ** 2 + B * x + C, 'r.', label='estimate')
ax.legend()
plt.show()
A much faster way to implement solution is to use a nonlinear least squares algorithm. This will be faster to write, but not faster to run. Using the one provided by scipy,
from scipy.optimize import leastsq
def f(arg):
a,b,c=arg
return a*x**2+b*x+c-y
(A,B,C),_=leastsq(f,[1,1,1])#you must provide a first guess to start with in this case.
That is a linear least squares problem. I think the easiest method which gives accurate results is QR decomposition using Householder reflections. It is not something to be explained in a stackoverflow answer, but I hope you will find all that is needed with this links.
If you never heard about these before and don't know how it connects with you problem:
A = [[x1^2, x1, 1]; [x2^2, x2, 1]; ...]
Y = [y1; y2; ...]
Now you want to find v = [a; b; c] such that A*v is as close as possible to Y, which is exactly what least squares problem is all about.
For a collision algorithm I am developing, I need to find out how to reflect a line over another.
Line 1:
y=ax+b
Line 2:
y=cx+d
Line 3:
(a result of reflecting line 1 over line 2) y=ex+f
Is there any algebraic way to determine e and f in terms of a, b, c, and d?
I have run over this exact same problem before. Stay with me here...
This problem involves two parts:
1. Find the point at which they intersect
to find where two lines intersect, we use the two equations of the lines:
y = M1x + B1
y = M2x + B2
Using substitution:
M1x + B1 = M2x + B2
M1x - M2x = B2 - B1
x(M1 - M2) = B2 - B1
x = (B2 - B1) / (M1 - M2)
To find the y value, just plug it in:
y = M1x + B1
2. Find the slope of the line from the other two slopes.
The second is far trickier. Using trigonometry, it is not impossible.
Let L1 be the "base line." (With a slope of M1)
Let L2 be the line that is to be reflected over the "base line." (With a slope of M2)
Let L3 be our resulting line. (With a slope of M3)
The equation I used is as follows:
double M3 = ((2 * M1) + (M2 * pow(M1, 2)) - M2) / (2 * M1 * M2 - pow(M1, 2) + 1);
Straight from my C code.
It is important to note that both slopes should be defined. You can use L'Hopital's rule to get an equation when one of the slopes is approaching infinity.
ONWARD WITH THE EXPLANATION!
Here is a crude drawing of three lines.
L2 is reflected over L1, resulting in L3. Drawing is not exact.
The angle between L1 and L2, as well as L2 and L3, is labelled as R.\
Here are the facts:
M1 = tan(A1)
M2 = tan(A2)
M3 = tan(A3)
This comes from the definition of tangent.
A3 = R + A1
This is a little trickier to see, but if you draw a horizontal line at the point of intersection it becomes obvious.
Thus, our goal is to find tan(A3). To accomplish this, we need to find R. As we can see, R can be found in a triangle with A2 and the supplement of A1 as the other angles. Thus, we know:
R + (180 - A1) + A2 = 180
R - A1 + A2 = 0
R = A1 - A2
Let's take the tangent of both sides:
tan(R) = tan(A1 - A2)
From trigonometry, we know:
tan(R) = (tan(A1) - tan(A2)) / (1 + tan(A1)tan(A2))
R = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2))
Arctan being inverse tangent. From our earlier formula, A3 = R + A1, we get:
A3 = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2)) + A1
A3 = arctan((M1 - M2) / (1 + M1*M2)) + A1
But we don't want A3. We want tan(A3). So again, we take the tangent of both sides.
tan(A3) = M3 = tan(arctan((M1 - M2) / (1 + M1*M2)) + A1)
M3 = tan(arctan((M1 - M2) / (1 + M1*M2))) + tan(A1) / (1 - tan(arctan((M1 - M2) / (1 + M1*M2))) * tan(A1))
Unfortunately, that's disgustingly hideous. Replacing tangents with slopes and simplifying, we get
M3 = ((M1 - M2) / (1 + M1*M2)) + M1 / (1 - ((M1 - M2)/(1 + M1*M2)) * M1)
M3 = (M1 - M2 + M1*(1 + M1*M2)) / (1 + M1*M2 - M1*M1 + M1*M2)
M3 = (M1^2 * M2 + 2*M1 - M2) / (1 + 2*M1*M2 - M1^2)
Which is the exact same as the formula above. Sorry about all the ugly math. When M2 is completely vertical, you can use L'Hopital's rule to get
M3 = (M1^2 - 1) / 2*M1
If anyone is so inclined, check my math. But I'm tired right about now.
Assuming the two lines are not parallel to each other
Step 1:
First find the intersection of the line y = ax + b with line y = cx + d , that is by solving comes out to be
m = (d - b) / (c - a)
Step 2:
The final line has point of the form (x , ex + f) , so wjat we know is the line joining the point and the corresponding image is perpendicular to the mirror line AND the the midpoint of the first point and its image lies on the mirror line. Solving for first requirement ....
(Slope of line joining point and its image) * (Slope of the mirror line) = -1
we get ...
c * (e*pt + f - a*n - b)/( pt - n ) = -1 -----> The first equation .
Then the midpoint of the point and its image lie on the central line , i.e.
Y coordinate of the midpoint - ( c* x coordinate of midpoint + d) = 0
y coordinate of midpoint = (a*n + e*pt) / 2 and x coordinate = ( pt + n) / 2
putting it above we get...
(a*n + e*pt)c - c( pt + n) - 2d =0 ----> second equation
3.
now the point and its image make equal angles from the intersection point .... a simple way of saying that angle between mirror line , point line and image line , point line being equal ... therefore ... the tangent of angle between lines mI and mM is equal to that of mM and mP
equating we get
( mM + mP ) / ( 1 + mp*mM) =( mI - mM )/ (1 + mI*mM)
where mM = c , mI = e, and mP = a -----> third equation
put it in their respective
places you get three equations in three unknowns , pt , e and f and solve ... just x in place of n earlier and there you have your e , f in terms of a , b , c , d.
Solve it yourselves ....
However if they are parallel its simple , you have two equations in two variables use the midpoint method
Yet another method:
Matrix of affine transformation for reflection relatively to line y=ax+b (works for non-vertical lines!).
Let's pa = 1+a^2, ma = 1-a^2, then matrix is (from Nikulin's Computer Geometry book)
ma/pa 2a/pa 0
2a/pa -ma/pa 0
-2ab/pa 2b/pa 1
So we can get two arbitrary points at second line, apply this transform, and calculate new line equation