18
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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]. $\endgroup$
    – march
    Commented Jun 19, 2015 at 21:09

2 Answers 2

15
$\begingroup$
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.

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Commented Jun 27, 2015 at 4:50
15
$\begingroup$

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}]

enter image description here

$\endgroup$
7
  • $\begingroup$ 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? $\endgroup$
    – Nasser
    Commented Jun 19, 2015 at 22:07
  • $\begingroup$ @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. $\endgroup$
    – Karsten7
    Commented Jun 19, 2015 at 22:23
  • 1
    $\begingroup$ I think the reference that @Nasser was starting with is this answer. $\endgroup$
    – Jens
    Commented Jun 20, 2015 at 17:23
  • $\begingroup$ 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 $\endgroup$
    – Emad
    Commented Jun 26, 2015 at 3:38
  • $\begingroup$ @Emad I added one way to find the solution numerically. $\endgroup$
    – Karsten7
    Commented Jun 26, 2015 at 20:45

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.