# Solve differential equation using a integral form boundary condition

I have a second order differential equation and I want to solve it analytically (DSolve) and numerically (NDSolve) with following boundary conditions.

y''[x] - y[x] == (x^2)


Boundary conditions:

y[0] == 1
Integrate[y[x], {x, 0, 2}] == 5


How can I implement an integral form boundary condition for solving this differential equation?

• First look at DSolve in the help menus. I think it would be easiest to get the analytic expression for y[x] first, Integrate the resulting expression from 0 to 2, then Solve the set of simultaneous equations (your boundary conditions) for C[1] and C[2]. – march Jun 19 '15 at 21:09

s = DSolveValue[{y''[x] - y[x] == (x^2), y[0] == 1}, y[x], x];
First@Solve[Integrate[s, {x, 0, 2}] == 5, C[1]];
s /. %
{*  E^-x (3 - 2 E^x - (9 + 26 E^2)/(3 (-1 + E^2)^2) + (E^(2 x) (9 + 26 E^2))/
(3 (-1 + E^2)^2) - E^x x^2) *}


Alternative solution

The answer by Karsten7 suggests the following: With y[x] replaced by g'[x],

D[DSolveValue[{g'''[x] - g'[x] == x^2, g'[0] == 1, g[2] - g[0] == 5},
g[x], x], x] // Simplify


which gives the same result as above.

A numerical solution, as request by the OP in a comment, can be obtained from the second solution above by replacing DSolveValue by NDSolveValue and assigning a value to g[0]. (Any value will do, because the subsequent differentiation eliminates it.)

s = NDSolveValue[{g'''[x] - g'[x] == x^2, g[0] == 0, g'[0] == 1, g[2] - g[0] == 5}, g', x]


which gives a curve identical to the one obtained previously.

• I saw your comment asking for an NDSolve solution only now. (Because you attached the comment to Karsten7's answer, it went only to him.) I have added a solution closely patterned after my second DSolve solution. In fact, the two can be written so that they differ only by the substitution of NDSolve for DSolve. Best wishes. – bbgodfrey Jun 27 '15 at 4:50

## Analytical Solution

A corrected version of the now deleted answer by Nasser, that uses a trick explained in this answer by Jens:

Clear[f, if, y, x, g];
f /: Integrate[f[x_], x_] := if[x];
SetAttributes[if, {NumericFunction}];
sol = Integrate[f[x], {x, 0, 2}];
bc2 = sol == 5 /. if -> g

-g[0] + g[2] == 5


With g beeing the antiderivative of y.

ode = g'''[x] - g'[x] == (x^2);
bc1 = g'[0] == 1;
sol = g[x] /. First@DSolve[{ode, bc1, bc2}, g[x], x]

y1[x_] = D[sol, x] // FullSimplify  (* y[x]=g'[x] *)

1/3 ((E^-x (E^2 (-44 + 9 E^2) + E^(2 x) (9 + 26 E^2)))/(-1 + E^2)^2 - 3 (2 + x^2))


## Numerical Solution

sol2[bc2_, {xmin_, xmax_}] :=
NDSolveValue[{y''[x] - y[x] == (x^2), y'[0] == bc2, y[0] == 1}, y, {x, xmin, xmax}];
int[bc2_?NumericQ] := NIntegrate[sol2[bc2, {0, 2}][x], {x, 0, 2}];
y2 = sol2[NMinimize[(int[bc2V] - 5)^2, bc2V][[-1, -1, -1]], {-3, 3}]

Plot[{y1[x], y2[x]}, {x, -3, 3}]


• Ok, I agree Mathematica result from my method is not correct, but my question is, what did I do wrong? where in the steps I did the error was? – Nasser Jun 19 '15 at 22:07
• @Nasser I hope my edit makes it more clear, that you changed from y being the original function to y` being the antiderivative of the original function in the first part, but didn't make that change in the second part, and therefore missed to take the derivative. – Karsten 7. Jun 19 '15 at 22:23
• I think the reference that @Nasser was starting with is this answer. – Jens Jun 20 '15 at 17:23
• Thanks guys. These are the answers are related to DSolve. What about NDSolve?? Assume that there is non-homogeneous second order differential equation with the same boundary condition (y''[x] - Cos[x] y[x] == x^2). Please help me on that, too – Emad Jun 26 '15 at 3:38
• @Emad I added one way to find the solution numerically. – Karsten 7. Jun 26 '15 at 20:45