       Re: Plotting a function of an interpolated function

• To: mathgroup at smc.vnet.net
• Subject: [mg52984] Re: [mg52925] Plotting a function of an interpolated function
• From: DrBob <drbob at bigfoot.com>
• Date: Sat, 18 Dec 2004 04:00:23 -0500 (EST)
• References: <200412171018.FAA16026@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```I'll take the last problem first -- the Plot:

Plot[W[\[Phi]], {t, 0, 100/m}]

It can't be evaluated until Phi is computed, but regardless of what Phi turns out to be, the plot is determined anyway:

Clear[m, mp, G, P, \[Rho], \[Phi], W,
Inflaton]
V = Function[x, (1/2)*m^2*x^2];
\[Rho] = Function[x, D[x, t]^2/2 + V[x]];
H = Function[x, Sqrt[((8*Pi*G)/3)*
\[Rho][x]]];
P = Function[x, D[x, t]^2/2 - V[x]];
Inflaton = Derivative[\[Phi]][t] +
3*H[\[Phi][t]]*Derivative[\[Phi]][t] +
Derivative[V][\[Phi][t]];
W = Function[x, P[x]/\[Rho][x]];
W[\[Phi]]

-1

W is constant, so the Plot should show -1 across the board (if it works at all).

Now for the problem with Phi. Your definition of Phi isn't the best, but if you must use it, the Plot needs Evaluate:

m = 10^16;
mp = 1.2211*10^19;
G = mp^(-2);
Clear[\[Phi]]
\[Phi] = \[Phi][t] /. First[NDSolve[
{Inflaton == 0, Derivative[\[Phi]][
0] == 0, \[Phi] == 1}, \[Phi][t],
{t, 0, 100/m}, MaxSteps -> 10^6]];
Plot[Evaluate[W[\[Phi]]], {t, 0, 100/m}]

But I don't see the expected constant -1. Instead, I see a Sin wave, I think due to noise.

(W simplified to -1 when its argument was a symbol, but when the argument is numeric, the fraction P[x]/rho[x] appears to be vulnerable to numerical problems.)

Define Phi this way, and I get a much more reasonable result:

Clear[\[Phi]]
\[Phi] = \[Phi] /. First[NDSolve[
{Inflaton == 0, Derivative[\[Phi]][
0] == 0, \[Phi] == 1}, \[Phi],
{t, 0, 100/m}, MaxSteps -> 10^6]];
Plot[W[\[Phi][t]], {t, 0, 100/m}]

The curve wavers, but it's -1 to machine precision, as expected.

Bobby

On Fri, 17 Dec 2004 05:18:38 -0500 (EST), Adam Getchell <agetchell at physics.ucdavis.edu> wrote:

> I've solved a function using NDSolve, expressed it as a pure function,
> and plotted it in the attached notebook. Now I'd like to define other
> functions in terms of this pure function and plot them. I haven't seen
> this come up in the archives, hence the post here.
>
> If phi is my interpolating function and W is my newly defined function, ie:
>
> In:=
> V = Function[x, (1/2)*m^2*
>      x^2];
> \[Rho] = Function[x,
>     D[x, t]^2/2 + V[x]];
> H = Function[x, Sqrt[
>      ((8*Pi*G)/3)*\[Rho][x]]];
>
> In:=
> V[\[Phi][t]]
> Derivative[V][\[Phi][t]]
> \[Rho][\[Phi][t]]
> H[\[Phi][t]]
> Out=
> (1/2)*m^2*\[Phi][t]^2
> Out=
> m^2*\[Phi][t]
> Out=
> (1/2)*m^2*\[Phi][t]^2 +
>   (1/2)*Derivative[\[Phi]][t]^
>     2
> Out=
> 2*Sqrt[(2*Pi)/3]*
>   Sqrt[G*((1/2)*m^2*
>       \[Phi][t]^2 + (1/2)*
>       Derivative[\[Phi]][t]^
>        2)]
>
> In:=
> Inflaton = Derivative[\[Phi]][
>     t] + 3*H[\[Phi][t]]*
>     Derivative[\[Phi]][t] +
>    Derivative[V][\[Phi][t]]
> Out=
> \[Phi][t]*m^2 + Derivative[
>     \[Phi]][t] + 2*Sqrt[6*Pi]*
>    Derivative[\[Phi]][t]*
>    Sqrt[G*((1/2)*m^2*
>        \[Phi][t]^2 + (1/2)*
>        Derivative[\[Phi]][t]^
>         2)]
>
> In:=
> m = 10^16;
> mp = 1.2211*10^19;
> G = mp^(-2);
>
> In:=
> \[Phi] = \[Phi][t] /. First[
>     NDSolve[{Inflaton == 0,
>       Derivative[\[Phi]][
>         0] == 0, \[Phi] ==
>        1}, \[Phi][t], {t, 0,
>       100/m}, MaxSteps ->
>       10^6]]
> Out=
> InterpolatingFunction[][t]
>
> In:=
> P = Function[x, D[x, t]^2/2 -
>     V[x]]
> \[Rho]
> W = Function[x, P[x]/\[Rho][x]]
> \[Phi]
> Out=
> Function[x, (1/2)*D[x, t]^2 -
>    V[x]]
> Out=
> Function[x, (1/2)*D[x, t]^2 +
>    V[x]]
> Out=
> Function[x, P[x]/\[Rho][x]]
> Out=
> InterpolatingFunction[][t]
>
> Then this doesn't work:
>
> In:=
> TestPlot = Plot[W[\[Phi]],
>     {t, 0, 100/m}];
>
> I've tried a symbolic solution to avoid the interpolating function, but
> my workstation hasn't produced one yet.
>
> I'm reading the Functions notebook from "Essential Mathematica for
> Students of Science", but as far as I can tell I should be using pure
> functions of the interpolating function.
>
> Any pointers appreciated, as always. Thanks for help in the past,
> especially Dr. Bob.
>
>
>
>
>
>
>

--
DrBob at bigfoot.com
www.eclecticdreams.net

```

• Prev by Date: Re: Mathematica language issues
• Next by Date: Re: Re: Re: Integrate in version 5.1
• Previous by thread: Plotting a function of an interpolated function
• Next by thread: Re: Plotting a function of an interpolated function