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}]
DSolve
in the help menus. I think it would be easiest to get the analytic expression fory[x]
first,Integrate
the resulting expression from 0 to 2, thenSolve
the set of simultaneous equations (your boundary conditions) forC[1]
andC[2]
. $\endgroup$