2

My input is the following:

sol = DSolve[ (1 - 2/x) y''[x] + (2/x)*(1 - 1/x)*y'[x] - (2/x^2) y[x] == -1/
   x^7, y[x], x ]

Mathematica returns:

{{y[x] -> (-1 + x) C[1] + 
    C[2] (-1 + (-1 + x) (-(1/2) Log[2 - x] + Log[x]/2)) + (
    36 + 20 x + 10 x^2 + 30 x^4 - 15 x^4 Log[2 - x] + 
     15 x^5 Log[2 - x] + 15 x^4 Log[x] - 15 x^5 Log[x])/(1152 x^4)}}

In fact, I only need this part: $(36+20x+10 x^2)/(1152x^4)$, which is a simpler solution!

My question is about how to force Mathematica to not show those $\log$ terms? Because the desired solution is hiding.. it is hard to pick it out.

xzczd
  • 65,995
  • 9
  • 163
  • 468
Sven2009
  • 205
  • 1
  • 5

4 Answers4

6

DSolve yields a general solution written in terms of constants $\;c_1$ and $c_2$. We could search for appropriate initial conditions on the general solution. There is another more straightforward way, simply to choose appropriate condition on the constants. Let's rewrite the general solution

Collect[
  DSolveValue[ (1 - 2/x)y''[x] + 2/x (1 - 1/x) y'[x] - (2/x^2) y[x] == -1/x^7, 
               y[x], x], {Log[x], Log[2 - x]}, Simplify]
 1/576 (15 + 18/x^4 + 10/x^3 + 5/x^2 + 576 (-1 + x) C[1] - 576 C[2]) - 
 1/384 (-1 + x) (-5 + 192 C[2]) Log[2 - x] + 
 1/384 (-1 + x) (-5 + 192 C[2]) Log[x]

and now we can see that it is sufficient to set C[2]-> 5/192:

sc=DSolveValue[(1 - 2/x) y''[x] + (2/x)(1 - 1/x) y'[x] - (2/x^2) y[x] == -1/x^7,
             y[x], x] /. C[2] -> 5/192 // Simplify
 1/(32 x^4) + 5/(288 x^3) + 5/(576 x^2) - C[1] + x C[1]

and this is an exact solution.

Having one-parameter family of solutions free of logarithmic terms we can also prescribe initial conditions. In general we need two conditions, e.g. value of the solution and its derivative, however to get the specific class of solutions we can set only one condition while another has to be calculated from the formula found above, e.g. for $x=1\;$ sc /. x -> 1 yields 11/192 and so:

