Splainidega interpoleerimine

sõlmed = {{1, 2}, {1.5, 3.8}, {2, 4.2}, {2.8, 3.1}}; 
n = Length[sõlmed] - 1; 
Do[x[i] = 
        sõlmed[[i + 1, 1]]; 
      y[i] = sõlmed[[i + 1, 
          2]], {i, 0, n}]

Lineaarsplain

S[i_][x_] := c[0, i] + c[1, i] x; 
tingimused = 
      Table[{S[i][x[i]] == y[i], S[i + 1][x[i]] == y[i]}, {i, 1, 
          n - 1}]; 
AppendTo[
      tingimused, {S[1][x[0]] == y[0], S[n][x[n]] == 
          y[n]}]; 
TableForm[tingimused = Flatten[tingimused]]

kordajad = First[Solve[tingimused]]

funs = 
      Table[S[i][x], {i, n}] /. kordajad

j1 = 
      Plot[Evaluate[funs], {x, 0, 3}, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

splain1 = 
      Table[Plot[
          funs[[i]], {x, x[i - 1], 
            x[i]}, PlotStyle -> Hue[0.4], 
          DisplayFunction -> Identity], {i, 1, n}]; 
lineaarsplain = 
      Show[splain1, DisplayFunction -> $DisplayFunction, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

Show[j1, lineaarsplain];

Ruutsplain

S[i_][x_] := c[0, i] + c[1, i] x + c[2, i] (x^2); 
dS[i_][x_] := c[1, i] + 2 c[2, i] x; 
tingimused = 
      Table[{S[i][x[i]] == y[i], S[i + 1][x[i]] == 
            y[i], dS[i][x[i]] == dS[i + 1][x[i]]}, {i, 1, 
          n - 1}]; 
AppendTo[
      tingimused, {S[1][x[0]] == y[0], S[n][x[n]] == 
          y[n], dS[1][x[0]] == 0}]; 
TableForm[tingimused = Flatten[tingimused]]

kordajad = First[Solve[tingimused]]

funs = 
      Table[S[i][x], {i, n}] /. kordajad

j2 = 
      Plot[Evaluate[funs], {x, 0, 3}, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

splain2 = 
      Table[Plot[
          funs[[i]], {x, x[i - 1], 
            x[i]}, DisplayFunction -> Identity, 
          PlotStyle -> Hue[0]], {i, 1, n}]; 
ruutsplain = 
      Show[splain2, DisplayFunction -> $DisplayFunction, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

Show[j2, ruutsplain];

Kuupsplain

S[i_][x_] := 
        c[0, i] + c[1, i] x + c[2, i] (x^2) + c[3, i] (x^3);
dS[i_][x_] := 
        c[1, i] + 2 c[2, i] x + 3 c[3, i] (x^2);
ddS[i_][x_] := 2 c[2, i] + 6 c[3, i] x;
tingimused = 
        Table[{S[i][x[i]] == y[i], S[i + 1][x[i]] == 
              y[i], dS[i][x[i]] == dS[i + 1][x[i]], ddS[i][
                x[i]] == ddS[i + 1][x[i]]}, {i, 1, n - 1}];
AppendTo[
        tingimused, {S[1][x[0]] == y[0], S[n][x[n]] == 
            y[n], dS[1][x[0]] == 0, dS[n][x[n]] == 0}];
TableForm[tingimused = Flatten[tingimused]]

kordajad = First[Solve[tingimused]]

funs = 
      Table[S[i][x], {i, n}] /. kordajad

j3 = 
      Plot[Evaluate[funs], {x, 0, 3}, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

splain3 = 
      Table[Plot[
          funs[[i]], {x, x[i - 1], 
            x[i]}, DisplayFunction -> Identity, 
          PlotStyle -> Hue[0.7]], {i, 1, n}]; 
kuupsplain = 
      Show[splain3, DisplayFunction -> $DisplayFunction, 
        Epilog -> {PointSize[0.03], 
            Point /@ sõlmed}];

Show[j3, kuupsplain];

Show[lineaarsplain, ruutsplain, kuupsplain];