0

In some sense the question is a combination of defining an operator and defining numerical integration with parameters. For example, I want to define $I[f] = \int_0^1 f(x)dx$. and tried

I[f_?NumericQ] = NIntegrate[f[x], {x, 0, 1}]

but it returns the error Nintegrate: ... non-numerical values for all sampling points ...

What's the right way to do that?

Fan Zheng
  • 101
  • 1
  • The function f has a Head of "Symbol" not "Numeric". Then, use SetDelayed := instead of Set = . Otherwise the integral will be evaluated at define time. – Daniel Huber Nov 17 '21 at 21:08
  • 3
    I is a protected symbol and represents the imaginary unit. – Roman Nov 17 '21 at 21:10

1 Answers1

2
i[f_] := NIntegrate[f[x], {x, 0, 1}]

i[Sin]
(*    0.459698    *)

i[#^2 &]
(*    0.333333    *)
Roman
  • 47,322
  • 2
  • 55
  • 121