# Differential equation with piecewise coefficient

I have this equation and I need to solve it analytically by Mathematica https://i.stack.imgur.com/fCnxk.png

I tried the following

k = 32;
eqn = {(u^\[Prime]\[Prime])[x] == Piecewise[{{1/2 k^2 Cos[2 \[Pi] k x], 0 <= x < 0.1}, {( k^2 Cos[2 \[Pi] k x])/1.5, 0.1 <= x < 0.2}, {(
k^2 Cos[2 \[Pi] k x])/0.75, 0.2 <= x < 0.3}, {(
k^2 Cos[2 \[Pi] k x])/1.5, 0.3 <= x < 0.4}, {(
k^2 Cos[2 \[Pi] k x])/3.75, 0.4 <= x < 0.5}, {(
k^2 Cos[2 \[Pi] k x])/0.75, 0.5 <= x < 0.6}, {(
k^2 Cos[2 \[Pi] k x])/1.25, 0.6 <= x < 0.7}, {(
k^2 Cos[2 \[Pi] k x])/0.75,
0.7 <= x < 0.8}, {1/2 k^2 Cos[2 \[Pi] k x],
0.8 <= x < 0.9}, {k^2 Cos[2 \[Pi] k x], 0.9 <= x < 1}}],
u[0] == 0.1, u[1] == 0.2}

sol = DSolve[eqn, u[x], x]

• What have you tried? – corey979 Apr 8 '18 at 22:09
• I am sorry I am new to this forum so how can I add my code properly – Amr Saleh Apr 8 '18 at 22:17
• I added my code – Amr Saleh Apr 8 '18 at 22:25

You simply need to enter the \[Prime]s without ^, that means

k = 32;
eqn = {(u'')[x] ==
Rationalize[Piecewise[{{1/2 k^2 Cos[2 π k x],
0 <= x < 0.1}, {(k^2 Cos[2 π k x])/1.5,
0.1 <= x < 0.2}, {(k^2 Cos[2 π k x])/0.75,
0.2 <= x < 0.3}, {(k^2 Cos[2 π k x])/1.5,
0.3 <= x < 0.4}, {(k^2 Cos[2 π k x])/3.75,
0.4 <= x < 0.5}, {(k^2 Cos[2 π k x])/0.75,
0.5 <= x < 0.6}, {(k^2 Cos[2 π k x])/1.25,
0.6 <= x < 0.7}, {(k^2 Cos[2 π k x])/0.75,
0.7 <= x < 0.8}, {1/2 k^2 Cos[2 π k x],
0.8 <= x < 0.9}, {k^2 Cos[2 π k x], 0.9 <= x < 1}}],
u[0] == 0.1, u[1] == 0.2}]
sol = DSolve[eqn, u[x], x];

Plot[Evaluate[PiecewiseExpand[u[x] /. sol[[1]]]], {x, 0, 1}]


Rationalize converts this to exact numbers, in order to make it easier for DSolve.

• I got this plot but when I solved it using finite element I got a different plot. I compared it to my friend solution by matlab. I found this plot is not the same but the finite element one is correct – Amr Saleh Apr 8 '18 at 22:43
• How did you validate that? With these high oscillations, you need quite fine elements in order to reproduce the wiggles with FEM. D[PiecewiseExpand[u[x] /. sol[[1]]], x, x] returns the right hand side exactly and satisfies also the boundary conditions. – Henrik Schumacher Apr 8 '18 at 22:50
• I validated it with my friend I used 10000 elements to be fine enough. you are correct I also did that and the solution returns the right hand side – Amr Saleh Apr 8 '18 at 22:55
• Ah, now I see it: Your strong formulation is wrong. You have to apply the product rule as $A_1$ is not a constant function: It is piecewise constant, thus its derivative is a linear combinations of Dirac distributions. So you get also term of the form $A_1'(t) \, u'(t) = -\sum_i \delta_{t_i}(t) \, u'(t)$. You may use DiracDelta for formulating this. – Henrik Schumacher Apr 8 '18 at 23:00
• I got your idea but I want to ask what is the value i – Amr Saleh Apr 9 '18 at 0:34