# Trouble with some optimization

I am attempting to use Mathematica to solve an optimization problem related to mechanism design (4-bar). The optimization problem is basically the following:

1) Solve these two simultaneous loop equations describing links in a mechanism as vectors:

Eq 1: $z(e^{i \alpha_2} - 1) + w(e^{i\beta_2} - 1) = \delta_2$

Eq 2: $z(e^{i \alpha_3} - 1) + w(e^{i\beta_3} - 1) = \delta_3$

In these equations what is know are both $\delta$'s, and both $\alpha$'s. Used mathematica to easily solve for $z$ and $w$ in terms of these parameters, so that the unknowns in the resultant equations are basically the $\beta$'s. $z$ and $w$ are obviously complex numbers, representing vectors representing links in a mechanism. The goal is to find both $\beta$'s such that the angle between these vectors will be minimized, ideally zero, as this will provide a high degree of mechanical advantage. I calculate this angle as

$\theta = cos^{-1}\left(\frac{w \cdot z}{|w||z|}\right)$

where $w$ and $z$ have been turned into pure vectors, like

$\text{vector form of z} = \left( \begin{array}{c} \text{Re}(z) \\ \text{Im}(z) \\ \end{array} \right)$

etc., and the dot indicates the dot product of vectors.

The mathematica code that I have up to this point is the following:

Subscript[[Delta], 2] = 9 E^(I*275 [Degree]);

Subscript[[Delta], 3] = 7.3 E^(I*-87 [Degree]);

Subscript[[Alpha], 2] = 30 [Degree];

Subscript[[Alpha], 3] = 0 [Degree];

Solve[{z (E^(I*Subscript[[Alpha], 2]) - 1) + w (E^(I Subscript[[Beta], 2]) - 1) == Subscript[[Delta], 2], z (E^(ISubscript[[Alpha], 3]) - 1) + w (E^(I Subscript[[Beta], 3]) - 1) == Subscript[[Delta], 3]}, {z, w}]

I then define variables z and w by setting them equal to the respective results of the solve operation. Then I go like

vecZ = ( { {FunctionExpand[Re[z]]}, {FunctionExpand[Im[z]]} } )

vecW = ( { {FunctionExpand[Re[w]]}, {FunctionExpand[Im[w]]} } )

AngleofInterest = Abs[ArcCos[Dot[vecZ, vecW]/(Norm[vecZ] Norm[vecW])]];

Minimize[{AngleofInterest, vecW[] < 0}, {Subscript[[Beta], 2], Subscript[[Beta], 3]}]

the constraint vecW[]<0 is just to do with the way we'd like the mechanism to look/operate.

When I try to run this code I wind up getting massive amounts of errors from the Dot operation telling me that the 'tensors' have 'incompatible shapes'. And also this error further down in the output:

Abs[ArcCos[0.0004018 {{51.0546},{9.37553}}.{{-47.9325},{1.14297}}]] is not a number at {Subscript[[Beta], 2],Subscript[[Beta], 3]} = {0.75721,0.152402}.

I will love you forever if you can educate me on this. Mechanism design is awful; it's so hard to get a design that works well so I have tried to formulate this as this optimization problem.. thanks!

I'd suggest setting it up as simply as possible, no subscripts or other possible sources of confusion.

e1 = (x1 + I*y1)*(Exp[I*Pi/6] - 1) + (x2 + I*y2)*(Exp[I*b2] - 1) -
9 E^(I*275*Degree);
e2 = (x1 + I*y1)*(Exp[I*0] - 1) + (x2 + I*y2)*(Exp[I*b3] - 1) -
73/10 E^(-I*87*Degree);
epolys = ComplexExpand[{Re[e1], Im[e1], Re[e2], Im[e2]}]
theta = ArcCos[(x1*x2 + y1*y2)/(Sqrt[x1^2 + y1^2]*Sqrt[x2^2 + y2^2])];

(* Out= {-x1 + (Sqrt x1)/2 - x2 - y1/2 + x2 Cos[b2] -
y2 Sin[b2] - 9 Sin[5 \[Degree]],
x1/2 - y1 + (Sqrt y1)/2 - y2 + y2 Cos[b2] + 9 Cos[5 \[Degree]] +
x2 Sin[b2], -x2 + x2 Cos[b3] - y2 Sin[b3] -
73/10 Sin[3 \[Degree]], -y2 + y2 Cos[b3] + 73/10 Cos[3 \[Degree]] +
x2 Sin[b3]} *)


Now do a numerical optimization.

{min, vals} =NMinimize[{theta, Thread[epolys == 0]},
{x1, y1, x2, y2, b2, b3}]

(* Out= {3.33200093731*10^-8, {x1 -> -1.70127972842,
y1 -> 2.24982907557, x2 -> -2.65849520069, y2 -> 3.51568304387,
b2 -> 2.27060692293, b3 -> 1.95142105721}} *)


Be aware that the angles are given in radian measure, not degrees.

Check that equations are satisfied to withing close tolerance:

In:= {e1, e2} /. vals

(* Out= {8.63301694576*10^-8 -
6.0190735951*10^-8 I, -2.16175702894*10^-8 + 3.78699474446*10^-7 I} *)

• Infinite thank yous, sir! This is extremely helpful. – Ern Mar 9 '16 at 20:38