# collecting terms with different order

I am using homotopy perturbation method to solve nonlinear ODE.

The first step is to introduce P parameter and collect different order of P.

The code is like this:

Collect[(1 -
p) ((((1 +
De^2 (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] + p^3 Derivative[1][u3])^2)^(
1/2 (-3 + n)) (1 +
De^2 n (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] +
p^3 Derivative[1][u3])^2) (Derivative[2][u0] +
p Derivative[2][u1] + p^2 Derivative[2][u2] +
p^3 Derivative[2][u3])) (Derivative[1][u0] +
p Derivative[1][u1] + p^2 Derivative[1][u2] +
p^3 Derivative[1][u3])) + (1 +
De^2 ((Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] + p^3 Derivative[1][u3]))^2)^((
n - 1)/2) (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] +
p^3 Derivative[1][u3]) (Derivative[2][u0] +
p Derivative[2][u1] + p^2 Derivative[2][u2] +
p^3 Derivative[2][u3])) +
p (-Ha^2 (u0 + p u1 + p^2 u2 +
p^3 u3) + (((1 +
De^2 (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] + p^3 Derivative[1][u3])^2)^(
1/2 (-3 + n)) (1 +

De^2 n (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] +
p^3 Derivative[1][u3])^2) (Derivative[2][u0] +
p Derivative[2][u1] + p^2 Derivative[2][u2] +
p^3 Derivative[2][u3])) (Derivative[1][u0] +
p Derivative[1][u1] + p^2 Derivative[1][u2] +
p^3 Derivative[1][u3])) + (1 +
De^2 ((Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] + p^3 Derivative[1][u3]))^2)^((
n - 1)/2) (Derivative[1][u0] + p Derivative[1][u1] +
p^2 Derivative[1][u2] +
p^3 Derivative[1][u3]) (Derivative[2][u0] +
p Derivative[2][u1] + p^2 Derivative[2][u2] +
p^3 Derivative[2][u3])), p]


The problem is that don't get anything back

What do you think?

• What do you mean by "The problem is that don't get anything back"? I get an output when I run your code. What are you expecting? There are p's still inside brackets because Mathematica can't expand ( stuff )^((n-1)/2) unless you supply an actual integer value for n. Is that the issue? Aug 31, 2017 at 19:03
• @march I mean that I want it to collect expression with p^0,p^1,p^2. Aug 31, 2017 at 19:08
• Mathematica won't do that for you, even if you specify that n is an integer (which I assume it is?). Either do this for different actual values of n or use Series to have Mathematica compute a power series in p before collecting. This will result in pretty large expressions though. Aug 31, 2017 at 20:05
• @march Ah, I finally found the problem. Collect[expr ,p] appears to return expr unchanged when expr contains a rational function of p (other than p^n_). E.g., Collect[(q + p) (1 + p)^(n - 1), p, foo] returns its input unchanged. IMHO, this seems somewhat inconsistent (it carries non-rational functions along, like in Collect[(1+p) f[p], p], and even carries along expressions which will be rational for certain values of the power, like in Collect[(1+p)(p+q)^n, p]), but I suppose some choice had to be made. Sep 1, 2017 at 19:49
• @jjc385. I suspected something of that sort, although admittedly it is a little inconsistent. (It looks like OP needs Series anyway, though!) Sep 8, 2017 at 23:08