# How to solve differential equations in for loop in mathematica?

I have a system of differential equation for which I want analytical solution. Let say my equations are -

x'[t] - X[[i]]*y[t] == 0,
y'[t] + X[[i]]*x[t] == 0


where X = [0.11, 0.21, 0.31, 0.41]

How can I write a for loop such that it takes X[[i]] from X and find the analytical solution and print/plot it?

I am trying following -

For[i = 1, i < 5,
sols = DSolve[{x'[t] - X[[i]]*y[t] == 0,
y'[t] + X[[i]]*x[t] == 0}, x[0] == 1, y[0] == 0, {x, y}, t]
];


Unfortunately, it isn't working.

c = Range[11, 41, 10]/100;
sol = Table[
DSolveValue[{x'[t] - c[[i]]*y[t] == 0, y'[t] + c[[i]]*x[t] == 0,
x[0] == 1, y[0] == 0}, {x[t], y[t]}, t], {i, Length@c}];
Plot[sol, {t, 0, 60}, PlotLegends -> "Expressions",
ImageSize -> Large]


• Thanks @Okkes Dulgerci. Could you please tell how to export solutions x[t] and y[t] separately in different files. Apr 5, 2018 at 13:57
• x[t]=sol[[All,1]] and y[t]=sol[[All,2]] Apr 5, 2018 at 14:05
• Or {x[t],y[t]}=Transpose@sol Apr 5, 2018 at 14:07
coefs = {0.11, 0.21, 0.31, 0.41}
DSolve[{x'[t] - #*y[t] == 0, y'[t] + #*x[t] == 0, x[0] == 1,
y[0] == 0}, {x, y}, t] & /@ coefs

X = {0.11, 0.21, 0.31, 0.41}
sols = {};
For[i = 1, i < 5, i++,
AppendTo[sols,DSolve[{x'[t] - X[[i]]*y[t] == 0, y'[t] + X[[i]]*x[t] == 0, x[0] == 1, y[0] == 0}, {x[t], y[t]}, t]]];