# How do I NIntegrate solutions to Differential Equations?

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

• Crossposted Here: community.wolfram.com/groups/-/m/t/2497425?p_p_auth=7sCO5gER Mar 25 at 8:25
• By the way, instead of solving 2 equations, you can formulate 1 ODE for the difference. What is more, you can formulate an ODE for the integral. Mar 25 at 11:57

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  *)

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


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


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


IntegratedDifferenceFunction[m, n]

(* 0.998132 *)


Look at the output from:

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


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.