# Wolfram 语言™

## 优化凸轮的形状

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

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

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

In[4]:=
```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]];```

In[5]:=
```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[0] -> rmin, r[n + 1] -> rmax};```

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

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

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

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

In[10]:=
`Table[r[i], {i, 95, 100}] /. sol[[2]]`
Out[10]=

In[11]:=
```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[0] -> rmin, r[n + 1] -> rmax, r[n + 2] -> r[n]} /. sol[[2]]; solpts2 = Map[{#[[1]], -#[[2]]} &, 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[11]=