There are two approaches to solving the structure of a 4 bar linkage closed chain, the first is to do a direct solution. You can set any of the angles in the closed chain to be an independent variable. Depending on the remaining two unknown variables you can fix either the location of joint 3, or the distance between between two adjacent links. A solution (if it exists) is then possible using the cosine rule.
The second method is to use a numerical method and let the computer do all the hard work. If you consider figure~/ref{fig:almost4bar} it represents a 4 link open chain where $\theta_1 = 0$ and link $l_1$ will become the ground link of a 4-bar mechanism. As you can see it is almost a 4-bar linkage, all that is needed is to move the end of link 4 a short distance $d$ so it coincides with joint 1 at the origin.
Since the forward kinematics can then be calculated as above all is needed it a way to adjust the joint angles until $d$ in the figure is zero.
There are a number of root (zero) finding algorithms but we can use the Nelder-Mead algorithm that is available in the Matlab as the fminsearch function.
The easiest way to compute the angles for a four bar linkage is to start with an open chain and allow fminsearch to determine the value of one of the angles.
The forward kinematics of 4 links can be done with the function
>> fk=@(len,theta) len*[cosd(cumsum(theta)) sind(cumsum(theta))];
Where $len$ is a vector of 4 link lengths and $theta$ is a vector of four angles.
Having set the theta1 to 0, we need to fix one other angle before calculating the solution for the remaining two. So the structure of the theta vector we will pass to the Nelder-Mead algorithm will be one of the following
\begin{align*} \vec{\theta} &= \begin{bmatrix} 0 & \theta_{2} & U_1 & U_2 \end{bmatrix}\text{ or}\\ &= \begin{bmatrix} 0 & U_1& \theta_{3} & U_2 \end{bmatrix}\text{ or}\\ &= \begin{bmatrix} 0 & U_1&U_2 & \theta_{4} \end{bmatrix}\numberthis \label{eqn:args} \end{align*}where $U_1$ and $U_2$ are the unknown values we are seeking
If we now choose the values for one other joints we can ask $fminsearch$ to calculate the best angle for a second joint. The final joint angle can then be calculated since we know the total of the joints must add up to 360 degrees.
We can do this with another function that calls $fk$. This could be done as a Matlab .m file or with another anonymous function. This function should return the distance between the final joint and the origin.
>> len=[2 .5 2 1.5]; % or what ever your link lengths are.
Set your independent angle theta1, theta2 or theta3 then use the appropriate case from the following definitions, c.f. the equation t.b.d. (eqn:args) above
>> disttooriginsq=@(U) sum(fk(len,[0; theta1; U(:)]).^2)
>> disttooriginsq=@(U) sum(fk(len,[0; U(1);theta2; U(2)]).^2)
>> disttooriginsq=@(U) sum(fk(len,[0; U(:);theta3]).^2)
Unpacking the disttooriginsq function: If $\vec{x}_5$ is the location of the final point on the last link then we need the distance between that point and the origin $d$ (or reduce calculation we will actually use $d^2$). This is simply done as the dot product so that $d^2=\vec{x}_5\cdot\vec{x}_5$. However rather than calculating the distance squared from the vector transpose form of the dot product ($d2=\vec{x}_5^T \vec{x}$) we simply square the elements of $\vec{x}_5$ and add. This is done with the Matlab \texttt{sum} function and the \verb|.^2| form of square that squares the elements of fk (i.e. $\vec{x_5}$ and sums the result. (This will mean that we must make sure that elements of U are in a column vector).
Now calling $disttoorigin$ with any angle (in degrees) will return the distance squared between the last link and the origin.
For a four-bar linkage to work this distance must be zero, use fminsearch to see how close it can get to zero.
>> startfrom=[20;180];
This is where you want to start your search from, You may need to play with this
>> theta_answer = fminsearch(disttoorigin,startfrom);
Once you have your answer you can put the values for U and theta (1,2, or 3) into the $fk$ and the result should be near to zero. If this is the case, this will give the configuration of the four bar linkage.
W.S. Harwin -- 9 February 2025