Mathematica Mathematica - automatically change function name in for loop

AI Thread Summary
The discussion focuses on how to dynamically call different functions in Mathematica within a loop to solve a set of rate equations. A user seeks to replace a placeholder symbol with an index in function names, specifically transitioning from N# to N[k]. Solutions provided include using ToExpression to construct function names and filling a table with results using a For loop. The conversation also suggests alternatives like using Table and Map for efficiency. Ultimately, the proposed solutions effectively meet the user's needs for generating the desired function calls.
drmetal
Messages
2
Reaction score
0
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
 
Physics news on Phys.org
ToExpression[StringJoin["N", ToString[k]]][time] /. s

might work
 
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:
Do you even need a loop? can't it be done with table and map?
 
thanks. that was exactly what i was looking for. it works
 

Similar threads

Replies
0
Views
501
Replies
5
Views
2K
Replies
6
Views
4K
Replies
12
Views
2K
Replies
1
Views
4K
Back
Top