Re: Options to FunctionInterpolation
- To: mathgroup at smc.vnet.net
- Subject: [mg67588] Re: Options to FunctionInterpolation
- From: "James Gilmore" <james.gilmore at yale.edu>
- Date: Sat, 1 Jul 2006 05:12:32 -0400 (EDT)
- Organization: Yale University
- References: <e82o97$r6e$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Andrew, I agree this does seem like strange behaviour. It can be understood by looking at the values Mathematica is computing while trying to obtain the required precision. First since you have supplied a symbolic function, i.e. In[610]:= f[x_] := Exp[x]; f[1] Out[610]= \[ExponentialE] although delayed, I would expect Mathematica to use symbolic arguments if supplied to the function. If you add in some code to keep track of the x and f[x] values that are being evaluated, you will see that Mathematica is computing symbolic exponetional functions with extremely large algebraic fractions. From my experience, this type of algebraic computation is slow by nature for the exp. n = 0; xl = {}; f[x_] := (++n; xl = Append[xl, {x, Exp[x]}]; Exp[x]); TimeConstrained[g = FunctionInterpolation[f[x], {x, -5, 5}, PrecisionGoal -> 5, InterpolationPrecision -> Automatic], 2, Print["im still computing symbolic exp[fractions]"]]; n xl If on the other hand you make f numerical this is not that case: n = 0; xl = {}; f[x_] := (++n; xl = Append[xl, {x, 1. Exp[x], Exp[x]}]; 1. Exp[x]); g = FunctionInterpolation[f[x], {x, -5, 5}, PrecisionGoal -> 5, InterpolationPrecision -> Automatic]; n xl Looking further, if you examine the output of xl when f[x_]=1.Exp[x]: {{5, 148.4131591025766, E^5}, {-5., 0.006737946999085467, 0.006737946999085467}, {-5., 0.006737946999085467, 0.006737946999085467},...} The first x is exact, as expected since the original interval was given as an integer. But the second x, i.e. -5 is now numerical. This would appear to be a result of f[x]=1. Exp[x] returning a numerical value, as can be seen above. This is quite a clever trick if you think about. So all this is well and good. The option InterpolationPrecision -> Automatic will chose whether numerical or symbolic arguments i.e. tyhe x values, should be used in this case. The bottleneck in the case you pointed out is the evaluation of the exp with the large symbolic fractions. Try n = 0; xl = {}; f[x_] := (++n; xl = Append[xl, {x, x^2}]; x^2); g = FunctionInterpolation[f[x], {x, -5, 5}, PrecisionGoal -> 5, InterpolationPrecision -> Automatic]; n xl This evaluates quickly, and you can see the fractions that are computed are becoming larger, but the x^2 is a much nicer function to evaluate than Exp[x]. I will also point out that making either end point numerical will force numerical evaluation n = 0; xl = {}; f[x_] := (++n; xl = Append[xl, {x, x^2}]; x^2); g = FunctionInterpolation[f[x], {x, -5., 5}, PrecisionGoal -> 5, InterpolationPrecision -> Automatic]; n xl And just for some fun, this is also true with symbolic intervals. Consider this pathological symbolic interval: n = 0; xl = {}; f[x_] := (++n; xl = Append[xl, {x, x^2}]; x^2); g = FunctionInterpolation[f[x], {x, -5/Exp[1], 5/\[Pi]}, PrecisionGoal -> 5, InterpolationPrecision -> Automatic]; n xl Hope this helped. Cheers James Gilmore "Andrew Moylan" <andrew.j.moylan at gmail.com> wrote in message news:e82o97$r6e$1 at smc.vnet.net... > Hi, > > I am trying to understand the meaning of the options to > FunctionInterpolation, especially the InterpolationPrecision option, > whose default value is Automatic. Please consider the following code: > > n = 0; > f[x_] := (++n; Exp[x]); > g = FunctionInterpolation[f[x], { > x, -5, 5}, PrecisionGoal -> 5, InterpolationPrecision -> > $MachinePrecision]; > Print[n]; > > It creates an interpolating function for f[x] in the range {-5,5}, > counting the number of times f[x] is evaluated. In this example, I get > n = 153, and the code takes less than a second to execute. Now consider > the following slightly different code: > > n = 0; > f[x_] := (++n; Exp[x]); > TimeConstrained[g = > FunctionInterpolation[f[x], {x, -5, 5}, PrecisionGoal -> 5, \ > InterpolationPrecision -> Automatic], 5, Print["ran out of time"]]; > Print[n]; > > Again this code tries to create an interpolating function for f[x] in > the range {-5,5}, but this time the default value of Automatic is used > for InterpolationPrecision. The output of this code is "ran out of > time" and "100". Thus, the code takes longer than 5 seconds to execute, > and f[x] is only evaluated 100 times during those 5 seconds! > > Can anyone explain what the default setting of Automatic for > InterpolationPrecision does, and why FunctionInterpolation is failing > in this case? For reference, it may be useful to consider the following > final piece of code, in which the default settings (of Automatic) are > used for both PrecisionGoal and InterpolationPrecision: > > n = 0; > f[x_] := (++n; Exp[x]); > g = FunctionInterpolation[f[x], {x, -5, 5}]; > Print[n]; > > The result of this code is a very quick "243". > > Thanks for any help. > > Cheers, > > Andrew >