(* Задание 1, Числени методи, Софтуерно инженерство, II курс, Вариант 17 *)
(* 1 задача: *)
LagrangeBasic[k_,list_,x_]:=(res=1;Do[If[i≠k,res*=(x-list[[i+1]])/(list[[k+1]]-list[[i+1]])],{i,0,Length[list]-1}];Expand[res])

LagrangePolynomial[nodes_,values_,x_]:=(resultat=0;Do[resultat+=values[[k]]*LagrangeBasic[k-1,nodes,x],{k,1,Length[nodes]}];Expand[resultat])

nodes=Table[k,{k,-40,160,40}];

values={6900, 8100, 9350, 10500, 11700, 12800};

p[x_]:=LagrangePolynomial[nodes,values,x];

p[45]

Plot[p[x], {x, -80, 200}]

(* 2 задача: *)
(* Имаме, че v1 = 3, v2 = 1, v3 = 1, а v1 + v2 + v3 = N + 1, т.е. N = 4 *)
(*a x^4+b x^3+ c x^2 + d x + e е видът на търсения полином *)
Solve[{a+b+c+d+e==-2,4a+3b+2c+d==2,12a+6b+2c==-14,16a+8b+4c+2d+e==-17,81a+27b+9c+3d+e==-122},{a,b,c,d,e}]

P[x] = a x^4+b x^3+ c x^2 + d x + e

%/.{{a->-2,b->0,c->5,d->0,e->-5}}

(* 3 задача: *)

NewtonBackward[n_,x0_,h_,f_]:=Interpolation[Table[{x,f[x]},{x,x0,x0+n h,h}]];
f[x_]:=E^x (* примерно *)

Plot[NewtonBackward[10,0,1/10,f][x],{x,0,10}]

(* 4 задача *)

L[x_] := InterpolationPolynomial[{{0, E^(0/3)}, {0.1, E ^(0.1/3)}, {0.2, E^(0.2/3)}, {0.3, E^(0.3/3)}}, x];
F[x_] := E^(x/3);
S[x_] := Expand[F''''[x]];
NMinimize[{S[x],{x≥0&& x ≤ 0.3}}, {x}]
NMaximize[{S[x],{x≥0&&x≤0.3}},{x}]
Plot[{Abs[0.0123457*(x-0)*(x-0.1)*(x-0.2)*(x-0.3)/24],Abs[0.0136441*(x-0)*(x-0.1)*(x-0.2)*(x-0.3)/24]},{x,0,0.3}, PlotStyle->{{Orange, Thick}, {Black, Thick}}]