Needs["LinearAlgebra`Tridiagonal`"]

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

OkrajUl[n_, xleft_, xright_, LeftBv_, RightBv_, a0_, a1_, a2_, f_, 
        pres_] := (h = 
          N[(xright - xleft)/(n + 1)]; 
pStrana = 
          Join[{f[xleft + 
                    h] - (a2[xleft + h]/h^2 - a1[xleft + h]/(2*h))*
                  LeftBv}, 
Table[
              f[xleft + (i*h)], {i, 2, 
                n - 1}], {f[
                  xleft + 
                    n*h] - (a2[xright - h]/h^2 + 
                      a1[xright - h]/(2*h))*
                  RightBv}]; 
diag = 
          Table[a0[xleft + i*h] - 2*a2[xleft + i*h]/h^2, {i, 1, 
              n}]; 
hornaDiag = 
          Table[a2[xleft + i*h]/h^2 + a1[xleft + i*h]/(2*h), {i, 1, 
              n - 1}]; 
dolnaDiag = 
          Table[a2[xleft + i*h]/h^2 - a1[xleft + i*h]/(2*h), {i, 2, 
              n}]; 
A = 
          IdentityMatrix[
            n]; 
Do[{A[[i, i]] = 
              diag[[i]], 
A[[i + 1, i]] = 
              dolnaDiag[[i]], 
A[[i, i + 1]] = 
              hornaDiag[[i]]}, {i, 1, 
            n - 1}]; 
A[[n, n]] = 
          diag[[n]]; 
Print[
          MatrixForm[A]]; 
riesenie = 
          TridiagonalSolve[N[dolnaDiag], N[diag], N[hornaDiag], 
            N[pStrana]]; 
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}];)

OkrajUl[n, 0, π, 0, -π, a0, a1, a2, f, pr]

g1 = 
      Plot[pr[x], {x, a, b}, PlotRange -> All, 
        DisplayFunction -> Identity]
g2 = 
      ListPlot[graf, 
        PlotStyle -> {RGBColor[0, 0.7, 0], PointSize[0.02]}, 
        DisplayFunction -> Identity]
Show[g1, g2, DisplayFunction -> $DisplayFunction]