f[x_]:=x^2*(Sin[x]-Cos[x])-2*x*Sin[x];
n=20;
a=0;
b=π;
ya=0;(*dirichletova podm*)
yb=-π;(*dirichletova podm*)
a0[x_]:=1;
a1[x_]:=-x;
a2[x_]:=x;
presneriesenie[x_]:=x*Cos[x];

Okrajulx[n_,xleft_, xright_, Leftbv_,
Rightbv_,a0_,a1_,a2_,f_,pres_ ]:=
(
h=N[(xright-xleft)/(n+1)];
ps2=
Join[
{f[xleft+h]-
(a2/h^2-a1/(2*h))*
Leftbv},Table[f[xleft+i*h],
{i,2,n-1}],
{f[xleft+n*h]-
(a2[xleft+n*h]/h^2+
a1[xleft+n*h]/(2*h))
*Rightbv}];


diag=
Table[a0[xleft+i*h]-2*a2[xleft+i*h]/h^2,
{i,1,n}];
hdiag=
Table[a2[xleft+i*h]/h^2+
a1[xleft+i*h]/(2*h),{i,1,n-1}];

ddiag=
Table[a2[xleft+i*h]/h^2-
a1[xleft+i*h]/(2*h),{i,2,n}];

A=SparseArray[{Band[{1,2}]->hdiag,
Band[{1,1}]->diag,Band[{2,1}]->ddiag},
{n,n}];

MatrixForm[Normal[A]];
Print[" matica"];
Print[MatrixForm[Normal[A]]];
Print["prava strana"];
Print[ps2];
riesenie=LinearSolve[A,N[ps2]];
xs=Table[xleft+(i-1)*h,{i,1,n+2}];
ys=Join[{Leftbv},
Table[riesenie[[i]],{i,1,n}],
{Rightbv}];
graf=Table[{xs[[i]],ys[[i]]},
{i,1,n+2}];
vysledky=
Table[{xs[[i]],ys[[i]],pres[xs[[i]]],
Abs[(ys[[i]]-pres[xs[[i]]])]},
{i,1,n+2}];)
(*a,b,c vzniknu z diskretizacie, h-krok *)

Okrajulx[n,a,b,ya,yb,a0,a1,a2,f,
presneriesenie]

g1=Plot[presneriesenie[x],{x,a,b},
PlotRange->All,
PlotStyle->RGBColor[0,1,0]];

g2=ListPlot[graf,PlotRange->All,
PlotStyle->{PointSize[0.02],
RGBColor[1,0,1]}];

g3=ListPlot[graf,PlotRange->All,
PlotStyle->{PointSize[0.02],
RGBColor[1,0,0]}];

Show[g1,g2,g3]

Print[" hodnota x num.ries pres. ries
odchylka"]
PaddedForm[TableForm[vysledky],{5,4}]