Message Boards Message Boards

Concatenating a function

Hello everyone,

I need to generate many functions of the same family; A much simplified version of which is something like:

f"i"[x_] =: x+i.

So I want to name the different functions like

f1 [x_] = x+1
f2 [x_] = x+2
f50 [x_] = x+50
fi [x_] = x+i

Any suggestions on how to create them with the name I want to;
3 years ago
You can easity do this by either defining
f[x_,i_]:= x+i
The latter is an example of how to use Operate function.
POSTED BY: Shenghui Yang
3 years ago
It is possible to do this, but I strongly recommend against it. Most often, this kind of programming leads to bad design and is difficult to debug. Shenghui's suggestion is the best way to do this kind of thing.

To answer your question, you can define your functions all at once by evaluating:
Do[Symbol["f" <> ToString[i]][x_] := Evaluate[x + i], {i, 1, 50}]
POSTED BY: Sean Clarke
3 years ago
Thank you both. I used your suggestion Sean!
3 years ago

Group Abstract Group Abstract