Mathematica - automatically change function name in for loop

In summary, the conversation was about a person trying to call different functions in a loop to calculate something, and they were looking for a way to replace an arbitrary sign with a variable in order to call the functions N0, N1, etc. with different values for time. The solution involved using a loop and the ToExpression function to define 27 functions x[0] to x[26] with the desired values. Another person suggested using a table and map instead of a loop.
  • #1
drmetal
2
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
  • #2
ToExpression[StringJoin["N", ToString[k]]][time] /. s

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

FAQ: Mathematica - automatically change function name in for loop

1. How can I change the name of a function automatically in a for loop in Mathematica?

In order to automatically change the name of a function in a for loop in Mathematica, you can use the ReplaceAll function. This allows you to specify a pattern to match and a replacement expression. For example, if you have a function named f[x] and you want to change the name to g[x] in a for loop, you can use f[x] /. f -> g within the loop.

2. Can I use a variable as the name of a function in a for loop in Mathematica?

Yes, in Mathematica, you can use a variable as the name of a function. This is known as a "symbolic function." To create a symbolic function, you can use the syntax function[x_]:= expression. This will create a function named function that takes in an argument x and evaluates the expression.

3. How do I change the name of a function in a for loop based on a condition in Mathematica?

To change the name of a function in a for loop based on a condition, you can use an If statement. For example, if you want to change the name of a function to g[x] if x is greater than 5, you can use If[x>5, f[x] /. f -> g, f[x]] within the for loop.

4. Is it possible to automatically create and name functions within a for loop in Mathematica?

Yes, it is possible to automatically create and name functions within a for loop in Mathematica. You can use the Do or Table functions to generate a list of functions with different names, and then use MapThread to apply a different expression to each function in the list.

5. Can I use a function as an argument to change the name of another function in a for loop in Mathematica?

Yes, you can use a function as an argument to change the name of another function in a for loop in Mathematica. This can be done by using the Map or MapIndexed functions. These functions allow you to apply a function to each element in a list, and you can specify the function to change the name of another function as the argument.

Similar threads

Replies
1
Views
900
Replies
5
Views
2K
Replies
6
Views
3K
Replies
1
Views
2K
Replies
12
Views
2K
Replies
1
Views
4K
Replies
2
Views
1K
Replies
1
Views
2K
Back
Top