0
$\begingroup$

This is a follow-up question to Ploting a Piecewise function obtained from linear interpolation of points, where I asked how to construct a piecewise function from a list of points.

I now have two following questions :

1) Is it possible to display the linear interpolation with linear functions $f_i(x) = a x + b$ ?

2) Is it possible to make h[x_] = Composition[g, g][x] more readable if g obtained from the previous question --- obviously the plot of h[x] is correct ?

$\endgroup$
2
$\begingroup$

Referring to your previous question: Why do you need the Piecewise construction. Pack it in one InterpolationFunction.

x[1] := 1; y[1] := 2;
x[2] := 2; y[2] := 3;
x[3] := 3; y[3] := 4;
x[4] := 4; y[4] := 1;
a = {x[1], y[1]};
b = {x[2], y[2]};
c = {x[3], y[3]};
d = {x[4], y[4]};

g[x_ /; 1 <= x <= 4] = Interpolation[{a, b, c, d}, InterpolationOrder -> 1][x]

h = Composition[g, g]

(*  Composition[InterpolatingFunction [{{1,4}},"<>"],
                InterpolatingFunction [{{1,4}},"<>"]]  *)

{Plot[g[x], {x, 0, 5}], Plot[h[x], {x, 0, 5}]}

enter image description here

Edit

Get functions

pts = {a, b, c, d}

tp = Transpose[{Rest[pts], Drop[pts, -1]}]

(*   {{{2, 3}, {1, 2}}, {{3, 4}, {2, 3}}, {{4, 1}, {3, 4}}}   *)

ip = InterpolatingPolynomial[#, x] & /@ tp

(*   {1 + x, 1 + x, 1 - 3 (-4 + x)}   *)

Show[Plot[g[x], {x, 0, 5}, PlotStyle -> {Thick, Red}], 
     Plot[ip, {x, 0, 5}, PlotStyle -> {Thin, Thin, Thin}], 
        PlotRange -> All]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.