# Using NIntegrate with a combined separable function

I have the function

w[x, t] = p1[x]*u1[t] + p2[x]*u2[t] + p3[x]*u3[t] + ....


where p1[x], p2[x] and p3[x] are known.

My problem is in using NIntegrate with

NIntegrate[w[x, t]*p1[x], {x, 0, 1}]


This does not work because there is the function u[t]. Is there any way to separate u[t] from each term before integration and then multiply them after preforming the integral? Or can I do NIntegrate with u[t] inside?

This can be done as follows. Here is your expression to be integrated:

w[x, t] = p1[x]*u1[t] + p2[x]*u2[t] + p3[x]*u3[t];


Assume that you need to integrate over x from 0 to 1. Then this makes the job:

w[x, t] /. u_[x]*v_[t] :> v[t]*NIntegrate[u[x], {x, 0, 1}]


(*  NIntegrate[p1[x], {x, 0, 1}] u1[t] +
NIntegrate[p2[x], {x, 0, 1}] u2[t] +
NIntegrate[p3[x], {x, 0, 1}] u3[t]   *)


If you do not fix the functions pi[x] there will be also warnings, they should disappear as soon as Mma "knows" these functions.

Later edit: The author commented that it does not work in the following case:

p1[x_] := -Cos[4.7300407 x] + Cosh[4.7300407 x] +
0.9825022 Sin[4.7300407 x] - 0.9825022 Sinh[4.7300407 x];
p2[x_] := -Cos[7.8532046 x] + Cosh[7.8532046 x] -
1.0007773 (-Sin[7.8532046 x] + Sinh[7.8532046 x]);


Indeed, after the substitution of the rule the result is wrong, and I do not see, why.

One can go another way around, much more simple which is possible, if one has fixed the functions p1[x] and p2[x]. Namely,

 w[x_, t_] := p1[x]*u1[t] + p2[x]*u2[t];
Integrate[w[x, t], {x, 0, 1}]

(*  0.830862 u1[t] + 1.94833*10^-6 u2[t]  *)


OK, if you strongly need NIntegrate to accelerate the calculation, the following can be done. Let me first redefine the functions to include such that your mentioned in your question:

p1[x_] := -Cos[4.7300407 x] + Cosh[4.7300407 x] +
0.9825022 Sin[4.7300407 x] - 0.9825022 Sinh[4.7300407 x];
p2[x_] := -Cos[7.8532046 x] + Cosh[7.8532046 x] -
1.0007773 (-Sin[7.8532046 x] + Sinh[7.8532046 x]);
p3[x_] := p1[x]^3*p2[x]^4;

w[x_, t_] := p1[x]*u1[t] + p2[x]*u2[t] + p3[x]*u3[t];


You are right that using simply Integrate[w[x, t], {x, 0, 1}] takes too much time, which is unacceptable. Using NIntegrate[w[x, t], {x, 0, 1}] instead is impossible. Let us try this:

Plus @@ Table[NIntegrate[Drop[w[x, t][[i]], -1], {x, 0, 1}]*
Last[w[x, t][[i]]], {i, 1, Length[w[x, t]]}] // AbsoluteTiming

(*  {4.98792, 0.830862 u1[t] + 1.94833*10^-6 u2[t] + 2.15982 u3[t]}  *)


which takes 5 seconds. I hope that's better.

Have fun!

• in this step: w[x, t] /. u_[x]*v_[t] :> v[t]*NIntegrate[u[x], {x, 0, 1}] why did you change the pi[x] to u[x] and u[t] to v[t] ? cant i use the same names? also can you give what does the code means ? – Love Eva Nov 7 '16 at 14:01
• @Love Eva This belongs to basic structures of the Wolfram Language. Have a look here wolfram.com/language/fast-introduction-for-programmers/patterns as well as Menu/Help/WolframDocumentation/guide/Patterns There you will find some more information on it. I can also recommend to read the corresponding section of the book of Leonid Shifrin which you can get here mathprogramming-intro.org for free. – Alexei Boulbitch Nov 7 '16 at 14:16
• is there any way to define this as rule? because w[x,t] is repeated many times and each time i should do NIntegrate. – Love Eva Nov 7 '16 at 14:48
• @Love Eva It is a rule. But you may, of course write: rule=u_[x]*v_[t] :> v[t]*NIntegrate[u[x], {x, 0, 1}]. and use it everywhere. – Alexei Boulbitch Nov 7 '16 at 15:06
• for some reason its not working: (rule) rule2 = u_[x]*v_[t] :> v[t]*NIntegrate[u[x], {x, 0, 1}]; (functions) p1[x] = -Cos[4.7300407 x] + Cosh[4.7300407` x] + 0.9825022 Sin[4.7300407 x] - 0.9825022 Sinh[4.7300407 x]; p2[x] = -Cos[7.8532046 x] + Cosh[7.8532046 x] - 1.0007773 (-Sin[7.8532046 x] + Sinh[7.8532046 x]); w[x, t] = p1[x]*u1[t] + p2[x]*u2[t]; w[x, t] /. rule2 (-Cos[4.73004 x] + Cosh[4.73004 x] + 0.982502 Sin[4.73004 x] - 0.982502 Sinh[4.73004 x]) u1[ t] + (-Cos[7.8532 x] + Cosh[7.8532 x] - 1.00078 (-Sin[7.8532 x] + Sinh[7.8532 x])) u2[t] – Love Eva Nov 7 '16 at 16:56