Mathematica - automatically change function name in for loop

Click For Summary

Discussion Overview

The discussion revolves around how to dynamically call different functions generated from a set of rate equations in Mathematica using a loop. Participants explore methods to automate the function naming and retrieval process within a loop structure.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks a method to replace a placeholder in a function name with a loop index to call functions like N0[time], N1[time], etc.
  • Another participant suggests using ToExpression and StringJoin to construct the function names dynamically.
  • A different approach is proposed to fill a table with results using a loop, providing an example of how to store the results in an array.
  • There is a suggestion to consider alternative methods such as using Table or Map instead of a loop for potentially more efficient code.
  • A participant confirms that one of the proposed solutions worked for their needs.

Areas of Agreement / Disagreement

The discussion includes multiple proposed methods for achieving the goal, indicating that there is no single agreed-upon solution. Participants explore different approaches without reaching a consensus on the best method.

Contextual Notes

Some methods rely on specific assumptions about the structure of the functions and the data being processed, which may not be universally applicable. The effectiveness of the proposed solutions may depend on the specific context of the problem.

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 5 ·
Replies
5
Views
3K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K