Solve differential equation using a integral form boundary condition
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.
Addendum
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.
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}]