0
$\begingroup$

I'm learning Mathematica through examples given in Engineering Vibrations by Daniel J. Inman. However, there is a huge mistake given in this bit of code offered:

M = {{4, 0}, {0, 9}};
K = {{30, -5}, {-5, 5}};
f = {{0.235*Sin[ω*t]}, {2.97922 Sin[ω*t]}};
ω = 2.75655;
x = {{x1[t]}, {x2[t]}}; 
xdd = {{x1''[t]}, {x2''[t]}};
system = m.xdd + k.x;

num = NDSolve[
       {system[[1]] == f[[1]],
        x1'[0] == 0,
        x1[0] == 0,
        system[[2]] == f[[2]],
        x2[0] == 0,
        x2'[0] == 0},
       {x1[t], x2[t]},
       {t, 0, 20}
      ];

Plot[
 {Evaluate[x1[t] /. num], Evaluate[x2[t] /. num]},
 {t, 0, 20}, 
 PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 1, 0]}, 
 PlotLegends -> {"x1[t]", "x2[t]"}
]

If I am to represent the matrix of time-dependent formulas for distance and acceleration, how am I suppose to do it?

$\endgroup$
1
  • 3
    $\begingroup$ You start with upper case M and then use a lower case m. Same with K and k. Those seems to be the only issues. $\endgroup$ Oct 3, 2019 at 20:08

1 Answer 1

3
$\begingroup$

Few edits, then the code will run!

m = {{4, 0}, {0, 9}}; 
k = {{30, -5}, {-5, 5}}; 
f = {{0.235*Sin[\[Omega]*t]}, {2.97922*Sin[\[Omega]*t]}}; \[Omega] = 2.75655; 
x = {{x1[t]}, {x2[t]}}; 
xdd = {{Derivative[2][x1][t]}, {Derivative[2][x2][t]}}; 
system = m . xdd + k . x; 
num = NDSolve[{system[[1]] == f[[1]], Derivative[1][x1][0] == 0, x1[0] == 0, 
     system[[2]] == f[[2]], x2[0] == 0, Derivative[1][x2][0] == 0}, {x1[t], x2[t]}, 
    {t, 0, 20}]; 
Plot[{Evaluate[x1[t] /. num], Evaluate[x2[t] /. num]}, {t, 0, 20}, 

PlotStyle -> {RGBColor[1, 0, 0], RGBColor[0, 1, 0]}, PlotLegends -> {"x1[t]", "x2[t]"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Damn, I didn't realize it was a simple syntax error. Thanks. $\endgroup$
    – TexMexDex
    Oct 3, 2019 at 20:13
  • $\begingroup$ it happens a lot for everyone $\endgroup$
    – nufaie
    Oct 3, 2019 at 20:49

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