# A problem when solving an ODE $-f'' + \left ( 1 - \frac{1}{k} \right )f=0$

I would like to solve an ODE $$-f'' + \left ( 1 - \frac{1}{k} \right )f = 0$$ on the interval $$(0, 1)$$ where $$k \in (0, 1)$$ is a constant. I use the command

sol = DSolve[{- f''[x] + (1 - 1 / k) * f[x] == 0}, f[x], {x, 0, 1}];
sol = Assuming[k > 0 && k < 1, FullSimplify[sol]]


Mathematica returns

The expression $$\sqrt{\frac{-1+k}{k}}$$ does not make sense to me because $$\frac{-1+k}{k} < 0$$ for $$k \in (0, 1)$$. Could you elaborate on this issue?

• The expression makes mich sense, because Sqrt[(-1+k)/k]->I /omega transforms the ode in a well known form omega^2 f''[x]+f[x]==0 Commented Dec 11, 2023 at 21:05
• @UlrichNeumann Could you explain what Sqrt[(-1+k)/k]->I omega means? Commented Dec 11, 2023 at 21:06
• It's a simple substitution Sqrt[(-1+k)/k]=:I omega  Commented Dec 11, 2023 at 21:08
• You are using DSolve and not NDSolve. So writing {x, 0, 1} is not needed and does not do anything. Just write x. Do you know of a case where DSolve result was different when writing {x,from,to} vs. just x? I know of no such case. Commented Dec 11, 2023 at 21:37
• To elaborate on what @UlrichNeumann said,$$e^{\pm \sqrt{(1-k)/k} x} = e^{\pm i \sqrt{(k-1)/k} x} = \cos \left( \sqrt{\frac{k-1}{k}} x \right) \pm i \sin \left( \sqrt{\frac{k-1}{k}} x \right)$$and if you choose complex values of $c_1$ and $c_2$ in your solution (specifically, $c_2 = c_1^*$) your solution becomes linear combination of a sine and a cosine. Commented Dec 11, 2023 at 22:12

I guess this is the form you are looking for...?

sol = DSolve[{-f''[x] + (1 - 1/k)*f[x] == 0}, f[x], {x, 0, 1}];
sol = First[f[x] /. Assuming[k > 0 && k < 1, FullSimplify[sol]]];
PowerExpand[ExpToTrig[sol], Assumptions -> 0 < k < 1];
Collect[%, {Cos[_], Sin[_]}, Factor]


(C[1] + C[2]) Cos[Sqrt[-1 + 1/k] x] + I (C[1] - C[2]) Sin[Sqrt[-1 + 1/k] x]


$$(c_1+c_2) \cos \left(x \sqrt{\frac{1}{k}-1}\right)+i (c_1-c_2) \sin \left(x \sqrt{\frac{1}{k}-1}\right)$$

• I meant to solve my ODE and present the solution in a familiar form. I could not see my ODE in your answer. Commented Dec 11, 2023 at 22:09
• The sol is output of your code. Commented Dec 11, 2023 at 22:10

You probably want the Trig solution to your problem

sol = DSolve[-f''[x] + (1 - 1/k) f[x] == 0, f[x], x] // Flatten
(* {f[x] -> C[1] E^((Sqrt[k - 1] x)/Sqrt[k]) + C[2] E^(-((Sqrt[k - 1] x)/Sqrt[k]))} *)


Convert the C's to forms we can manipulate and get the argument of the Sqrt positive based on your range of k

f[x_] = f[x] /. sol /. {C[1] -> c1, C[2] -> c2, Sqrt[k - 1] -> I Sqrt[1 - k]}
(* c1 E^((I Sqrt[1 - k] x)/Sqrt[k]) + c2 E^(-((I Sqrt[1 - k] x)/Sqrt[k])) *)


Convert to trig form

ExpToTrig[f[x]] // Simplify
(* (c1 + c2) Cos[(Sqrt[1 - k] x)/Sqrt[k]] + I (c1 - c2) Sin[(Sqrt[1 - k] x)/Sqrt[k]] *)


And simplify the constants.

f[x_] = % /. {c1 + c2 -> c1, I (c1 - c2) -> c2}
(* c1 Cos[(Sqrt[1 - k] x)/Sqrt[k]] + c2 Sin[(Sqrt[1 - k] x)/Sqrt[k]] *)