CurrentValue[$FrontEnd,CellContext]=Notebook;

rovnice = D[f[x],x,x]==-Abs[Cos[Pi x]] f[x]
okrajPodminky = {f[-1]==1 , f[1]==1}

diskretizace = {
f''[(Subscript[X, i_])] :> ((Subscript[f, i+1])-2(Subscript[f, i])+ (Subscript[f, i-1]))/h^2,
f[(Subscript[X, i_])] :> (Subscript[f, i]),
f[-1] -> (Subscript[f, 1]), f[1]-> (Subscript[f, n])}

gridX  = {(Subscript[X, i_]):>  -1+2/(n-1)(i-1),h->2/(n-1)}
(Subscript[X, n])/.%
(*Hold@Table[(Subscript[x, k]),{k,1,n}]/.gridX/.n->10//ReleaseHold*)
table[(Subscript[X, k]),{k,1,n}]/.gridX/.n->10/.table->Table
Graphics[Point[{#,0}]&/@%,AspectRatio->.1,Frame->True]

nn=8;
myGrid = table[ (Subscript[X, k]) ,{k,1,n}]/. n->nn/.table->Table
nezname = table[ (Subscript[f, k]) ,{k,1,n}]/. n->nn/.table->Table
Join[ 
Table[ rovnice ,  {x,myGrid[[2;;-2]]}]/.diskretizace/.gridX/.n->nn,
okrajPodminky/.diskretizace/.gridX/.n->nn
]
reseni =NSolve[%,nezname][[1]]

NDSolve[ rovnice && okrajPodminky,f[x],{x,-1,1}];
mathReseni =f[x]/.%[[1]];
Show[
Plot[ mathReseni,{x,-1,1},PlotTheme->"Detailed",PlotStyle->Red,ImageSize->Large],
ListPlot@Transpose[{myGrid,nezname}/.reseni/.gridX/.n->nn]
]

InterpolatingPolynomial[{{0,f0},{h,f1},{2h,f2},{3h,f3},{4h,f4}},x];
D[%,x,x]/.x->2h//Simplify
D[%%,x,x]/.x->3h//Simplify

diskretizace4rad = {
f''[(Subscript[X, i_])] :> Which[
i==2,(-(Subscript[f, i+3])+4(Subscript[f, i+2])+ 6(Subscript[f, i+1])-20(Subscript[f, i])+11(Subscript[f, i-1]))/(12h^2),
i==n-1,(-(Subscript[f, i-3])+4(Subscript[f, i-2])+ 6(Subscript[f, i-1])-20(Subscript[f, i])+11(Subscript[f, i+1]))/(12h^2),
True, (-(Subscript[f, i+2])+16(Subscript[f, i+1])- 30(Subscript[f, i])+16(Subscript[f, i-1])-(Subscript[f, i-2]))/(12h^2)],
f[(Subscript[X, i_])] :> (Subscript[f, i]),
f[-1] -> (Subscript[f, 1]), f[1]-> (Subscript[f, n])};

nn=8;
myGrid = table[ (Subscript[X, k]) ,{k,1,n}]/. n->nn/.table->Table
nezname = table[ (Subscript[f, k]) ,{k,1,n}]/. n->nn/.table->Table
Join[ 
Table[ rovnice ,  {x,myGrid[[2;;-2]]}]/.diskretizace4rad/.gridX/.n->nn,
okrajPodminky/.diskretizace4rad/.gridX/.n->nn
]
reseni =NSolve[%,nezname][[1]]

NDSolve[ rovnice && okrajPodminky,f[x],{x,-1,1}];
mathReseni =f[x]/.%[[1]];
Show[
Plot[ mathReseni,{x,-1,1},PlotTheme->"Detailed",PlotStyle->Red,ImageSize->Large],
ListPlot@Transpose[{myGrid,nezname}/.reseni/.gridX/.n->nn]
]