I have a pool of ten different gas mixtures. Each gas mixture is made of nitrogen, oxygen and carbon dioxide in different percentages.
The goal is to mix and match sets of gasses to to get a requested percentage output.
Lets say we want 33-33-33 out. Whats the best methodology to select the best subset of gases and mixing proportions to get an optimal output?
Just solve 3 equations with 3 unknowns.
if mixture 1 has a1 fraction of chemical a (and similarly with other chemicals/mixtures), and you need xa : xb : xc mixture, where xa+xb+xc=1, this will be:
a1 * x1 + a2 * x2 + a3 * x3 = xa
b1 * x1 + b2 * x2 + b3 * x3 = xb
c1 * x1 + c2 * x2 + c3 * x3 = xc
Solve for x1, x2, x3. If you get negative numbers (or no solutions), this means that it's impossible to get the wanted mixture.
Related
A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2. The output values of Y1 and Y2 are obtained as follows:
If A<B, then Y1 and Y2 will be equal to A-B. Else Y1 and Y2 will be equal to A.
How To Determinate truth table for this
There are two inputs A[0:1] and B[0:1], 4 inputs in total. Your truth table will have 16 inputs(rows).Are Y1 and Y2 2-bit outputs and is it magnitude of A-B? If yes, The left two columns can be A and next two will be B. For 6 of these rows from 16 cases, A<B in 6 cases ([A,B] = {[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]}). these six rows will have Y1 = Y2 = [B-A]. All other rows will have Y1 = Y1 = A input. Seems straightforward, but I may be missing something here.
Is there any algorithm to solve a system of equations expressed in different modulo spaces?
For exemple, consider this system of equations:
(x1 + x2 ) % 2 = 0
( x2 + x3) % 2 = 0
(x1 + x2 + x3) % 3 = 2
One of the solutions of this system is:
x1 = 0
x2 = 2
x3 = 0
How could I arithmetically find this solution (without using a brute force algorithm)?
Thanks
You can rewrite these equations as
x1 + x2 = 2*n1
x2 + x3 = 2*n2
x1 + x2 + x3 = 3*n3 + 2
Now, this is a linear Diophantine equation problem for which there are solutions in the literature.
Example: http://www.wikihow.com/Solve-a-Linear-Diophantine-Equation
Also see: https://www.math.uwaterloo.ca/~wgilbert/Research/GilbertPathria.pdf
Algorithm:
Write xi as a function of nks
In this case:
x3 = 3*n3 + 2 - 2*n1
x2 = 2*n2 - (3*n3 + 2 - 2*n1)
x1 = 2*n1 - (2*n2 - (3*n3 + 2 - 2*n1))
Since there is no division on the right-hand side, pick any (n1, n2, n3) and you should get a solution.
First line is same as saying x1, x2 is all even or all odd numbers.
Second line is same as saying x2, x3 is all even or all odd numbers.
Hence x1,x2,x3 is all even or all odd numbers.
From third line we can replace the question to "3 odd or 3 even numbers that accumulate to 3k+2."
You can convert your system to modulo LCM (least common multiple). Just find the LCM of all equation's modulo, and multiply each equation appropriately.
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)
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
I'm trying to set up a linear program in which the objective function adds extra weight to the max out of the decision variables multiplied by their respective coefficients.
With this in mind, is there a way to use min or max operators within the objective function of a linear program?
Example:
Minimize
(c1 * x1) + (c2 * x2) + (c3 * x3) + (c4 * max(c1*x1, c2*x2, c3*x3))
subject to
#some arbitrary integer constraints:
x1 >= ...
x1 + 2*x2 <= ...
x3 >= ...
x1 + x3 == ...
Note that (c4 * max(c1*x1, c2*x2, c3*x3)) is the "extra weight" term that I'm concerned about. We let c4 denote the "extra weight" coefficient. Also, note that x1, x2, and x3 are integers in this particular example.
I think the above might be outside the scope of what linear programming offers. However, perhaps there's a way to hack/reformat this into a valid linear program?
If this problem is completely out of the scope of linear programming, perhaps someone can recommend an optimization paradigm that is more suitable to this type of problem? (Anything that allows me to avoid manually enumerating and checking all possible solutions would be helpful.)
Add in an auxiliary variable, say x4, with constraints:
x4 >= c1*x1
x4 >= c2*x2
x4 >= c3*x3
Objective += c4*x4