1

How can I create such lists of variables:

list1={sus1[t],sus2[t],sus3[t],...,sus99[t]};
list2={inf1[t],inf2[t],inf3[t],...,inf99[t]};

Consecutive Integers should be placed in between sus and [t] and between inf and [t] as in the above examples.

Earlier, @kglr did the following:

list = {das[t], das[t], das[t]};
Replace[list, {h_[a__] :> Symbol[SymbolName[h] <> "0"][a], 
 s_ :> Symbol[SymbolName[s] <> "0"]}, {1}]

Which replaces 0 only.

What I have in mind is a format like:

fn[sus_, n_, t_]:= something

and

fn[inf, 3, t] 

should generate

{inf1[t], inf2[t], inf3[t]}
Tugrul Temel
  • 6,193
  • 3
  • 12
  • 32
  • 3
    Why are you doing this with symbols? Your life will be significantly better by just using 2D arrays – b3m2a1 Dec 06 '20 at 18:07
  • @b3m2a1: How can I use 2D arrays? – Tugrul Temel Dec 06 '20 at 18:09
  • 1
    BTW, "tailor-made" generally means hand-altered, that is, type them out by hand; what you really want is "machine-made" or "factory-made." The answer to your problem must already exist on site: Table[ToExpression["das"<>ToString[k]][t], {k, 99}] -- You can use Symbol instead of ToExpression. – Michael E2 Dec 06 '20 at 18:12
  • @MichaelE2: I edited the title statement of my question. Let me try your suggestion. Thanks. – Tugrul Temel Dec 06 '20 at 18:16
  • @MichaelE2: Yes, your answer works. You may put it as an answer if you like. Tugrul – Tugrul Temel Dec 06 '20 at 18:18
  • 2
    @TugrulTemel just store your data in a list-of-list format or use indexed variables – b3m2a1 Dec 06 '20 at 18:27
  • 1
    I'd like to second @b3m2a1 's suggestion here. Don't bother with constructing variable names with strings. Instead use numerical indices. It's much more amenable to machine-manipulation down the line. – MarcoB Dec 06 '20 at 20:51
  • @MarcoB: I have string variable names used as the variables of a model, which is solved for the string variables. I have limited knowledge of programming, and therefore, I would appreciate if you give me a simple example of your suggestion. Thanks. – Tugrul Temel Dec 06 '20 at 21:08
  • 1
    @Tugrul Yes I can see how that would be conceptually easier (indeed, I've done it myself in the past, and lived to regret it!). I think my suggestion is well exemplified by Bob Hanlon's answer below. Even more to the point may be to just keep working with lists of numbers if you only need numerical evaluation. Alternatively, I have been warming up to the use of Association or lists or Rules to store data together with their metadata (see e.g. https://mathematica.stackexchange.com/a/235579/27951). – MarcoB Dec 06 '20 at 21:33
  • @MarcoB: Thanks for your reference, and I do not want to regret it later!! therefore, I will study the reference you gave me. I would be happy to hear from you if you make an improvement in the use of Association and Rules. – Tugrul Temel Dec 06 '20 at 22:50

2 Answers2

3
Clear["Global`*"]

n = 3; (* Use any desired value *)

Use indexed variables

susV = Array[sus[#][t] &, n]

(* {sus[1][t], sus[2][t], sus[3][t]} *)

infV = Array[inf[#][t] &, n]

(* {inf[1][t], inf[2][t], inf[3][t]} *)

If you are going to assign values to the variables then the indexed variables will never show up in the output, just their values. Consequently, formatting would not be important. If however you want symbolic results (unlikely with vectors of length 99):

Format[sus[n_][t_]] := Subscript[sus, n][t];
Format[inf[n_][t_]] := Subscript[inf, n][t];

As much as possible, define functions with vector operations.

fn[t_] = susV.infV + infV // Simplify

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
2

Here's a way:

list = Table[ToExpression["das"<>ToString[k]][t], {k, 99}];

One can use Symbol instead of ToExpression.

The closest related Q&A I've found is Some challenge for a Table mixing between output number Variables and ToString symbols

One finds these solutions already presented in many questions:

Michael E2
  • 235,386
  • 17
  • 334
  • 747