# Want to use NIntegrate with some unknown coefficients associated with the expressions

NIntegrate[C*x^2, {x, 0, 2}]


Here is the simple expression I want to use NIntegrate to find the value, but I am getting "The integrand C x^2 has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,2}}." this error. How to overcome this. I dont want to use Integrate.

Why I am not using Integrate is, Suppose if I have 'n' terms in my expressions associated with 'n' unknown coefficients, Then Integrate takes a lot of time to get the result.

(The integrand I am interested in has 70 terms, and Integrate takes too much time in this case)

It is not a complete answer, but I can say I have solved 50% problem suppose if we have an expression

exp = Expand[C1* Sin[x] + C2*Cos[x]]
exp1 = Expand[(D[exp, {x, 2}])^2]
Expand[N[Integrate[exp1, {x, 0, 2}]]](*Regular integral result*)
s = CoefficientList[exp1, {C1, C2}]
MatrixForm[s]
Flatten[NIntegrate[s, {x, 0, 2}]](*Numerical integration result*)


But how to put it back to original form?

• why not just C NIntegrate[x^2, {x, 0, 2}]? – kglr Oct 10 '18 at 7:56
• Please be aware of the fact that C is reserved in Mma. Use, say, c. – Alexei Boulbitch Oct 10 '18 at 8:13
• The error message is pretty informative: C is not a number so of course NIntegrate[] will be unable to evaluate numerically. – J. M.'s technical difficulties Oct 10 '18 at 8:15
• I know that, but I want the the result in terms of c at the end – acoustics Oct 10 '18 at 8:25
• In which case NIntegrate[] is the wrong function. You haven't explained why Integrate[] is not useful for you, so there isn't much else to say. – J. M.'s technical difficulties Oct 10 '18 at 8:37

You can use Collect where the 3rd argument performs your integration:

Collect[c x^2, c, NIntegrate[#, {x, 0, 2}]&]


2.66667 c

expr = D[c1 Sin[x] + c2 Cos[x], x, x]^2
Collect[expr, c1|c2, NIntegrate[#, {x, 0, 2}]&]


(-c2 Cos[x] - c1 Sin[x])^2

1.1892 c1^2 + 0.826822 c1 c2 + 0.810799 c2^2

Another possibility is to use an inactive integral, distribute, then integrate. This way you perform 3 simple integrals instead of 1 complicated integral. The inactive integral:

integral = Inactive[Integrate][Expand @ expr, {x, 0, 2}]


Inactive[Integrate][(c2^2 Cos[x]^2 + 2 c1 c2 Cos[x] Sin[x] + c1^2 Sin[x]^2), {x, 0, 2}]

Use Distribute, then activate:

N @ Activate @ Distribute @ integral


1.1892 c1^2 + 0.826822 c1 c2 + 0.810799 c2^2

Note: Capitals are reserved in Mathematica, so use small letters!!

Don't use "NIntegrate" directly, Make it as a function of constant coefficients, then It will work.

See this:

 f[c_] := NIntegrate[c*x^2, {x, 0, 2}]
f[1.]


2.66667

another example:

 f[c_, d_] := NIntegrate[c*x^2 + d*x, {x, 0, 2}]
f[1., 2.]


6.66667

• I looking for an answer in terms of C, I do not know the value of C aprior. – acoustics Oct 10 '18 at 8:26
• @vijay, Here you are putting the value of c after evaluating the integral. Note that, "NIntegrate" is a numerical operation, Hence numerically you can't get a parametric answer, This is not possible!!. – 2.1 Oct 10 '18 at 8:28