MathGroup Archive 1995

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

Search the Archive

Re: Piecewise function generator


REECE_D <REECE+_D%A1%Electromagnetic_Sciences at mcimail.com> writes:

>    I want to generate a piecewise continuous linear function by giving a 
> function, call it F, a list of {x,y} values defining the corners of the 
> function.  I can't seem to do it.  Does anyone out there have a quick way 
> of doing it?
>    
>    pts={{20,0.08},{50,0.8},{250,0.8},{5000,0.004}}
>    
>    F[pts_] := ????
>    
>    F[pts]
>    
>    Out:
>    {(0.72 f-12)/30 /; 20<=f<=50,
>    ...}


A quick and easy way is Interpolation with InterpolationOrder->1

In[1]:= pts={{20,0.08},{50,0.8},{250,0.8},{5000,0.004}}

Out[1]= {{20, 0.08}, {50, 0.8}, {250, 0.8}, {5000, 0.004}}

In[2]:= f[x_] = Interpolation[pts, InterpolationOrder->1][x]

Out[2]= InterpolatingFunction[{20, 5000}, <>][x]


It's easier to see the effect for this data set:

In[3]:= pts = Table[{i, Sin[i]}, {i, 7}]

Out[3]= {{1, Sin[1]}, {2, Sin[2]}, {3, Sin[3]}, {4, Sin[4]}, {5, Sin[5]}, 
 
>    {6, Sin[6]}, {7, Sin[7]}}

In[4]:= g[x_] = Interpolation[pts, InterpolationOrder->1][x]

Out[4]= InterpolatingFunction[{1, 7}, <>][x]

In[5]:= Plot[g[x], {x, 1, 7}]

Out[5]= -Graphics-


Robby Villegas


  • Prev by Date: Pretty Graphics of Astroid by envolope
  • Next by Date: Re: Finding Regression Coefficient
  • Previous by thread: Piecewise function generator
  • Next by thread: Re: Piecewise function generator