I want to approximate the solution to ODE of form
$(k(x)u'(x))'=f(x)$
I've done it one way, by differentianting the equation and approximating each derivative by central differences.
$u''(x)=\frac{u(x+h)+u(x-h)-2u(x)}{h^2}+O(h^2)$
$u'(x)=\frac{u(x+h)-u(x-h)}{2h}+O(h^2)$
The other way goes like this:
If we are appoximating the solution to ODE above on an interval [a,b], we have the discrete points $x_i=a+ih$, where $h=\frac{b-a}{n+1}$, $n$ number of points in between $a$ nad $b$. We add also the midpoints: $x_{i\pm\frac{1}{2}}$. First I have to approximate $k(x)u'(x)$ in the midpont. Then, using the gotten approximations I need to approximate $(k(x)u'(x))'$ in the nodes $x_i$, also with central differences.
How I began:
since $u'(x)=\frac{u(x+\frac{h}{2})-u(x-\frac{h}{2})}{h}+O(h^2)$ and I know the value of $k(x)$, then $k(x)u'(x)\approx k(x)\frac{u(x+\frac{h}{2})-u(x-\frac{h}{2})}{h} \Rightarrow$
$k(x_{i+\frac{1}{2}})u'(x_{i+\frac{1}{2}}) \approx k(x_{i+\frac{1}{2}})\frac{u(x_{i+1})-u(x_{i})}{h}$ $k(x_{i-\frac{1}{2}})u'(x_{i-\frac{1}{2}}) \approx k(x_{i-\frac{1}{2}})\frac{u(x_{i})-u(x_{i-1})}{h}$
Finally,
$(k(x)u'(x))'\big|_{x=x_i}\approx \frac{k(x_{i+\frac{1}{2}})u'(x_{i+\frac{1}{2}})-k(x_{i-\frac{1}{2}})u'(x_{i-\frac{1}{2}})}{h}=\dots=\frac{k(x_{i+\frac{1}{2}})(u(x_{i+1})-u(x_i))-k(x_{i-\frac{1}{2}})(u(x_i)-u(x_{i-1}))}{h^2}$
I'm not sure whether it is correctly done. Can I approximate (k(x)u'(x))' like I have, since it is a product of functions?
You are on the right track! Your method follows exactly the standard central difference method.
Your result should be correct, but I am afraid there is some typo in your last line. The correct result should read $$ \left(k(x)u'(x)\right)'\big|_{x=x_j}=\frac{k(x_{j+1/2})\left[u(x_{j+1})-u(x_j)\right]-k(x_{j-1/2})\left[u(x_j)-u(x_{j-1})\right]}{h^2}+O(h^2). $$