0
$\begingroup$

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?

  • 0
    If I remove $f$, e.g. {y'[t] == 2 y[t], y[2] == {1, 2, 3}}, it can be easily solved.2011-09-11

2 Answers 2

1

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}] 
  • 0
    @Mohsen: I'd try to use `Piecewise[]` instead of `If[]`. Something like `f[y_List] := {Piecewise[{{1, y[[1]] > 2}, {2, True}}]*y[[2]], y[[2]] + y[[3]], y[[3]] + y[[1]]}`2011-09-12
2

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}] 
  • 0
    Thanks J.M., good point. Do you have any idea about my comment under Andrew's answer?2011-09-12