I'd like to be able to make InterpolatingFunction
s for antiderivatives of functions that can't be integrated symbolically. However, the following code returns several error messages:
FunctionInterpolation[Integrate[Sqrt[1 + x^3], {x, 0, t}], {t, 0, 10}]
Here are the messages:
Thread::tdlen: "Objects of unequal length in {-1.25,-0.416667,0.416667,1.25}^{} cannot be combined. "
Thread::tdlen: "Objects of unequal length in {0.223144 +3.14159\ I,-0.875469+3.14159\ I,-0.875469,0.223144}\ {}\n cannot be combined. "
Thread::tdlen: "Objects of unequal length in {-1.25,-0.416667,0.416667,1.25}\ {} cannot be combined."
General::stop: "Further output of Thread::tlden will be suppressed during this calculation."
FunctionInterpolation::nreal: Near t = 1.25`, the function did not evaluate to a real number.
FunctionInterpolation::nreal: Near t = 1.3277777777777777`, the function did not evaluate to a real number.
What's going on? Is there a simple way to make this work? Changing Integrate
to NIntegrate
doesn't help, though the error messages are different:
NIntegrate::nlim: x = t is not a valid limit of integration.
NIntegrate::nlim: x = t is not a valid limit of integration.
NIntegrate::nlim: x = t is not a valid limit of integration.
General::stop: "Further output of NIntegrate::nlim will be suppressed during this calculation. "
Answer
Use NDSolve
antiD = NDSolveValue[{f'[x] == Sqrt[1 + x^3], f[0] == 0}, f, {x, 0, 10}]
Example usage:
Plot[antiD[x], {x, 0, 10}]
Alternatively...
This works because this function can be antidifferentiated (by Mathematica).
antiD = FunctionInterpolation[
Evaluate @ Integrate[Sqrt[1 + x^3], {x, 0, t}, Assumptions -> 0 < t < 10],
{t, 0, 10}]
or...
integral[t_?NumericQ] := NIntegrate[Sqrt[1 + x^3], {x, 0, t}];
FunctionInterpolation[integral[t], {t, 0, 10}]
FunctionInterpolation
evaluated its argument on a symbolic t
. The pattern test ?NumericQ
prevents evaluation of NIntegrate
until an actual number is substituted for t
. See also What are the most common pitfalls awaiting new users?. Note also that this way does many evaluations of NIntegrate
, whereas the NDSolve
method does just one integration.
Comments
Post a Comment