4
$\begingroup$

I have a list of solutions Subscript[x, 1][t], i = 1,...,n (n given).

I would like to use these variables to define new variables such as:

i = 1;
For[i = 1, i <= n, i++, 
 Subscript[X, i][t_] := Subscript[x, i][t] /. sol]

I know that this code is not working and I know why. However, I would like to ask you if there is a way how to define these variables at once (without having to define them one by one)?

Thank you very much in advance for your help.

David.

$\endgroup$
3
  • 1
    $\begingroup$ TIP: never use Subscript nor For. They are one of the main sources of confusion. $\endgroup$ Nov 30, 2019 at 18:15
  • $\begingroup$ Here you could try Table[X[i][t_] := x[i][t] /. sol, {i, 1, n}]. Does this work as expected? $\endgroup$ Nov 30, 2019 at 18:16
  • $\begingroup$ Thank you very much, I replaced Substrict and with some more changes itw working properly now. Many thanks! $\endgroup$
    – derdin1
    Nov 30, 2019 at 19:08

1 Answer 1

5
$\begingroup$

You can use Inactive to postpone the SetDelayed operation until after the RHS has been defined. Here is an example:

Clear[Subscript];
sol = Subscript[x, i_][t] -> t^i;
n = 10;

Activate @ Table[
    Inactive[SetDelayed][
        Subscript[X,i][t_],
        Subscript[x,i][t]/.sol
    ],
    {i, n}
];

Check:

Subscript[X, 4][2]

16

$\endgroup$
1
  • $\begingroup$ Many thanks, that helped me a lot! $\endgroup$
    – derdin1
    Nov 30, 2019 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.