# Wolfram Mathematica

## Optimize the Shape of a Cam

Design the shape of a convex cam to maximize the area of the valve opening for one rotation of the cam, with constraints on the radii of the cam.

Assume that the shape of the cam is circular over an angle of of its circumference with radius and is shaped symmetrically over the remaining . The design variables , , , , , represent the radius of the cam at equally spaced angles distributed over an angle of , the top half of the non-circular part of the cam.

In:= `n = 100; vars = Array[r, n];`

Each radius is constrained to lie in the interval .

In:= ```rmin = 1; rmax = 2; varbounds = Table[rmin <= r[i] <= rmax, {i, 1, n}];```

Enter the angle defined above.

In:= `\[Theta] = 2 Pi/(5 (n + 1));`

Convexity constraints are expressed by the system of inequalities .

In:= ```convexconstri = (1/2) r[i - 1] r[i + 1] Sin[2 \[Theta]] <= (1/2) r[i - 1] r[i] Sin[\[Theta]] + (1/2) r[i] r[ i + 1] Sin[\[Theta]];```

Using that , the system can be re-expressed as follows. Notice that is the radius of a point on the circular part of the cam and hence equal to .

In:= ```convexconstr = Table[2 r[i - 1] r[i + 1] Cos[\[Theta]] <= r[i] (r[i - 1] + r[i + 1]), {i, 0, n}] /. {r[-1] -> rmin, r -> rmin, r[n + 1] -> rmax};```

A constraint on the rate of change of the radius is expressed in terms of the parameter as .

In:= ```\[Alpha] = 1.5; rchangeconstr = Table[-\[Alpha] <= (r[i + 1] - r[i])/\[Theta] <= \[Alpha], {i, 0, n}] /. {r -> rmin, r[n + 1] -> rmax};```

The objective function (valve opening area), is assumed to have a simple linear relationship with the design variables given as , where is a constant related to the geometry of the valve.

In:= ```rv = 1; objfun = Pi rv^2 (1/n) Sum[r[i], {i, 1, n}];```

Choose initial points for the variables.

In:= `initpts = Table[.5 (rmin + rmax), {i, 1, n}];`

Solve the maximization problem.

In:= ```sol = FindMaximum[ Join[{objfun}, varbounds, convexconstr, rchangeconstr], Thread[vars, initpts]];```

Note that several of the largest radii are at the maximum .

In:= `Table[r[i], {i, 95, 100}] /. sol[]`
Out= Plot the solution.

show complete Wolfram Language input
In:= ```solpts1 = Table[{r[i] Cos[-2. Pi/5 + \[Theta] i], r[i] Sin[-2. Pi/5. + \[Theta] i]}, {i, -1, n + 2}] /. {r[-1] -> rmin, r -> rmin, r[n + 1] -> rmax, r[n + 2] -> r[n]} /. sol[]; solpts2 = Map[{#[], -#[]} &, Reverse@solpts1]; solpts = Join[solpts1, solpts2]; Show[ListLinePlot[solpts, PlotRange -> {{-2.1, 2.1}, {-2.1, 2.1}}, PlotLabel -> "Cam Shape", AspectRatio -> 1, Axes -> False, Frame -> True], Graphics[{Circle[{0., 0.}, 1.]}], ImageSize -> Medium]```
Out= 