Find maximum angle of box in slot - math

How would I find the maximum possible angle (a) which a rectangle of width (W) can be at within a slot of width (w) and depth (h) - see my crude drawing below

Considering w = hh + WW at the picture:
we can write equation
h * tan(a) + W / cos(a) = w
Then, using formulas for half-angles and t = tan(a/2) substitution
h * 2 * t / (1 - t^2) + W * (1 + t^2) / (1 - t^2) = w
h * 2 * t + W * (1 + t^2) = (1 - t^2) * w
t^2 * (W + w) + t * (2*h) + (W - w) = 0
We have quadratic equation, solve it for unknown t, then get critical angle as
a = 2 * atan(t)
Quick check: Python example for picture above gives correct angle value 18.3 degrees
import math
h = 2
W = 4.12
w = 5
t = (math.sqrt(h*h-W*W+w*w) - h) / (W + w)
a = math.degrees(2 * math.atan(t))
print(a)

Just to elaborate on the above answer as it is not necessarly obvious, this is why why you can write equation:
h * tan(a) + W / cos(a) = w
PS: I suppose that the justification for "why a is the maximum angle" is obvious

Related

Find the position of a point on a circle so it has a given distance to a point on another circle

I have a point on a half circle that needs a line connecting it to the black half circle. The line goes through the origin of the orange circle (perpendicular). When moving along the upper circle, the length of the line changes. Is there a way to calculate a position for the arrow, so the green line has a length of a given value? None of the circles are necessarily at the origin.
No need to check if the green line does intersect the black circle, I already made sure that's the case.
For the length s of the line from the orange centre to the black circle you get the formula:
s^2 = (x + r * cos(a))^2 + (y + r * sin(a))^2
where x is the absolute value of the x-component of the centre of the black circle and y the corresponding y-component. r is the radius of the black circle. a is the angle of the intersection point on the black circle (normally there will be two solutions).
Expanding the given formula leads to:
s^2 = x * x + r * r * cos(a)^2 + 2* r * x * cos(a)
+ y * y + r * r * sin(a)^2 +2 *r * y * sin(a)
As
r * r * cos(a)^2 + r * r * sin(a)^2 = r * r
we have
s^2 - x^2 - y^2 - r^2 = 2 *r * (x * cos(a) + y * sin(a)) (1)
Dividing by 2*r and renaming the left side of the equation p (p contains known values only) results in
p = x * cos(a) + y * sin(a) = SQRT(x * x + y * y) * sin(a + atan(x / y))
==>
a = asin(p /SQRT(x*x + y*y)) + atan(x / y) (2)
Let's have an example with approximate values taken from your drawing:
x = 5
y = -8
r = 4
s = 12
Then (1) will be
144 = 25 + 16 + 64 + 8 * (5 * cos(a) - 8 * sin(a)) ==>
39 / 8 = 5 * cos(a) - 8 * sin(a) =
SQRT(25 + 64) * sin(a + atan(5 / -8)) ==>
0.5167 = sin(a + atan(5 / -8))
asin(0.5167) = a - 212°
asin(0.5167) has two values, the first one is 31.11°, the second one is 148.89°. This leads to the two solutions for a:
a1 = 243.11°
a2 = 360.89° or taking this value modulo 360° ==> 0.89°
I just found a much simpler solution using the Law of cosines:
c * c = a * a + b * b - 2ab * cos(gamma)
You got a triangle defined by three points: the centre points of both circles and the point of intersection on the black circle. The lengths of all three sides are known.
So we get:
cos(gamma) = (a * a + b * b - c * c) / 2ab
If we choose the angle at centre of orange circle to be gamma we get
a = sqrt(89) = 9.434 (distance between the centres of both circles)
b = 12 (distance between centre of orange circle and point if intersection
c = 4 (radius of black circle)
Using this values we get:
cos(gamma) = (89 + 144 - 16) / (2 * sqrt(89) * 12) = 0.9584
gamma = acos(0.9584) = +/- 16.581°
´

plotting Hamid Naderi Yeganehs parrot using ggplot

I am a) new to stackoverflow and b) an advanced beginner to R ;-)
i saw some bird artworks of Yeganeh with the associated functions in the web Drawing Birds in Flight With Mathematics and wanted to reproduce them in R to experiment a bit with colouring and so on.
However, while this one yielded a quite good result:
k <- 1:9830
X <- function(k) {
sin(pi * k / 20000) ^ 12 *
(0.5 * cos(31 * pi * k / 10000) ^ 16 *
sin(6 * pi * k / 10000) + (1 / 6 * sin(31 * pi * k / 10000)) ^ 20) +
3 * k / 20000 + cos(31 * pi * k / 10000) ^ 6 *
sin((pi / 2) * ((k - 10000) / 10000) ^ 7 - pi / 5)
}
Y <- function(k) {
-9 / 4 * cos(31 * pi * k / 10000) ^ 6 *
cos(pi / 2 * ((k - 10000) / 10000) ^ 7 - pi / 5) *
(2 / 3 + (sin(pi * k / 20000) * sin(3 * pi * k / 20000)) ^ 6) +
3 / 4 * cos(3 * pi * ((k - 10000) / 100000)) ^ 10 *
cos(9 * pi * ((k - 10000) / 100000)) ^ 10 *
cos(36 * pi * ((k - 10000) / 100000)) ^ 14 +
7 / 10 * ((k - 10000) / 10000) ^ 2
}
R <- function(k) {
sin(pi * k / 20000) ^ 10 *
(1 / 4 * cos(31 * pi * k / 10000 + 25 * pi / 32) ^ 20 +
1 / 20 * cos(31 * pi * k / 10000) ^ 2) +
1 / 30 * (3 / 2 - cos(62 * pi * k / 10000) ^ 2)
}
bird <- data.frame(x = X(k), y = Y(k), r = R(k))
library(tidyverse)
library(ggforce)
q <- ggplot() +
geom_circle(aes(x0 = x, y0 = y, r = r),
data = bird,
n = 30) +
coord_fixed() +
theme_void()
the following code yielded some weird result which should basically be related to the difference in the function. (x-A(k))+(y-B(k))=(R(k)) for the parrot below, whlie the bird above "simply" consisted of the k-th circle (X(k), Y(k)) and the radius of the k-th circle R(k)
k <- -10000:10000
A <- function(k) {
(3*k/20000)+(cos(37*pi*k/10000))*sin((k/10000)*(3*pi/5))+(9/7)*(cos(37*pi*k/10000))*(cos(pi*k/20000))*sin(pi*k/10000)
}
B <- function(k) {
(-5/4)*(cos(37*pi*k/10000))*cos((k/10000)*(3*pi/5))*(1+3*(cos(pi*k/20000)*cos(3*pi*k/20000)))+(2/3)*(cos(3*pi*k/200000)*cos(9*pi*k/200000)*cos(9*pi*k/100000))
}
R <- function(k) {
(1/32)+(1/15)*(sin(37*pi*k/10000))*((sin(pi*k/10000))+(3/2)*(cos(pi*k/20000)))
}
parrot <- data.frame(a = A(k), b = B(k), r = R(k))
q <- ggplot() +
geom_circle(aes(x0 = a, y0 = b, r = r),
data = parrot,
n=30) +
coord_fixed() +
theme_void()
q
Any help would be very much appreciated. Cartesian coords already applied as [explained here] (https://www.wikiwand.com/en/Hamid_Naderi_Yeganeh). From the visual point of view, it seems like the function is plotted properly but the "view" on it needs to be changed...
Thanks in advance!

How to algebraically solve a system of equations?

I am not sure whether this is right place to ask. Here N, L, H, p, and d are parameters. I need to solve this system of equations. Specifically, I need to solve for b(t) and e(t).
Variables | t=1 | t>1
----------|--------|------------------------
n(t) | N |N(1-p)^(t-1)
s(t) | 1 |((1-p+dp)/(1-p))^(t-1)
b(t) | L |b(t-1)+p(H-b(t-1))
e(t) |(H-L)/2 |e(t-1)+(p(H-b(t-1)))/2
c(t) |(1-d)pN |(1-d)pN(1-p+dp)^(t-1)
Please help me how should I start this problem to solve.
Since you used a Wolfram-Mathematica tag, perhaps you intend to use Mathematica
RSolve[{b[1]==L, b[t]==b[t-1]+p(H-b[t-1]),
e[1]==(H-L)/2, e[t]==e[t-1]+p(H-b[t-1])/2}, {b[t],e[t]}, t]//FullSimplify
which returns
Solve::svars: Equations may not give solutions for all "solve" variables
{b[t]->H+(-H+L)(1-p)^(-1+t),
e[t]->((H-L)(-2+(1-p)^t+2 p))/(2(-1+p))}
It seems that these formulas give recurrent equations - you find values for t = 1 (from table), then calculate values for t = 2, then for t = 3 and so on
b(t) = b(t-1) + p * (H - b(t-1))
t = 1: L
t = 2: b(2) = b(1) + p * (H - b(1)) or
L + p * (H - L) = L + p * H - p * L
t = 3: b(3) = b(2) + p * (H - b(2))
Example: L= 2; p = 3; H = 7;
b(1) = 2
b(2) = 2 + 3 * (7 - 2) = 17
b(3) = 17 + 3 * (7 - 17) = -13

Mutual Information in a Binary Erasure Channel

Imagine a Binary Erasure Channel as depicted on Wikipedia.
One equation describing the mutual information is following:
I(x;y)
= H(x) - H(x|y)
= H(x) - p(y=0) • 0 - p(y=?) • H(x) -p(y=1) • 0
Why is it "p(y=?) • H(x)" and not "p(y=?) • H(x|y=?)"?
It can be proved using Bayes' theorem.
The channel:
x y
1-f
0 --------> 0
\
\ f
+------> ?
/
/ 1-f
1---------> 1
Let input distribution be P(x) = {p(x=0)=g; p(x=1)=1-g}
Then:
p(x=0/y=?) = p(y=?/x=0) * p(x=0) / p(y=?)
p(x=0/y=?) = (f * g) / (f * g - f * (1 - g)) = g;
p(x=1/y=?) = p(y=?/x=1) * p(x=1) / p(y=?)
p(x=1/y=?) = (f * (1 - g)) / (f * g - f * (1 - g)) = 1 - g;
As result:
p(x=0/y=?) = p(x=0)
p(x=1/y=?) = p(x=1)
From the defenitions of entropy and conditional entropy:
H(X) = p(x=0) * log(1 / p(x=0)) + p(x=1) * log(1 / p(x=1))
H(X/y=?) = p(x=0/y=?) * log(1 / p(x=0/y=?)) + p(x=1/y=?) * log(1 / p(x=1/y=?))
So:
H(X) = H(X/y=?)

Line/Plane intersection based on points

I have two points in space, L1 and L2 that defines two points on a line.
I have three points in space, P1, P2 and P3 that 3 points on a plane.
So given these inputs, at what point does the line intersect the plane?
Fx. the plane equation A*x+B*y+C*z+D=0 is:
A = p1.Y * (p2.Z - p3.Z) + p2.Y * (p3.Z - p1.Z) + p3.Y * (p1.Z - p2.Z)
B = p1.Z * (p2.X - p3.X) + p2.Z * (p3.X - p1.X) + p3.Z * (p1.X - p2.X)
C = p1.X * (p2.Y - p3.Y) + p2.X * (p3.Y - p1.Y) + p3.X * (p1.Y - p2.Y)
D = -(p1.X * (p2.Y * p3.Z - p3.Y * p2.Z) + p2.X * (p3.Y * p1.Z - p1.Y * p3.Z) + p3.X * (p1.Y * p2.Z - p2.Y * p1.Z))
But what about the rest?
The simplest (and very generalizable) way to solve this is to say that
L1 + x*(L2 - L1) = (P1 + y*(P2 - P1)) + (P1 + z*(P3 - P1))
which gives you 3 equations in 3 variables. Solve for x, y and z, and then substitute back into either of the original equations to get your answer. This can be generalized to do complex things like find the point that is the intersection of two planes in 4 dimensions.
For an alternate approach, the cross product N of (P2-P1) and (P3-P1) is a vector that is at right angles to the plane. This means that the plane can be defined as the set of points P such that the dot product of P and N is the dot product of P1 and N. Solving for x such that (L1 + x*(L2 - L1)) dot N is this constant gives you one equation in one variable that is easy to solve. If you're going to be intersecting a lot of lines with this plane, this approach is definitely worthwhile.
Written out explicitly this gives:
N = cross(P2-P1, P3 - P1)
Answer = L1 + (dot(N, P1 - L1) / dot(N, L2 - L1)) * (L2 - L1)
where
cross([x, y, z], [u, v, w]) = x*u + y*w + z*u - x*w - y*u - z*v
dot([x, y, z], [u, v, w]) = x*u + y*v + z*w
Note that that cross product trick only works in 3 dimensions, and only for your specific problem of a plane and a line.
This is how I ended up doing it in come code. Luckily one code library (XNA) had half of what I needed, and the rest was easy.
var lv = L2-L1;
var ray = new Microsoft.Xna.Framework.Ray(L1,lv);
var plane = new Microsoft.Xna.Framework.Plane(P1, P2, P3);
var t = ray.Intersects(plane); //Distance along line from L1
///Result:
var x = L1.X + t * lv.X;
var y = L1.Y + t * lv.Y;
var z = L1.Z + t * lv.Z;
Of course I would prefer having just the simple equations that takes place under the covers of XNA.

Resources