I want to solve the following differential equation:

y''[x]+a*y[x]=-b*u[x], y[0]==0, y'[0]==0

for y(x). u[x] is a vector, getting its values from a txt document (I have one column with x values (x starting from 0 to 20 with a 0.02 step) and another one with u(x) values). I would appreciate any help.

  • $\begingroup$ you need to set u[x] as a function of x and then substitute that in the DE. can you provide the values of x and u(x)? $\endgroup$ Commented Nov 8, 2014 at 17:08
  • $\begingroup$ @Algohi u[x] is actually a vector of numerical data. For each x, u[x] is a random number. The values of x are from 0 to 20 with a 0.02 step (0,0.02,0.04,0.06,...,19.98,20). The values of u[x] are random, it doesn't really matter what you use. $\endgroup$
    – epl
    Commented Nov 8, 2014 at 17:21

1 Answer 1


Mathematica finds the general solution for any u[x] simply by using DSolve:

sol = DSolve[{y''[x] + a*y[x] == -b*u[x], y[0] == 0, y'[0] == 0}, y[x], x];

yy[x_] = y[x] /. First[sol] /. {K[1] -> t, K[2] -> t}

$-\text{Cos}\left[\sqrt{a} x\right] \int_1^0 \frac{b \text{Sin}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt+\text{Cos}\left[\sqrt{a} x\right] \int_1^x \frac{b \text{Sin}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt-\left(\int_1^0 -\frac{b \text{Cos}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt\right) \text{Sin}\left[\sqrt{a} x\right]+\left(\int_1^x -\frac{b \text{Cos}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt\right) \text{Sin}\left[\sqrt{a} x\right]$

To improve readability we have changed the symbols used for the integration variables from K[1] and K[2] to t.

This can be simplified further, but Mathematica didn't see that the intergals can be joined to one interval from 0 to x.

So we do it by hand. The solution is then

$y1[x] = \text{Cos}\left[\sqrt{a} x\right] \int_0^x \frac{b \text{Sin}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt -\text{Sin}\left[\sqrt{a} x\right] \int_0^x \frac{b \text{Cos}\left[\sqrt{a} t\right] u[t]}{\sqrt{a}} \, dt;$

For your convenience here's the (not very pleasant looking) MMA form

y1[x_] = Cos[Sqrt[a]*x]*Integrate[(b*Sin[Sqrt[a]*t]*u[t])/Sqrt[a], {t, 0, x}] - 
   Sin[Sqrt[a]*x]*Integrate[(b*Cos[Sqrt[a]*t]*u[t])/Sqrt[a], {t, 0, x}]; 

y1[x] in fact solves the ODE

D[y1[x], {x, 2}] + a y1[x] + b u[x] // FullSimplify

(* Out[36]= 0 *)

as well as the initial conditions

{y1[0], y1'[0]}

(* Out[39]= {0, 0} *)

Remark 1: If your equation is to be interpreted as a vector equation, then the solution holds for each component separately (as there are no "crossover" terms).

Remark 2: If a>0 the ODE is the equation of motion for a harmonic oscillator with eigenfreqency Sqrt[a] subject to an external force b*u[x]. The variable x is then the time.


  • $\begingroup$ What confuses me is that u(x) is not unknown, for every x I have a value of u and for that x I must find y. This is why I cannot fully understand the integral in the solution. $\endgroup$
    – epl
    Commented Nov 8, 2014 at 19:56
  • $\begingroup$ @ epl: if you have a value of u for each value of x then you have the function u(x). As these seem to be discrete you could either first make an interpolation to find u(x) and then enter it into the integral or else write down a difference equation. In any case, please provide a concrete example of all the parameters (a, b, u) of your problem. $\endgroup$ Commented Nov 8, 2014 at 21:32
  • $\begingroup$ @ epl: 1) the simplest approach is to replace the integrals by sums. 2) It is also not very complicated to solve the difference equations. $\endgroup$ Commented Nov 9, 2014 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.