I wanted to write this code in such a way that I run the whole thing for each $n$, and not write a1 a2 a3 or eq1 eq2 eq3 every time.
Clear[y, x, M, V, th, EI, PE]
y = a4*x^4 + a3*x^3 + a2*x^2;
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}] + P*(y /. x -> L);
Eq2 = D[PE, a2];
Eq1 = D[PE, a3];
Eq3 = D[PE, a4]
Sol = Solve[{Eq1 == 0, Eq2 == 0, Eq3 == 0}, {a3, a2, a4}];
y = y /. Sol[[1]];
th /. Sol[[1]];
FullSimplify[M /. Sol[[1]]]
FullSimplify[V /. Sol[[1]]]
Here's my attempt but it doesn't work:
Clear[a, y, x, M, V, th, EI, PE]
n = 4
y = Sum[{y^i}*a[i], {i, 0, 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}] + P*(y /. x -> L)
Eq = Table [{D[PE, a[i]]}, {i, 2, n}]
Sol = Solve[{Eq[i] == 0}, {a[i]}];
y = y /. Sol[[1]];
th /. Sol[[1]];
FullSimplify[M /. Sol[[1]]]
FullSimplify[V /. Sol[[1]]]
Solvestatement tosol = Solve[Eq == 0, Array[a, n]].Solvewill treat the0as if it were a vector. Also, the warning message can be avoided withsol=Solve[Eq==0, Rest@Array[a, n]]– Bob Hanlon Aug 15 '20 at 23:53-((L P x^2)/(2 EI)) + (P x^3)/(6 EI) + a[0] + x a[1]– flinty Aug 16 '20 at 15:11Remove["Global`*"] n = 4; 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}] + P*(y /. x -> L); Eq = Table[D[PE, a[i]], {i, 1, n}]; sol = Solve[Eq == 0, Array[a, n]] y /. sol th /. sol Eq /. sol; FullSimplify[M /. sol[[1]]] FullSimplify[V /. sol[[1]]] EI = 100; P = 1; L = 10; Plot[{V, -100, 100}, {x, 0, L}, AxesLabel -> {"x (m)", "M (N.m.)"}, PlotLegends -> {Style[" M", Bold, 12]}]– Ali AlCapone Aug 16 '20 at 16:11