1
$\begingroup$

Below is a minimal example,

D[k[θ[t]]*θ[t] == θ'[t], t] // TraditionalForm

Output is

$$ \theta (t) \theta '(t) k'(\theta (t))+k(\theta (t)) \theta '(t)=\theta ''(t) $$

Can I determine the form of the equation like $ (\cdot) \ddot{\theta} + (\cdot)*\dot{\theta} + \cdot = 0$(all in left hand)? Or more generally speaking, is there a common method to tidy up the formula output by assuming orders?

By the way, can I abbreviate $k(\theta(t))$ as $k(\theta)$ and $\theta(t)$ as $\theta$ in more compact form? I find a general method to do it by Pre-defined function, it seems more difficult.

Any comments will be much appreciated!

$\endgroup$

2 Answers 2

4
$\begingroup$

This question arises from time to time on this site. Unfortunately, I have no references to the previous discussions.

In general, Mma has its internal order to write the expressions. The use of the TraditionalForm may partially change this order, but not completely. It is impossible to violate this order without introducing some limitations to the expression. This I describe below.

In this situation, it is important to understand why do you want to get the form you are asking for? The answer to your question depends on this reason.

I see two reasons to do that.

  1. The expression in question is the ready result to use for a presentation of any kind. For example, you are going to show it during a lecture, or publish it in a report, paper, etc.
  2. You need to have this form of expression to better see it. For example, this expression is your intermediate result, but you need to get it in a comfortable or familiar form to decide how to operate further.

In the first case, I assume that you do not need to work with this expression anymore. For example, you will not transform it further, or use it to make a plot, solve, or whatever.

In this case, one can reorder the expression using the function Hold or HoldForm. These functions prevent Mma from any reordering of the expressions. You can do it in a number of ways. This has also been already discussed here, and some good solutions have already been published. I usually use a function for that which you can use if you like it:

rearrange[expr_, finalPositions_List] := Module[{lst, newlst},
   lst = List @@ expr;
   newlst = 
    Table[lst[[Position[finalPositions, i][[1, 1]]]], {i, 1, 
      Length[lst]}];
   HoldForm[Evaluate[expr]] /. MapThread[Rule, {lst, newlst}]
   ]; 

The instruction is as follows: The function rearrange[expr,listOfFinalPositions] rewrites a simple sum in the order prescribed by the list entitled "listOfFinalPositions." Arguments: expr is the expression with the head Plus. listOfFinalPositions is a list. It is important that its length must be equal to the length of the expression. It indicates the positions that the terms of the sum must take in the end. For example, for the expression a+b+c the listOfFinalPositions {2,3,1} means that a must go the second position, b - to the third, and c - to the first one resulting in c+a+b. The function wraps the result by the HoldForm function to forbid an undesired reordering. Therefore, to use the expression further one needs to apply ReleaseHold

If the function rearrange should be applied to a more complex expression to rearrange a part of it, map it onto the necessary part using the MapAt function (as it is shown below).

Here is how one can operate with your example:

eq = D[y'[t] - k[y[t]]*y[t], t] == 0:

MapAt[rearrange[#, {2, 3, 1}] &, eq, {1}] // TraditionalForm

enter image description here

To address your second question, you can apply first the replacement k[y[t]]->k[y] as follows:

eq2 = eq /. k -> (k[#] &) /. y[t] -> y

Alternatively you can replace like this: {k[y[t]] -> k[y], k'[y[t]] -> k'[y]}. Then apply the rearrange function:

MapAt[rearrange[#, {2, 3, 1}] &, eq2, {1}] // TraditionalForm

enter image description here

If your reason is the second one, that is, you will make your calculations further, I recommend that you make the rearrangements separately. It can be achieved simply by giving a rearranged expression a certain name different from that of the original expression.

Like this, you do not use the rearranged expression for further calculations of whatever sort.

Alternatively, you can, of course, apply the function ReleaseHold that removes all functions of the Hold family. However, during long and complex calculations you can forget to remove them and get errors.

The same is true for the replacement {k[y[t]] -> k[y], k'[y[t]] -> k'[y]}. Namely, you can replace back

eq2 /. {k[y] -> k[y[t]], k'[y] -> k'[y[t]]}

enter image description here

But do not forget to do that, otherwise, it will cause errors.

Have fun!

$\endgroup$
1
  • $\begingroup$ Thank you so much! your answer is very helpful and comprehensive! $\endgroup$
    – Ben
    Dec 3, 2021 at 14:58
0
$\begingroup$

Try this:

Collect[D[k[θ[t]]*θ[t] - θ'[t], t] == 0, {D[θ[t], t], D[θ[t], {t, 2}]}]
$\endgroup$
3
  • 1
    $\begingroup$ oh, good, Collect is a good choice, the output order is from lower to higher. can I invert it? thanks a lot! $\endgroup$
    – Ben
    Dec 3, 2021 at 8:54
  • 1
    $\begingroup$ If you move $\theta^{''}$ to the front by hand and hit enter, you'll see that Mathematica will put it back to the end. That is how the front end likes to format it. Better give up and do not worry about it as order does not matter, else you'll be fighting Mathematica all the time. $\endgroup$
    – Nasser
    Dec 3, 2021 at 9:20
  • $\begingroup$ @Nasser, Thank you for comments! Change orders can make output better to understand and presentation, Alexei post a answer can solve this problem, we can learn from it and apply it to deal with the $\endgroup$
    – Ben
    Dec 3, 2021 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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