Re: Piecewise function generator
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1182] Re: Piecewise function generator
- From: roth at sunny.mpimf-heidelberg.mpg.de (Arnd Roth)
- Date: Sat, 20 May 1995 05:33:34 -0400
- Organization: Max-Planck-Institut fuer Medizinische Forschung
In article <3p8shv$6b8 at news0.cybernetics.net> 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, > ...} There is a built-in object for such purposes, InterpolatingFunction. Try this: 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]:= ?Interpolation Interpolation[data] constructs an InterpolatingFunction object which represents an approximate function that interpolates the data. The data can have the forms {{x1, f1}, {x2, f2}, ...} or {f1, f2, ...}, where in the second case, the xi are taken to have values 1, 2, .... In[3]:= ?InterpolationOrder InterpolationOrder is an option to Interpolation. InterpolationOrder -> n specifies interpolating polynomials of order n. In[4]:= F[points_] := Interpolation[points, InterpolationOrder -> 1] In[5]:= specialF = F[pts] Out[5]= InterpolatingFunction[{20, 5000}, <>] In[6]:= Plot[specialF[x], {x, 20, 5000}] Out[6]= -Graphics- In[7]:= InputForm[specialF] Out[7]//InputForm= InterpolatingFunction[{20, 5000}, {{20, 20, {0.08, 0}, {0}}, {50, 20, {0.8, 0.024}, {0}}, {250, 50, {0.8, 0.}, {0}}, {5000, 250, {0.004, -0.0001675789473684210526}, {0}}}] (* this is the internal data structure for this InterpolatingFunction *) Arnd Roth Abteilung Zellphysiologie Max-Planck-Institut fuer Medizinische Forschung Postfach 10 38 20, D-69028 Heidelberg, Germany http://sunny.mpimf-heidelberg.mpg.de/people/roth/ArndRoth.html