Solving ODE in Mathematica

438 Views Asked by At

I want to solve an ODE in the form $\{y'[t] == f[y[t]], y[2] == \{1, 2, 3\}\}$ using NDSolve in Mathematica, where $f: R^3 \rightarrow R^3$ is defined as follows,

f[y_] := {2 y[[1]] + 1, 3 y[[2]] + y[[3]], 2 y[[3]] + y[[1]]}
s = NDSolve[{y'[t] == f[y[t]], y[2] == {1, 2, 3}}, y, {t, 0, 10}]
Plot[First[y /. s][t], {t, 0, 10}]

However, when I run the code it says "Part::partw: "Part 2 of y(t) does not exist."". How can I solve the problem?

2

There are 2 best solutions below

4
On BEST ANSWER

Changing definition of $y$ and making as many equations as there are functions in $y$ could help here. This one works:

f[y_] := {2 y[[1]] + 1, 3 y[[2]] + y[[3]], 2 y[[3]] + y[[1]]}
y[t_] = {y1[t], y2[t], y3[t]};
s = NDSolve[{#[[1]] == #[[2]]&/@Transpose[{y'[t], f[y[t]]}], 
             #[[1]] == #[[2]]&/@Transpose[{y[2], {1, 2, 3}}]}, 
             y[t], {t, 0, 10}]
Plot[First[y[t] /. s], {t, 0, 10}]
1
On

Thread[] is your friend:

f[y_] := {2 y[[1]] + 1, 3 y[[2]] + y[[3]], 2 y[[3]] + y[[1]]};
y[t_] = {y1[t], y2[t], y3[t]};
s = NDSolve[Join[Thread[y'[t] == f[y[t]]], Thread[y[2] == {1, 2, 3}]], 
   y[t], {t, 0, 10}];
Plot[First[y[t] /. s], {t, 0, 10}]