# How to plot torsion prismatic beam

I dont know why dont work but I want to animate torsion prismatic beam.

a = 1; b = 2; L = 10; G = 1; dx = dy = dz = 0.25; J = 2.1849801331564294;
Clear[x, y, z, T]; k = T/( G J);
phi = (32 G k a)/Pi^3 Sum[1/n^3 (-1)^((n - 1)/2) (1 - Cosh[n Pi y/(2 a)]/
Cosh[n Pi b/(2a)]) Cos[(n Pi x)/(2 a)], {n, 1, 10, 2}];
tauyz = -D[phi, x]; tauxz = D[phi, y];
gammayz = tauyz/G;
gammaxz = tauxz/G;
Rotate[{x_, y_, z_}, Theta_] := {x Cos[Theta] - y Sin[Theta],
x Sin[Theta] + y Cos[Theta], z};
Off[Graphics3D::"gprim"];
Do[Show[Graphics3D[Table[alpha = 0.5 k z;
p1 = p2;
p2 = Map[({x, y} = #;
Rotate[{x + 0 gammaxz/2,
y + 0 gammayz/2,
z + (x gammaxz + y gammayz)/
2}, alpha]) &,
Flatten[{Table[{x, -b/2}, {x, -a/2, a/2 - dx, dx}],
Table[{a/2, y}, {y, -b/2, b/2 - dy, dy}],
Table[{x, b/2}, {x, a/2, dx - a/2, -dx}],
Table[{-a/2, y}, {y, b/2, dy - b/2, -dy}]}, 1]];
n = Length[p2];
If[z == 0, Polygon[p2],
Table[Polygon[{p1[[i]], p2[[i]], p2[[Mod[i, n] + 1]],
p1[[Mod[i, n] + 1]]}], {i, 1, n}]], {z, 0, L, dz}],
ViewPoint -> {-1, 1, 1}, ViewVertical -> {0, 1, 0}, Axes -> True,
AxesLabel -> {"x", "y", "z"},
PlotRange -> {{-a, a}, {-b, b}, {0, L}}]], {T, 0, 1, 0.1}];
SelectionMove[
EvaluationNotebook[], All, GeneratedCell];
FrontEndTokenExecute["CellGroup"];
FrontEndTokenExecute["OpenCloseGroup"];
FrontEndTokenExecute["SelectionAnimate"];

• What behaviour were you expecting, that is not occurring? It's hard to help you without more information. "It doesn't work" is not very informative, unfortunately. Commented Oct 3, 2014 at 7:31
• Rotate is a built-in function. You cannot redefine it just like that. Furthermore, lots of undefine variables and what's 0 gammaxz supposed to do? And as to animation, why the rather strange use of frontend tokens? Are you aware of the Animate function? Commented Oct 3, 2014 at 10:42
• Since we have a good answer from Junbo Lee, I think this question should be reopened, as there was clearly enough info for him to answer it. Commented Oct 3, 2014 at 11:46

## 1 Answer

I think this code might be old version. You made some mistake so I changed your code like this.

1. Rotate -> rotate

2. Do -> Table

3. SelectionAnimate -> Manipulate or Animate

We can give change variable T in Manipulate but that computation time is so long. Thus I set the Graphics3D-s to frames and apply Manipulate or Animate.

Have try like this.

a = 1; b = 1; L = 10; G = 1; dx =
dy = dz = 0.25; J = 2.1849801331564294;
Clear[x, y, z, T];
k = T/(G J);
phi = ((32*G*k*a)*Sum[((-1)^((n - 1)/2)*(1 - Cosh[(n*Pi*y)/(2*a)]/
Cosh[(n*Pi*b)/(2*a)])*Cos[(n*Pi*x)/(2*a)])/
n^3, {n, 1, 10, 2}])/Pi^3;
tauyz = -D[phi, x]; tauxz = D[phi, y];
gammayz = tauyz/G;
gammaxz = tauxz/G;
rotate[{x_, y_, z_}, Theta_] := {x Cos[Theta] - y Sin[Theta],
x Sin[Theta] + y Cos[Theta], z};
Off[Graphics3D::gprim];

frames = Table[
Graphics3D[
Table[alpha = 0.5 k z; p1 = p2;
p2 = (({x, y} = #1;
rotate[{x + (0 gammaxz)/2, y + (0 gammayz)/2,
z + 1/2 (x gammaxz + y gammayz)}, alpha]) &) /@ Flatten[{
Table[{x, -(b/2)}, {x, -(a/2), a/2 - dx, dx}],
Table[{a/2, y}, {y, -(b/2), b/2 - dy, dy}],
Table[{x, b/2}, {x, a/2, dx - a/2, -dx}],
Table[{-(a/2), y}, {y, b/2, dy - b/2, -dy}]}, 1];
n = Length[p2];
If[z == 0, Polygon[p2],
Table[Polygon[{p1[[i]], p2[[i]], p2[[Mod[i, n] + 1]],
p1[[Mod[i, n] + 1]]}], {i, 1, n}]], {z, 0, L, dz}],
ViewPoint -> {-1, 1, 1}, ViewVertical -> {0, 1, 0}, Axes -> True,
AxesLabel -> {"x", "y", "z"},
PlotRange -> {{-a, a}, {-b, b}, {0, L}}],
{T, 0, 1, 0.1}];


You can manipulate motion with this code.

Manipulate[frames[[i]], {i, 1, Length[frames], 1}]


or

Animate[frames[[i]], {i, 1, Length[frames], 1},
AnimationRunning -> False]


• There is a glitch with a = 1 and b = 2 which isn't present when both are equal to 1 so I edited your answer and the plot (with a better quality :)). Feel free to rollback :)
– Öskå
Commented Oct 3, 2014 at 15:26
• @Öskå I got it. thanks Commented Oct 3, 2014 at 15:31