I am trying to solve this system
$$\left( \begin{array}{ccccc} 2 k & -k & 0 & 0 & 0 \\ -k & 2 k & -k & 0 & 0 \\ 0 & -k & 2 k & -k & 0 \\ 0 & 0 & -k & 2 k & -k \\ 0 & 0 & 0 & -k & 2 k \\ \end{array} \right) \left( \begin{array}{c} y_1(t) \\ y_2(t) \\ y_3(t) \\ y_4(t) \\ y_5(t) \\ \end{array} \right)+\left( \begin{array}{ccccc} m & 0 & 0 & 0 & 0 \\ 0 & m & 0 & 0 & 0 \\ 0 & 0 & m & 0 & 0 \\ 0 & 0 & 0 & m & 0 \\ 0 & 0 & 0 & 0 & m \\ \end{array} \right) \left( \begin{array}{c} y_1''(t) \\ y_2''(t) \\ y_3''(t) \\ y_4''(t) \\ y_5''(t) \\ \end{array} \right)=\left( \begin{array}{c} F(t) \\ 0 \\ F(t) \\ 0 \\ F(t) \\ \end{array} \right)$$ but the following code doesn't seem to work..
n = 6;
M = m IdentityMatrix[n - 1];
K = Table[0, {j, 1, n - 1}, {i, 1, n - 1}];
For[j = 1, j <= n - 1, j++,
For[i = 1, i <= n - 1,
i++, {If[j == i, K[[j, i]] = 2 k, Nothing],
If[i == j + 1, K[[j, i]] = -k, Nothing],
If[i == j - 1, K[[j, i]] = -k, Nothing]}]]
yy[t] = Table[Subscript[y, i][t], {i, 1, n - 1}];
FF[t] = Table[
If[(i/n) == 1/6 || i == n/2 || (i/n) == 5/6, F[t], 0], {i, 1,
n - 1}];
F[t_] = Piecewise[{{Subscript[F, 0] t/(T/2),
0 <= t <= T/2}, {-Subscript[F, 0],
T/2 <= t <= T}}];
m = 1;
k = 1;
Subscript[F, 0] = 1;
T = 1;
NDSolve[{M.D[yy[t], {t, 2}] + K.yy[t] == F[t], yy[t] == 0 /. t -> 0,
D[yy[t], t] == 0 /. t -> 0}, yy[t], {t, 10 T, 20 T}]
For
loops and have much more readable code (though your layout is good) by usingDiagonalMatrix
: reference.wolfram.com/language/ref/DiagonalMatrix.html $\endgroup$