Collect[
  DSolveValue[{(1 - 2/x) y''[x] + 2/x (1 - 1/x) y'[x] - (2/x^2) y[x] == -1/x^7, 
               y[1] == 11/192, y'[1] == c}, y[x], x],
          x, Simplify]
-(7/36) - c + 1/(32 x^4) + 5/(288 x^3) + 5/(576 x^2) + (7/36 + c) x
Artes
  • 57,212
  • 12
  • 157
  • 245
  • Setting C[2]-> 5/192 with no explanation seems ad hoc! – Hans Olo Aug 25 '22 at 12:49
  • 1
    @HansOlo The factor (-5 + 192 C[2]) in front of each Log[] seems to make C[2] -> 5/192 need no further explanation. – Michael E2 Aug 25 '22 at 12:53
  • 1
    I've rewritten the general solution to figure out what we need to get rid of logarithmic terms and setting C[2]-> 5/192 is just a full explanation. – Artes Aug 25 '22 at 12:53
4

Having both solutions sol1 and sol2, substitute placeholders for the Log's to get rid of and SolveAlways for all x.

sol1 = (-1 + x) C[1] + 
  C[2] (-1 + (-1 + x) (-(1/2) Log[2 - x] + Log[x]/2)) + (36 + 20 x + 
     10 x^2 + 30 x^4 - 15 x^4 Log[2 - x] + 15 x^5 Log[2 - x] + 
     15 x^4 Log[x] - 15 x^5 Log[x])/(1152 x^4);

sol2 = (36 + 20 x + 10 x^2)/(1152 x^4);

sa = First@ SolveAlways[sol1 == sol2 /. {Log[2 - x] -> log2x, Log[x] -> logx}, x]

(* {C[1] -> 0, C[2] -> 5/192} *)

sol2 == sol1 /. sa // Simplify

(* True *)

Akku14
  • 17,287
  • 14
  • 32
3

Assuming you need the dominant terms near $x=0$, one may do a series expansion:

Series[sol[[1, 1, 2]], {x, 0, -1}]

which returns the approximation: $$\frac{1}{32 x^4}+\frac{5}{288 x^3}+\frac{5}{576 x^2}+O\left(x^0\right) $$

Plotting (eg for $C[1]=1$, $C[2]=2$) we see that the approximation is indeed quite good for small $x$:

enter image description here

Hans Olo
  • 1,843
  • 2
  • 9
  • 15
3

Others beat me to posting the first two ways I thought of, so here's...

Another way:

ode = -(2y[x]/x^2) + 2 (1-1/x) y'[x]/x + (1-2/x) y''[x] == -1/x^7;
sol = DSolveValue[ode, y[x], x];
basis = D[sol, {Array[C, 2]}]; (* = {x-1, logs} *)
uode = ode /. y -> Function[x, basis[[2]] u[x] // Evaluate];
usol = DSolve[uode, u, x];
basis[[2]] u[x] /. First[usol] /. C[2] -> 0 // 
 Collect[#, C[1], Together] &
(*
  (18 + 10 x + 5 x^2)/(576 x^4) + 1/4 (1 - x) C[1]
*)

Instead of lines 4 and 5 above, you can use the new function:

usol = DSolveChangeVariables[Inactive[DSolve][ode, y, x],
  u, x, y[x] == basis[[2]] u[x]] // Activate

And another way:

C[2] -> Limit[C[2] /. First@Solve[sol == 0, C[2]], x -> 2]
sol /. % // Simplify // Collect[#, C[1], Together] &
(*
  C[2] -> 5/192
  (18 + 10 x + 5 x^2)/(576 x^4) + (-1 + x) C[1]
*)

And another way:

DeleteCases[Expand[sol /. x -> 1/2], t_ /; FreeQ[t, _Log]] // 
 Solve[# == 0] &
(*
  {{C[2] -> 5/192}}
*)

And another way:

DSolveValue[{ode, y[2] == 0}, y[x], x]
DSolveValue[{ode, 
  y[2] == -Coefficient[%, x]/Coefficient[basis[[1]], x]}, y[x], x]
(*
  (144 + 80 x + 40 x^2 + 29 x^4 - 29 x^5)/(4608 x^4)
  (18 + 10 x + 5 x^2)/(576 x^4)
*)

And another way:

DSolveValue[{ode, y[2] == c}, y[x], {x, 0, 2}]
% /. First@
   Solve[Coefficient[%, 
      x^Exponent[DeleteCases[Expand@%, t_ /; FreeQ[t, c]], x]] == 0, 
    c] // Simplify
(*
  (144 + 80 x + 40 x^2 +
    (29 - 4608 c) x^4 + (-29 + 4608 c) x^5)/(4608 x^4)
  (18 + 10 x + 5 x^2)/(576 x^4)
*)

And another way:

The series solution has the following recurrence:

2 (1 + n)^2 a[1 + n] == (-1 + n) (2 + n) a[n]

By inspection, the constant term turns out to be free. If we set it to zero, all the higher-order terms vanish and we have a Laurent polynomial solution.

Subtract @@ ode /. y -> Function[x, Sum[a[k] x^k, {k, -4, 0, 1}]] // 
     CoefficientList[
       Replace[Expand[#], {c_. x^k_ :> c*x[k], c_. x :> c*x[1], 
         c_ :> c*x[0]}, 1], Table[x[k], {k, -7, 2, 1}]] & // Flatten //
    DeleteCases[0] // 
  Collect[#, Table[a[k], {k, -4, 0, 1}], Factor] & //
 Sum[a[k] x^k, {k, -4, 0, 1}] /. First@Solve[# == 0] &
(*
  1/(32 x^4) + 5/(288 x^3) + 5/(576 x^2)
*)

And another way:

Limit[sol/Log[2 - x], x -> 2] == 0 // Solve
(*
  {{C[2] -> 5/192}}
*)

And another way:

x0 = Pi;(* any value in the domain of ode will do *)
yyp[x_ -> x0_] := NestList[D[#, x] &, #, 1] /. {x -> x0} &;
Thread[Array[C, 2] ->
  LinearSolve[
   yyp[x -> x0][basis],
   yyp[x -> x0][(18 + 10 x + 5 x^2)/(576 x^4) - sol] /. _C -> 0]
 ]
(*
  {C[1] -> 0, C[2] -> 5/192}
*)

And another way:

g = 1/(2 Pi) # Exp[I t]/4 /. {x -> 2 + Exp[I t]/4, _C -> 0} &;
Integrate[g@D[sol, x], {t, 0, 2 Pi}] +
  C[2] Integrate[g@D[basis[[2]], x], {t, 0, 2 Pi}] //
 Solve[# == 0] &
(*
  {{C[2] -> 5/192}}
*)

And another way:

Related to the preceding method, but simpler.

Residue[D[sol, x], {x, 2}] == 0 // Solve
(*
  {{C[2] -> 5/192}}
*)

Have fun. :)

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Many of the methods above target the regular singular point x == 2. Annihilating the logarithmic term there , which is essentially what C[2] -> 5/192 does, turns out annihilate Log[x], too. (In a way, that "must" happen in this case, because the OP knows there is such a solution and there is a polynomial basis function.) – Michael E2 Aug 26 '22 at 14:11