Lagrange interpolatsioonipolünoom

sõlmed = {{1, 4.2}, {2, 3.7}, {3, 3.8}, {4, 2.8}, {5, 
            2.9}, {6, 2.8}, {7, 3.2}, {8, 2.9}};
n = Length[sõlmed];
xx = Transpose[
            sõlmed][[1]];
ff = Transpose[
            sõlmed][[2]];
Pn = (∑_(i = 1)^n)((∏_(k = 1)^(n - 1))((x - Drop[xx, {i}][[k]])/(xx[[i]] - Drop[xx, {i}][[k]]))) ff[[
            i]]

Expand[Pn]

poly = InterpolatingPolynomial[sõlmed, x]

Expand[poly]

Plot[Pn, {x, 0.5, 8.5}, AspectRatio -> Automatic, 
      Epilog -> {PointSize[0.03], Hue[0], 
          Point /@ sõlmed}];

Splainidega interpoleerimine

sõlmed = {{1, 4.2}, {2, 3.7}, {3, 3.8}, {4, 2.8}, {5, 
            2.9}, {6, 2.8}, {7, 3.2}, {8, 2.9}};
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, 9}, 
          Epilog -> {PointSize[0.015], 
              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.015], 
              Point /@ sõlmed}];

Show[j3, kuupsplain];

Show[lineaarsplain, ruutsplain, kuupsplain];

Kahe muutuja funktsiooni interpoleerimine

xkoord = {1.0, 2.0, 3.0, 4.0}; 
ykoord = {1.2, 2.0, 2.9}; 
zkoord = {{0.2, 0.7, 1.0, 1.2}, {1.0, 1.5, 2.0, 1.7}, {1.2, 1.3, 1.4, 
          1.5}};

LagrangePoly[xx_List, ff_List, x_Symbol] := 
      Module[{n = Length[xx]}, 
        Expand[(∑_(i = 1)^n)((∏_(k = 1)^(n - 1))((x - Drop[xx, {i}][[k]])/(xx[[i]] - Drop[xx, {i}][[k]]))) ff[[
                i]]]]

P1 = 
      LagrangePoly[xkoord, zkoord[[1]], 
        x]

P2 = 
      LagrangePoly[xkoord, zkoord[[2]], 
        x]

P3 = 
      LagrangePoly[xkoord, zkoord[[3]], 
        x]

P = LagrangePoly[ykoord, {P1, P2, P3}, y]

pind = 
      Plot3D[P, {x, 1, 4}, {y, 1, 3}, 
        DisplayFunction -> Identity]; 
j1 = 
      ParametricPlot3D[{{x, 1.2, P1 + 0.003}, {x, 2, P2 + 0.003}, {x, 2.9, 
            P3 + 0.003}}, {x, 1, 4}, 
        DisplayFunction -> Identity]; 
jooned = 
      Graphics3D[{Hue[0. ], Thickness[0.01], 
          j1[[1]]}]; 
Show[jooned, pind, Axes -> True, 
      AxesLabel -> {"x", "y", "z"}, 
      DisplayFunction -> $DisplayFunction];