# How to put equations in Block or Module to return n values?

i have this code it return EigenValues, it's all right.

Problem:

i have to specify n in order for summation and solution to give output.

Solution:

i just need to put them in Block or Module to calculate EigenValues of system n without specifying prior n so i can later just plug any n for the system.

This is my Code

Clear["Global*"]
Needs["VariationalMethods"]

n = 4; (* thats what i mean problem and solution*)
m = 0.145;
Subscript[x, 0][t_] = 0;
Subscript[x, n + 1][t_] = 0;
Subscript[k, (j_)?EvenQ] = 1.7;
Subscript[k, (j_)?OddQ] = 5;
Subscript[k, 1] = 0;
Subscript[k, n + 1] = 0;

ue[x_, t_, k_, n_] :=
(1/2)*Sum[Subscript[k, j]*(Subscript[x, j - 1][t] - Subscript[x, j][t])^2,
{j, 1, n + 1}];

te[x_, t_, n_] := (1/2)*m*Sum[Derivative[1][Subscript[x, j]][t]^2, {j, 1, n}];

lg[x_, t_, k_, n_] := te[x, t, n] - ue[x, t, k, n];

eq[x_, t_, k_, n_] := Expand[EulerEquations[lg[x, t, k, n],
Table[Subscript[x, j][t], {j, 1, n}], t]];

sol1[x_, t_, k_, n_] := Simplify[Solve[eq[x, t, k, n],
Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}]]];

lst = -(m*D[Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}] /.
First[sol1[x, t, k, n]], {Table[Subscript[x, j][t], {j, 1, n}]}]);

Eigenvalues[lst]


The Solution is to put them in Block defined by n

Example given but doesn't return any values for me!

Clear["Global*"]
Needs["VariationalMethods"]

w[n_] := Block[{p = n},

m = 0.145;
Subscript[x, 0][t_] = 0;
Subscript[x, p + 1][t_] = 0;
Subscript[k, (j_)?EvenQ] = 1.7;
Subscript[k, (j_)?OddQ] = 5;
Subscript[k, 1] = 0;
Subscript[k, p + 1] = 0;

ue[x_, t_, k_, p_] := (1/2)*Sum[Subscript[k, j]*
(Subscript[x, j - 1][t] - Subscript[x, j][t])^2, {j, 1, p + 1}];

te[x_, t_, p_] := (1/2)*m*Sum[Derivative[1][Subscript[x, j]][t]^2, {j, 1, p}];

lg[x_, t_, k_, p_] := te[x, t, p] - ue[x, t, k, p];

eq[x_, t_, k_, p_] := Expand[EulerEquations[lg[x, t, k, p],
Table[Subscript[x, j][t], {j, 1, p}], t]];

sol1[x_, t_, k_, p_] :=
Simplify[Solve[eq[x, t, k, p], Table[Derivative[2][Subscript[x, j]][t],
{j, 1, p}]]];

lst = -(m*D[Table[Derivative[2][Subscript[x, j]][t], {j, 1, p}] /.
First[sol1[x, t, k, p]], {Table[Subscript[x, j][t], {j, 1, p}]}]);

Eigenvalues[lst]
]


so When i put

w[4]


it gives eigenvalues for system with n=1 so i choose independently my n Then i could ListPlot All 1

i could do this by

q=Table[i,{i,1,30}]
ListPlot[w[q]]

• It works for me after changing Block to Module and fixing the typo "EvepQ". I'm also nor sure you want the Drop at the end - it removes all the values from the list. Apr 7 '19 at 23:06
• can you please paste your full try. i edit the question EvepQ & Drop
– user63891
Apr 8 '19 at 0:08
• it still not giving anything
– user63891
Apr 8 '19 at 0:19

Don't put your definitions for other functions in your definition of w. Write your code like this:

Clear["Global*"]
Needs["VariationalMethods"]

m = 0.145;
Subscript[x, 0][t_] = 0;
Subscript[x, p + 1][t_] = 0;
Subscript[k, (j_)?EvenQ] = 1.7;
Subscript[k, (j_)?OddQ] = 5;
Subscript[k, 1] = 0;
Subscript[k, p + 1] = 0;
ue[x_, t_, k_, p_] :=
(1/2)*Sum[Subscript[k, j]*(Subscript[x, j - 1][t] - Subscript[x, j][t])^2, {j, 1, p + 1}];
te[x_, t_, p_] := (1/2)*m*Sum[Derivative[1][Subscript[x, j]][t]^2, {j, 1, p}];
lg[x_, t_, k_, p_] := te[x, t, p] - ue[x, t, k, p];
eq[x_, t_, k_, p_] :=
Expand[EulerEquations[lg[x, t, k, p], Table[Subscript[x, j][t], {j, 1, p}], t]];
sol1[x_, t_, k_, p_] :=
Simplify[Solve[eq[x, t, k, p], Table[Derivative[2][Subscript[x, j]][t], {j, 1, p}]]];

w[n_] :=
Block[{p = n, lst},
lst =
-(m*D[Table[Derivative[2][Subscript[x, j]][t], {j, 1, p}] /.
First[sol1[x, t, k, p]], {Table[Subscript[x, j][t], {j, 1, p}]}]);
Eigenvalues[lst]]


Then

Table[w[i], {i, 4}]


{{1.7}, {7.22324, 1.17676}, {11.8445, 2.82344, 0.432089}, {12.1077, 6.75913, 2.59265, 0.340519}}