0
$\begingroup$

i tried to construct a matrix from the coefficients of my solution, but problem that;

1; how can i get coefficients out of Solve

2; some Matrix elements are empty so i must assign 0 in those empty cells. and that's why it doesn't construct matrix directly by MatrixForm.

look at Code after Sol input**

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

n = 4; 
m = 0.145; 

Subscript[x, 0][t_] = 0;
 "Those two initial condition make some cells empty that's why i can't directly construct a matrix using matrixform"
Subscript[x, n + 1][t_] = 0; 

Subscript[k, (j_)?EvenQ] = 1.7; 
Subscript[k, (j_)?OddQ] = 5; 
Subscript[k, 0] = 1; 
Subscript[k, n + 1] = 1; 

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]]; 


sol = Simplify[MatrixForm[Solve[eq[x, t, k, n], Table[Derivative[2][Subscript[x, j]][t], 
      {j, 1, n}]]]]

" I Tried these down with all ways never give me coefficients alone so i put them in a matrix i just need the number with no x[t] or x''[t]"

rules = CoefficientRules[lst[j], {Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}]}]

matr = CoefficientArrays[lst, Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}]]

lst = List @@ sol
$\endgroup$
3
  • 2
    $\begingroup$ There are many issues with your code. First of all, the use of Subscripts isn't recommended. Secondly, you are trying to use CoefficientRules and CoefficientArrays on lst before it's even defined. Furthermore, it seems like lst is a List, and yet inside CoefficientRules, you are using lst[j] when likely it should be lst[[j]]. Unfortunately, Mathematica.SE is not a debugging service, so unless you can focus on a specific issue with Mathematica that you are having, it's difficult to help. Also, perhaps you should explain what you are trying to do. $\endgroup$
    – march
    Mar 14, 2019 at 20:21
  • $\begingroup$ i will edit my question sir. thanks $\endgroup$
    – nufaie
    Mar 14, 2019 at 20:47
  • $\begingroup$ Don't use MatrixForm to define matrices, it is only a display wrapper and interferes with calculations. See mathematica.stackexchange.com/questions/3098/… $\endgroup$
    – Roman
    Mar 15, 2019 at 0:43

1 Answer 1

3
$\begingroup$

After you get the sol without MatrixForm with

sol = Simplify[Solve[eq[x,t,k,n], Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}]]]

you can get the matrix of coefficients with

matr = D[Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}] /. First[sol],
  {Table[Subscript[x, j][t], {j, 1, n}]}]

{{-46.2069, 11.7241, 0, 0}, {11.7241, -46.2069, 34.4828, 0}, {0, 34.4828, -46.2069, 11.7241}, {0, 0, 11.7241, -18.6207}}

The first part of this is how you get the solutions out of Solve:

Table[Derivative[2][Subscript[x, j]][t], {j, 1, n}] /. First[sol]

$$ \left\{11.7241 x_2(t)-46.2069 x_1(t),11.7241 x_1(t)-46.2069 x_2(t)+34.4828 x_3(t),34.4828x_2(t)-46.2069 x_3(t)+11.7241 x_4(t),11.7241 x_3(t)-18.6207 x_4(t)\right\} $$

The second part is taking the first derivative with respect to each one of the $x_j(t)$ in order to get the matrix of coefficients.

Finally, if you want to look at matr in a pretty form, you can use MatrixForm as a display wrapper:

MatrixForm[matr]

$$ \left( \begin{array}{cccc} -46.2069 & 11.7241 & 0 & 0 \\ 11.7241 & -46.2069 & 34.4828 & 0 \\ 0 & 34.4828 & -46.2069 & 11.7241 \\ 0 & 0 & 11.7241 & -18.6207 \\ \end{array} \right) $$

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.