a trig problem solved in MATLAB
- 2 minutes read - 286 wordsI came across this post. The basic idea is the guy wants to maximize $L_1+L_2$ constrained to this box, where $L_i$ is the length of beam $i$. It’s constrained to be a 61 cmx61 cm box, but one beam must start from 10cm up from the bottom right corner and the beams must meet at a point along the top of the box. I added the further assumption that the other beam must end in the bottom left corner.
In this case, based on the pictures, I came up with 4 equations: ( \begin{split} T_1 =& L_1\sin\theta_1\ 51 =& L_2\cos\theta_1\ T_2 =& L_2\sin\theta_2\ 61 =& L_2\cos\theta_2 \end{split} ) which fall from simple trig. There’s one more equation, which constrains the side length to 61 cm: ( T_1 + T_2 = 61 )
Next, I squared each pair of equations to get ( \begin{split} T_1^2 =& L_1^2\sin^2\theta_1\ 51^2 =& L_1^2\cos^2\theta_1 \end{split}\implies L_1^2 = T_1^2 + 51^2 ) and similarly $L_2^2=61^2+T_2^2$.
I'm planning on using MATLAB's FMINCON, which means I need to formulate this as a minimization problem. This is accomplished by observing ( \max f(x) \iff \min -f(x). )
Therefore, the final nonlinear program that I want to solve is ( \begin{split} \min \qquad & -L_1 - L_2\ \text{subject to} \qquad & L_1^2 -T_1^2 - 51^2 = 0\ & L_2^2 -T_2^2 - 61^2 = 0\ & T_1 + T_2 - 61 = 0 \end{split} ) which can be solved with the matlab program
function xsol = solveproblem()
f = @(x) -x(1)-x(2);
x0 = [0;0;0;0]; LB=[0;0;0;0];
settings = optimset('TolFun',1e-8,'Algorithm','interior-point');
xsol = fmincon(f,x0,[],[],[],[],LB,[],@nonlincon,settings);
function [c,ceq]=nonlincon(x)
l1=x(1);l2=x(2);t1=x(3);t2=x(4);
c=[];
ceq = [l1^2-t1^2-51^2;
l2^2-t2^2-61^2;
t1 + t2 - 61];
end
end
When run, this produces a length 140.5 pair of beams. Hooray!