# Is there any way to define a programming function not mathematic one?

I want to define a programming function, as I understood in Mathematica we can define it as Module. Is there any way to pack this code in a Module and call it n time to plot n time m v , Y like in range 1,n? I just want to compare the results for different n.

Remove["Global*"]
Clear["Global*"]
n = 5;
y = Sum[x^i*a[i], {i, 2, n}];
th = D[y, x];
M = EI*D[y, {x, 2}];
V = EI*D[y, {x, 3}];
PE = EI/2*Integrate[(D[y, {x, 2}])^2, {x, 0, L}] -
Integrate[q*(x/L)*y, {x, 0, L}];
Eq = Table[D[PE, a[i]], {i, 1, n}];
sol = Solve[Eq == 0, Array[a, n]]
Y = y /. sol
th /. sol
Eq /. sol;
m = FullSimplify[M /. sol[[1]]]
v = FullSimplify[V /. sol[[1]]]
EI = 100;
q = -1;
L = 10;
Plot[{m}, {x, 0, L}, AxesLabel -> {"x (m)", "M (N.m.)"},
PlotLegends -> {Style[" M", Bold, 12]}]
Plot[{v}, {x, 0, L}, AxesLabel -> {"x (m)", "V (N/m^2.)"},
PlotLegends -> {Style[" V", Bold, 12]}]
Plot[{Y}, {x, 0, L}, AxesLabel -> {"x (m)", "Y (m)"},
PlotLegends -> {Style[" Y", Bold, 12]}]

• Take a look at the documentation. Commented Aug 20, 2020 at 16:28

To define a function you should clarify what you want as the input and output. As an alternative, you can use Manipulate

Clear["Global*"]

EI = 100;
q = -1;
L = 10;

Manipulate[
y = Sum[x^i*a[i], {i, 2, n}];
th = D[y, x];
M = EI*D[y, {x, 2}];
V = EI*D[y, {x, 3}];
PE = EI/2*Integrate[(D[y, {x, 2}])^2, {x, 0, L}] -
Integrate[q*(x/L)*y, {x, 0, L}];
Eq = Table[D[PE, a[i]], {i, 1, n}];
sol = Solve[Eq == 0, Array[a, n]];
Y = y /. sol;
th /. sol;
Eq /. sol;
m = FullSimplify[M /. sol[[1]]];
v = FullSimplify[V /. sol[[1]]];
Column[{
StringForm["M = ", m],
StringForm["V = ", v],
StringForm["Y = ", Y[[1]]],
Plot[Evaluate[Tooltip /@ {m, v, Y}], {x, 0, L},
AxesLabel -> {Style["x (m)", 12, Bold], None},
PlotLegends -> Placed[
(Style[#, 12, Bold] & /@ {"M (N.m.)", "V (N/m^2)", "Y (m)"}),
{0.7, 0.35}],
ImageSize -> Medium]}],
{{n, 5, Style["n", 12, Bold]}, 3, 6, 1, Appearance -> "Labeled"}] //
Quiet
`