2
$\begingroup$

How do I use NIntegrate solutions to Differential Equations?

IntegratedDifferenceFunction[I_, O_] := 
 NIntegrate[I[U] - O[U], {U, 0, 2*Pi}]

m = DSolve[y''[x] + y[x] == 0, y[x], x]
n = DSolve[y''[x] + y'[x] == 0, y[x], x]

IntegratedDifferenceFunction[m, n]

I want to compute the difference between two Ordinary Differential Equation Solutions from 0 to 2*Pi, It can be any ODE's. Is there also a way to apply the function for Numerical Solutions that were solved using NDSolve?

 m = NDSolve[y''[x] + y[x] == 0, y[x], {x,0,2*Pi}]
 n = NDSolve[y''[x] + y'[x] == 0, y[x], {x,0,2*Pi}]
$\endgroup$
2

3 Answers 3

5
$\begingroup$

For numerical methods, you need functions/expressions that yield numerical values (without symbolic parameters). Your IntegratedDifferenceFunction is defined for I and O to be functions, not algebraic expressions. To match it, use y instead of y[x] for the value returned by DSolve/DSolveValue. (And don't start names with capitals, especially single-letter capitals, especially especially Protected system symbols like I).

integratedDifferenceFunction[I_, O_] := 
  NIntegrate[I[U] - O[U], {U, 0, 2*Pi}];

(* Need IVPs, not general problems *)
m = DSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, x]; (* don't forget the semicolons! *)
n = DSolveValue[{y''[x] + y'[x] == 0, y[0] == 0, y'[0] == 1}, y, x];

integratedDifferenceFunction[m, n]
(*  -5.28505  *)

For NDSolve:

(* Put an N in front... *)
m = NDSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, 
   y, {x, 0, 2 Pi}];
n = NDSolveValue[{y''[x] + y'[x] == 0, y[0] == 0, y'[0] == 1}, 
   y, {x, 0, 2 Pi}];

integratedDifferenceFunction[m, n]
(*  -5.28505  *)
$\endgroup$
5
$\begingroup$
$Version

(* "13.0.1 for Mac OS X x86 (64-bit) (January 28, 2022)" *)

Clear["Global`*"]

IntegratedDifferenceFunction[I_, O_] := 
 NIntegrate[I[U] - O[U], {U, 0, 2*Pi}]

Include initial conditions

m[x_] = DSolveValue[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}, y[x],
   x]

(* Sin[x] *)

n[x_] = DSolveValue[{y''[x] + y'[x] == 0, y[0] == -1, y'[0] == 1}, 
  y[x], x]

(* -E^-x *)

int[x_] = Integrate[m[t] - n[t], {t, 0, x}]

(* 2 - Cos[x] - Cosh[x] + Sinh[x] *)

{int[2 Pi], int[2. Pi]}

(* {1 - Cosh[2 π] + Sinh[2 π], 0.998133} *)

IntegratedDifferenceFunction[m, n]

(* 0.998133 *)

Plot[{Tooltip@int[x], Tooltip[m[x] - n[x]],
  Tooltip@m[x], Tooltip@n[x]}, {x, 0, 2 Pi},
 Filling -> {2 -> Axis},
 PlotLegends -> Placed["Expressions", {.6, .6}]]

enter image description here

m[x_] = NDSolveValue[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1},
  y[x], {x, 0, 2 Pi}]

enter image description here

n[x_] = NDSolveValue[{y''[x] + y'[x] == 0, y[0] == -1, y'[0] == 1},
  y[x], {x, 0, 2 Pi}]

enter image description here

IntegratedDifferenceFunction[m, n]

(* 0.998132 *)
$\endgroup$
3
$\begingroup$

Look at the output from:

DSolve[y''[x] + y[x] == 0, y[x], x]

enter image description here

Note, this is a rule, not a function. To get a function of x, you write y[x]/. output of DSolve. Further, there are undetermined constants c1 and c2. The reason for this is, that you did not specify initial conditions. With initial conditions they will be determind.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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