# Creating a Four Bar Multibody Mechanism in MATLAB

This example constructs a four bar mechanism in MATLAB using Simscape Multibody. It demostrates various classes under simscape.multibody.* package to build a multibody system in MATLAB.

A custom function fourbar constructs the four-bar multibody with user specified link lengths. Each parameter is expected to be a Simscape Value, specifying a scalar length.

```openExample('sm/CreateAFourBarMechanismInMATLAB'); [fb, op] = fourBar(simscape.Value(12, 'cm'), simscape.Value(10, 'cm'), simscape.Value(5, 'cm'), simscape.Value(8, 'cm'));```

The four-bar is constructed so that the X-Y plane is the plane of motion, with gravity in the -Y direction. Output op is an operating point that sets the bottom left joint angle to a nominal value with high priority.

To visualize the mechanism, run the following :

```cmb = compile(fb); visualize(cmb,computeState(cmb,op),'vizFourBar');``` Once the four bar mechanism is created, we can also perform some analysis on it like computing and plotting the trajectory of the top (distal) link of the specified four-bar mechanism. The function fourBarMotionPlot plots the trajectory of the top link over a full 360 deg range of the crank angle (when the left link is a crank of the four bar). This function varies the crank angle over a full 360-deg range in steps of 1 deg. The trajectory of the midpoint of the top link is plotted over these 1-deg steps to form a (typically) smooth curve. The coordinates along this curve are relative to the world frame.

`fourBarMotionPlot(fb);`  