# How to combine more plots in single image inside a loop

I was reading basic help here How to | Combine Two or More Graphics , but I did not get it.

I have simple differential equation and want to change a constant "b" (in this case "damping") and plot all graphs in a single image. Here is the code:

(*Single mass damper system of a rail car *)
(*with 10 mm starting deflection *)
m = 40000;     (* [kg]    *)
k = 1600000; (* [N/m]   *)

for[b=0, b<=160000, b=b+20000,

sol = NDSolve[{
m*x''[t] + b*x'[t] + k*x[t]== 0,
x'[0] == 0,x[0] == 0.01},
x[t], {t, 0, 10}];

Print[Plot[x[t] /. sol, {t, 0, 3},
PlotRange -> Full ,
PlotTheme -> {"Grid"},
AxesLabel->{"Time [s]", "Vertical suspension deflection [m]"},
PlotLabel->Style["Single mass damper system of a rail car", 12, Bold]]]
]


• Trying to use the procedural programming you are used from Matlab usually wont give you a nice solution in mathematica. The more idiomatic Mathematica way is usually shorter and easier. Your problem gets very easy like that: sols=Table[x[t]/.NDSolve[{m*x''[t]+b*x'[t]+k*x[t]==0,x'[0]==0,x[0]==0.01},x[t],{t,0,10}],{b,0,160000,20000}]; Plot[sols,{t,0,3}] Jun 27 '21 at 21:03
• You should also be aware that for is not the same as For. Jun 28 '21 at 4:06
• I also recommend looking up ParametricNDSolveValue, which makes is much easier to solve DEs for different parameter values. Jun 28 '21 at 6:47
• Thank you all for the hints! I really appreciate that! Jun 28 '21 at 15:34

Clear["Global*"];

m = 40000;
k = 1600000;


Your equation can be solved exactly with DSolve

sol = DSolve[{m*x''[t] + b*x'[t] + k*x[t] == 0, x'[0] == 0, x[0] == 1/100},
x[t], t][[1]] // Simplify


Alternatively,

sol2 = sol // ExpToTrig // Simplify


Plotting,

Plot[Evaluate@Table[x[t] /. sol,
{b, 0, 160000, 20000}], {t, 0, 3},
PlotTheme -> "Grid",
Frame -> True,
FrameLabel -> (Style[#, 12, Bold] & /@
{"Time [s]",
"Vertical suspension deflection [m]"}),
PlotLabel ->
Style["Single mass damper system of a rail car", 12, Bold],
PlotLegends ->
LineLegend[Range[0, 160000, 20000],
LegendLabel -> Style["b =", 12, Bold]]]
`

• Thank you very much! That works perfect! I appreciate your help. Jun 28 '21 at 15:33