1

I am trying to initialize a collection of abstract functions $\theta_k(t)$ in a variable $t$ where $1\leq k\leq n$.

I cannot define them explicitely; the entire point is that I want to numerically solve an ODE I can define with their derivatives. It's clear how I can do this if I had the array, but I don't know how to initialize the array.

If I only cared about a particular value for $n$, I could declare an array of functions explicitly like this:

thetas = Array[{theta1[t_], theta2[t_], ... , thetan[t_]}]

There should be a away to initialize an array of abstract functions in the variable $t$. Once I have such an array, thetas, I can likewise initialize a second array, derivs such that

For[i=0,i<n, ++i,
derivs[[i]]=Sum[matrix[[i,j]]Sin[Abs[thetas[[j]]-thetas[[i]]]],{j,0,n}]
]

and then use NDSolve as suggested in the comments:

NDSolve[derivs, thetas[0] = myInitialCondition, thetas, {t,0,reallyBigNumber}]

gdd
  • 133
  • 4
  • The question is unclear, please show us your specific problem. Anyway, if I have to guess: are you trying to handle implicit array? If so, in most cases you don't need any special treatment, NDSolve can directly handle it, see 3rd example in section Scope -> Ordinary Differential Equations of document of NDSolve. – xzczd Dec 03 '22 at 01:34
  • @xzczd Maybe I'm misunderstanding your meaning, but the third example (entitled "Directly specify a system of equations") does not do what I want; the author explicitly names the functions they are differentiating, that is, they specify ahead of time that they are defining a system of ODEs using three functions, x(t), y(t), and z(t). For any n, I want to initialize a collection of n functions; if they are in an array A, I can de-reference them with their index and define the system of ODEs with a For loop. I don't know how to tell Mathematica "A is an array of functions in the variable t" – gdd Dec 03 '22 at 02:21
  • I'm refering to "Solve for a vector-valued function". Notice there're separation lines between the examples. – xzczd Dec 03 '22 at 02:24
  • You can also find many examples in this site. Just a few examples: https://mathematica.stackexchange.com/q/144339/1871 https://mathematica.stackexchange.com/q/268249/1871 https://mathematica.stackexchange.com/q/25260/1871 Then, don't use For loop: https://mathematica.stackexchange.com/q/134609/1871 – xzczd Dec 03 '22 at 02:39
  • @xzczd Ah, apologies. I think I can do something with this. Thank you. – gdd Dec 03 '22 at 02:42
  • Something like: n = 3;; thetas = Table[Symbol["theta" <> ToString[i]][t_], {i, n}] ? – Daniel Huber Dec 03 '22 at 08:20
  • As a rule of thumb, never use For in Mathematica. There is almost always a much better way. See here for details. Here, for instance, there is no need to pre-initialize your functions – they can be calculated when needed: see lazy evaluation. – Roman Dec 03 '22 at 10:36

1 Answers1

0

Looks with With could be used.

a way to initialize an array of abstract functions

With[{vars = Array[theta, 3]}, vars = {"a", "b", "c"}]

or with non-ordinal indices

With[{vars = {theta[4], theta[7], theta[8]}}, vars = {"a", "b", "c"}]

e.g.

theta[8]

c

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108