# Eliminate complex answer to differential equation

I've tried the following:

In[32]:= DSolve[{y'[x] == (x + 2)/(x*(3 - x)), y[4] == 2}, y[x], x]

Out[32]= {{y(x)->1/3 (-5 log(3-x)+2 log(x)+5 I \[Pi]+6-2 log(4))}}

In[57]:= z[x_] := 2/3*Log[x] - 5/3*Log[x - 3] + (2 - 2/3*Log[4])

In[58]:= z[4]

Out[58]= 2

In[59]:= D[z[x], x]

Out[59]= 2/(3 x)-5/(3 (x-3))

In[60]:= Simplify[-(5/(3 (-3 + x))) + 2/(3 x)]

Out[60]= (x+2)/(3 x-x^2)


In[32] produces an answer with a complex number. In[57] is the answer I generated by hand and you can see that it checks.

How do I use DSolve to produce the answer In[57]?

-
Out[32] looks strange. Log without initial uppercase letter, no square function brackets. Is this really the output you got? Or perhaps TraditionalForm? – Sjoerd C. de Vries Jan 22 '14 at 7:47

Er… I'm not familiar with complex function, but I guess your answer by hand is also generated in field of real number, right? Then since it involves the term Log[x-3], I think you've actually used the assumption:

$$x-3>0$$

With this assumption, Mathematica will give the same answer:

z2[x_] =Simplify[y[x] /. DSolve[{y'[x] == (x + 2)/(x(3 - x)), y[4] == 2}, y, x], x > 3][[1]]
z2[x] == z[x] // Simplify

1/3 (6 - Log[16] - 5 Log[-3 + x] + 2 Log[x])
True

-

     ds1 = DSolve[{y'[x] == (x + 2)/(x*(3 - x)), y[4] == 2}, y[x], x][[1, 1,
2]]

(*  1/3 (6 + 5 I \[Pi] - 2 Log[4] - 5 Log[3 - x] + 2 Log[x])  *)


The domain of the function in question is x>3. Indeed, let us try to plot in in the interval from -1 to 5: Plot[ds, {x, -2, 5}]. This is what you get: .

The function is not defined, where the plot contains nothing. Now, one can apply Simplify:

    ds2=Simplify[ds1, x > 3]

(* 1/3 (6 - Log[16] - 5 Log[-3 + x] + 2 Log[x]) *)


which answers the question. The expression may be written in a still more compact way by using the function collectLog below:

    collectLog[expr_] := Module[{rule1a, rule1b, rule2, g, a, b, x},
rule1a = Log[a_] + Log[b_] -> Log[a*b];
rule1b = Log[a_] - Log[b_] -> Log[a/b];
rule2 = x_*Log[a_] -> Log[a^x];
g[x_] := x /. rule1a /. rule1b /. rule2;
FixedPoint[g, expr]]


Then

    collectLog[ds2]

(* 1/3 (6 + Log[x^2/(16 (-3 + x)^5)]) *)

-