MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Re: Combining

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107202] Re: [mg107171] Re: [mg107129] Re: [mg107092] Combining
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Fri, 5 Feb 2010 03:23:46 -0500 (EST)
  • References: <201002020830.DAA08963@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

This does each interpolation ONCE, and it defines f just ONCE.

Clear[y]
f1 = y /.
    First@NDSolve[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1},
       y, {x, 0, Pi}][[1]];
f2 = y /.
    First@NDSolve[{y''[x] + y[x] == 0, y[Pi] == -1, y'[0] == 1},
       y, {x, Pi, 2 Pi}][[1]];
f[x_] = Piecewise[{{f1@x, 0 <= x <= Pi}, {f2@x, Pi < x <= 2 Pi}}];
Plot[f@x, {x, 0, 2 Pi}]

There is no single interpolation that will accurately combine two  
functions such as that, since they don't "fit" at the breakpoint Pi. You  
can't interpolate a vertical jump!

As for wanting "to be able to use... a replacement rule", the function f  
above IS a replacement rule.

If you want, you can define it this way:

Clear[f]
f[x_] := f[x] =
    Piecewise[{{f1@x, 0 <= x <= Pi}, {f2@x, Pi < x <= 2 Pi}}];
Timing[Print@Plot[f@x, {x, 0, 2 Pi}];]

{0.017917, Null}

If you do that, you've now stored quite a few values:

Length@DownValues@f

479

One of those is the function definition, so it took less than .02 seconds  
to compute the function 478 times, store 478 values, AND create the plot.

Every time you calculate f@x for a new x (not evaluated when creating the  
Plot), the function will STILL have to make the choice between f1 and f2  
and apply one of them. No way around it.

But, as the Timing above shows, Mathematica does it extremely fast... so  
don't worry about it.

Bobby

On Thu, 04 Feb 2010 06:00:05 -0600, Simon Pearce  
<Simon.Pearce at nottingham.ac.uk> wrote:

> Thanks to those who have responded. In the responses everyone defines a
> new function f[x_] using piecewise. However, I want to be able to use
> the interpolatingfunctions as a replacement rule, as that is what the
> rest of my code requires. In particular I use the f[x_]:=f[x] =... trick
> so I don't have to compute the same pieces of code repeatedly.
>
> Here are some example interpolatingfunctions from NDSolve,
>
> f1 = NDSolve[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1},
>    y, {x, 0, Pi}][[1]];
> f2 = NDSolve[{y''[x] + y[x] == 0, y[Pi] == -1, y'[0] == =
> 1},
>    y, {x, Pi, 2 Pi}][[1]];
>
> And then I want to be able to combine the two replacement functions to
> act on an expression involving y, say A. If I just had one function I'd
> do A/.f1, which gives me a function that I can then plot, evaluate at
> different points etc. But I want the correct function f1 or f2 for the
> appropriate ranges of x in A, but I don't want to specify x in the
> piecewise.
>
> So I can write Piecewise[{{ff1, 0 <= x <= Pi}, {ff2, Pi < x <= 2 Pi}}],
> but that doesn't work as it doesn't have a value of x when used as a
> replacement. It does work if I specify x beforehand, but I don't want
> to!
>
> Any ideas?
>
> Thanks,
> Simon
>
> -----Original Message-----
> From: DrMajorBob [mailto:btreat1 at austin.rr.com]
> Sent: 03 February 2010 11:12
> To: mathgroup at smc.vnet.net
> Subject: [mg107171] [mg107129] Re: [mg107092] Combining  
> InterpolatingFunctions
>
> For instance:
>
> Clear[f]
> f1 = Interpolation@Table[{x, Sin@x}, {x, 0, Pi, 1/10}];
> f2 = Interpolation@Table[{x, Cos@x}, {x, Pi, 2 Pi, 1/10}];
> f[x_] = Piecewise[{{f1@x, 0 <= x <= Pi}, {f2@x, Pi < x <= 2 Pi}}];
> Plot[f@x, {x, 0, 2 Pi}]
>
> Bobby
>
> On Tue, 02 Feb 2010 02:30:36 -0600, Simon Pearce
> <Simon.Pearce at nottingham.ac.uk> wrote:
>
>> Hi MathGroup,
>>
>>
>>
>> I have two sets of InterpolatingFunctions coming from two separate
>> NDSolve's. One of them is defined over the region [0,rc] and the other
>> over the region [rc,2]. I would like Mathematica to automatically
> choose
>> the correct one when I use a replacement rule. If I could tell it
> never
>> to extrapolate this would be perfect, though I don't seem to be able
> to.
>>
>>
>>
>> I've tried using FunctionInterpolation, but in order to keep my error
>> terms down I had to increase the InterpolationPoints to 1000, which
>> increases the calculation time from approximately .5sec to 1.5sec.
>>
>>
>>
>> Can anyone suggest an efficient way of combining
> InterpolatingFunctions
>> without re-interpolating them? Or turning the extrapolation off!
>>
>>
>>
>> Thanks,
>>
>>
>>
>> Simon Pearce
>>
>>
>
>
> --
> DrMajorBob at yahoo.com
>
>
> This message has been checked for viruses but the contents of an  
> attachment may still contain software viruses which could damage your  
> computer system: you are advised to perform your own checks. Email  
> communications with the University of Nottingham may be monitored as  
> permitted by UK legislation.
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Localize Pattern variables
  • Next by Date: Re: Combining InterpolatingFunctions
  • Previous by thread: Re: Re: Re: Combining
  • Next by thread: Re: Combining InterpolatingFunctions