1
$\begingroup$

This might be a duplicate, but I have not seen this problem addressed for DSolve usage.

I have a system of ODEs of the form

Y' = AY + B

When we put this into Mathematica as a vector variable:

sol = FullSimplify[DSolve[y'[t] == A . y[t], y \[Element] Vectors[n], t]]

We get a pure function solution where each state variable solution is just separated by a comma. I would like each state variable in the solution to be labelled in the standard way:

vec_{y} = {y_{1}, y_{2}, ..., y_{n}} = {y_{1} = c1*e^(-10t)+..., ...}

So that I can also subset the solution depending on which variable I am interested in, for example sol[[y1]] or sol[[1]].

$\endgroup$
2
  • $\begingroup$ "labelled in the standard way" -- Do you mean in some sort of TeX output? In the Front End notebook? Or in code? You can't use _ in code in this way, because in code, it means Blank[] (q.v.). $\endgroup$
    – Goofy
    Commented Sep 21, 2023 at 23:43
  • $\begingroup$ The code above doesn't work. A minimal working example would help. I have an idea how to transform the output, but I can't check it. $\endgroup$
    – Goofy
    Commented Sep 21, 2023 at 23:45

1 Answer 1

2
$\begingroup$

I'm guessing the output of a working DSolve call is something like this:

sol = {{y -> Function[t, Evaluate@Table[Exp[n t], {n, 3}]]}}
(* {{y -> Function[t, {E^t, E^(2 t), E^(3 t)}]}} *)

Then this splits it up and indexes the solution components. It's not clear to me how you wanted the components constructed. Indexed symbols are the usual way to code subscripts, but the y[k] can be changed to whatever suits you.

{{y -> Function[t, Evaluate@Table[Exp[n t], {n, 3}]]}} /. 
   {y_ -> Verbatim[Function][t_, body_List]} :> 
  Table[y[k] -> Function[t, Evaluate[body[[k]]]], {k, Length@body}]
(*
{{y[1] -> Function[t, E^t], y[2] -> Function[t, E^(2 t)], 
  y[3] -> Function[t, E^(3 t)]}}
*)
$\endgroup$

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.