Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica - automatically change function name in for loop

  1. Sep 24, 2010 #1
    Hi folks,

    i just want to call different functions which are solutions of set of rate equations from s= NDSolve in a loop to calculate something.



    For[k = 0, k < 27, k++,

    x[k] = N#[time] /. s;

    ]

    # is just an arbitrary sign. i just want to replace # with k so i call

    N0[time]
    N1[time]
    ...


    how do i do that ?
    i use mathematica 7

    thanks
    Heiko
     
  2. jcsd
  3. Sep 24, 2010 #2

    Hepth

    User Avatar
    Gold Member

    ToExpression[StringJoin["N", ToString[k]]][time] /. s

    might work
     
  4. Sep 24, 2010 #3
    Are you are trying to fill a table with 27 results. If so then perhaps something like this.

    time=1;s=p->q;x=Table[0,{26}];
    For[k=1,k<27,k++,
    x[[k]]=ToExpression[StringJoin["N", ToString[k]]][time] /. s;
    ];
    x

    which gives

    Out[6]={N1[1],N2[1],N3[1],N4[1],N5[1],N6[1],N7[1],N8[1],N9[1],N10[1],N11[1],N12[1],N13[1],N14[1],N15[1],N16[1],N17[1],N18[1],N19[1],N20[1],N21[1],N22[1],N23[1],N24[1],N25[1],N26[1]}

    Are you are trying to define 27 "functions" x[0], x[1],...x[26] then something like this.

    time=1;s=p->q;
    For[k=0,k<27,k++,
    x[k]=ToExpression[StringJoin["N",ToString[k]]][time]/.s;
    ];
    ?x

    which gives

    x[0] = N0[1],
    x[1] = N1[1],
    ...
    x[26] = N26[1]
     
    Last edited: Sep 24, 2010
  5. Sep 25, 2010 #4
    Do you even need a loop? can't it be done with table and map?
     
  6. Sep 27, 2010 #5
    thanks. that was exactly what i was looking for. it works
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook