Re: Suming InterpolatingFunction Objects
- To: mathgroup at smc.vnet.net
- Subject: [mg120236] Re: Suming InterpolatingFunction Objects
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Thu, 14 Jul 2011 07:14:09 -0400 (EDT)
- References: <201107140922.FAA15682@smc.vnet.net>
On Thu, 14 Jul 2011, Gabriel Landi wrote:
> Hello everyone.
>
> I encountered the following problem, which I am not sure is a bug or simply
> a annoying detail.
> I will use as an example a piece of code from the mathematica tutorials.
>
> Say I solve a system of ODEs
>
> s = NDSolve[{x'[t] == -y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3,
> x[0] == y[0] == 1}, {x, y}, {t, 20}]
>
> My quantity of interest could be x[t] + y[t]. I can plot it:
>
> Plot[x[t] + y[t] /. s, {t, 0, 20}]
>
> But say I wish to store it as a single object.
> So I do (this is the kinky part):
>
> sum = FunctionInterpolation[x[t] + y[t] /. s, {t, 0, 20}]
>
> Now, when I plot the result, it comes out completely different. It looks
> like something changed with the interpolation order or something.
>
> Plot[sum[t], {t, 0, 20}]
>
> What do you guys think?
>
> Best regards,
>
> Gabriel
>
Gabriel,
here are two alternatives:
You could sample at more points via
sum = FunctionInterpolation[Evaluate[x[t] + y[t] /. s], {t, 0, 20},
InterpolationPoints -> 100]
additionally specifying the derivatives may be a good idea:
sum = FunctionInterpolation[
Evaluate[Table[
D[Evaluate[x[t] + y[t] /. s], {{t}, k}], {k, 0, 2}]], {t, 0, 20},
InterpolationPoints -> 30]
(* then you get away with less InterpolationPoints *)
or do it manually as follows:
The good thing here is that the NDSolve solutions are sampled at the same
points.
tutorial/NDSolvePackages#120436095
s = NDSolve[{x'[t] == -y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3,
x[0] == y[0] == 1}, {x, y}, {t, 20}]
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"]
domain = InterpolatingFunctionDomain[if1]
{coords1} = InterpolatingFunctionCoordinates[if1];
{coords2} = InterpolatingFunctionCoordinates[if2];
Norm[coords1 - coords2, 1]
vals1 = InterpolatingFunctionValuesOnGrid[if1];
vals2 = InterpolatingFunctionValuesOnGrid[if2];
if3 = Interpolation[Transpose[{coords1, vals1 + vals2}]]
Plot[if3[x], {x, 0, 20}]
Hope this helps.
Oliver
- Follow-Ups:
- Plotting a star map
- From: "Tong Shiu-sing" <sstong@phy.cuhk.edu.hk>
- Re: Suming InterpolatingFunction Objects
- From: Oliver Ruebenkoenig <ruebenko@wolfram.com>
- Re: Suming InterpolatingFunction Objects
- From: Oliver Ruebenkoenig <ruebenko@wolfram.com>
- Plotting a star map
- References:
- Suming InterpolatingFunction Objects
- From: Gabriel Landi <gtlandi@gmail.com>
- Suming InterpolatingFunction Objects