# Integrate a function of NDSolve

I'm having this problem of integrating a function that's the solution of a NDSolve multiplied by another function. I basically compute a cycle wich add at each step the quantity

func[x, t] =
Gaussian* Evaluate[u[x] /.
NDSolve[system, u[x], {x, 0, 6}]]*(Cos[(F - 3 + 0.02*n)*t] -
I*Sin[(F - 3 + 0.02*n)*t]);


where at each step one solves a different differential equation. At the end the resulting function is given by

sum[x,t].


The problem comes up when I want to compute the integral of the resulting function multiplied by another function. I want to have as result a function of the time that could be plotted. I know that there's some mistake I'm doing when I try to integrate a function that has an interpolating function in it but, after having seen other discussion, I'm still not understanding how it should be done. Here's the entire code you can run.

psi2[x_, t_] = Abs[sum[x, t]]^2
ss[x_, t_] = x*psi2[x, t]
s[t] = Integrate[ss[x_, t_], {x, 0, 6}]


If I do this, it gives me problems when one tries to plot s[t].

Clear[system, sum, n, integrle]
sum[x_, t_] = 0;
int = 0
F = 2
S = 1
Gaussians = Sqrt[1/(2*Pi)]*E^((-((F + n - 2)/S)^2))
system := {u''[x] == ((9*Pi^2)/4*((x/2)^6 - (x/2)^10) - (F + n))*u[x],
u[0] == 0, u'[0] == 1};
For[n = 0, n < 100, n++,
Clear[func, oldSum];
oldSum[x_, t_] = sum[x, t];
Clear[sum];
func[x, t] =
Gaussian*Evaluate[u[x] /.
NDSolve[system, u[x], {x, 0, 6}]]*(Cos[(F - 3 + 0.02*n)*t] -
I*Sin[(F - 3 + 0.02*n)*t]);
sum[x, t] = oldSum[x, t] + func[x, t];
]
psi2[x_, t_] = Abs[sum[x, t]]^2
normalization = Integrate[psi2[x, 0], {x, 0, 6}]
ss[x_, t_] = x*psi2[x, t]
ss2[x_, t_] = x^2*psi2[x, t]
s[t_] = Integrate[ss[x_, t_]/normalization, {x, 0, 6}]
s2[t] = Integrate[ss[x_, t_], {x, 0, 6}]
s3[t_] = Integrate[ss2[x_, t_], {x, 0, 6}]
Plot[{s[t]}, {t, 0, 6}]
Plot[{s2[t]}, {t, 0, 6}]
Plot[{s3[t]}, {t, 0, 6}]

• Remove the underscorees on the right side of the definitions s[t], s2[t],s3[t] – Ulrich Neumann Jun 7 at 6:45

If I understand your question you try to build a sum over n.

First solve the ode (depending on n)

U = ParametricNDSolveValue[system, u , {x, 0, 6}, n]


which gives U[n][x]

Summation over n

Gaussians = Sqrt[1/(2*Pi)]*E^((-((F + n - 2)/S)^2))
sum[x_, t_] :=Sum[Gaussians*U[n][x]*(Cos[(F - 3 + 0.02*n)*t] - I*Sin[(F - 3 + 0.02*n)*t]), {n,0, 100}]


Now you can evaluate psi2

psi2[x_, t_] := # Conjugate[#] &[sum[x, t]]
normalization = NIntegrate[psi2[x, 0], {x, 0, 6}]
(*0.219279*)
Table[NIntegrate[x psi2[x, t ], {x, 0, 6}], {t, 0, 6}]/normalization
(*{0.310724, 0.310714, 0.310682, 0.31063, 0.310556, 0.310461, 0.310346}*)


But NIntegrate gives some errors concerning convergence.

• I'm sorry if I didn't explain the issue very well. The problem is computing the integrals of ss[x,t] and ss2[x,t]. They are the product of x (or x^2) times the function that is the modulus square of sum(which has many interpolation function in it). Both if I try to Integrate and NIntegrate I'm not able to solve them. The aim of the code is plotting as a function of t the division s2[t]/s3[t]. For what I saw it doesn't solve the integrals and it gives me problems when I try to plot s2[t] or s3[t]. – Andrea Campolongo Jun 7 at 7:29
• That's the next step. Is sum[x,t] from my answer what you tried to calculate? – Ulrich Neumann Jun 7 at 7:31
• The cycle works, it sum 100 times solutions of different differential equations multiplied by a function of time. the problem comes up when one tries to compute the integral of x*Abs[sum[x,t]].I want to obtain a function of time that can be plotted. I have to compute this integral and the integral of x^2*Abs[sum[x,t]] and plot their division as a funtion of time. – Andrea Campolongo Jun 7 at 7:38