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}]
Flatten[NIntegrate[s, {x, 0, 2}]](*Numerical integration result*)

But how to put it back to original form?

  • $\begingroup$ why not just C NIntegrate[x^2, {x, 0, 2}]? $\endgroup$ – kglr Oct 10 '18 at 7:56
  • 2
    $\begingroup$ Please be aware of the fact that C is reserved in Mma. Use, say, c. $\endgroup$ – Alexei Boulbitch Oct 10 '18 at 8:13
  • 1
    $\begingroup$ The error message is pretty informative: C is not a number so of course NIntegrate[] will be unable to evaluate numerically. $\endgroup$ – J. M.'s technical difficulties Oct 10 '18 at 8:15
  • $\begingroup$ I know that, but I want the the result in terms of c at the end $\endgroup$ – acoustics Oct 10 '18 at 8:25
  • $\begingroup$ 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. $\endgroup$ – 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

For your second example:

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

| improve this answer | |

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


another example:

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


| improve this answer | |
  • $\begingroup$ I looking for an answer in terms of C, I do not know the value of C aprior. $\endgroup$ – acoustics Oct 10 '18 at 8:26
  • $\begingroup$ @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!!. $\endgroup$ – 2.0 Oct 10 '18 at 8:28

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.