f[x_]:=8*Sin[x];
n=9;
a=0;
b=π;
ya=-5;
yb=-3;
p[x_]:=Sin[x];
q[x_]:=-2*Sin[x];
presneriesenie[x_]:=-Cos[x]-4;

Okrsamoadj[n_,xleft_,xright_,Leftbv_,Rightbv_,p_,q_,f_,pres_]:=
(
h=N[(xright-xleft)/(n+1)];
ps=Join[{f[xleft+h]*h^2+p[xleft+h/2]*Leftbv},
Table[h^2*f[xleft+i*h],{i,2,n-1}],
{f[xleft+n*h]*h^2+p[xright-h/2]*Rightbv}];
diag=Table[p[xleft+i*h-h/2]+p[xleft+i*h+h/2]+h^2*q[xleft+i*h],{i,1,n}];
hdiag=Table[-p[xleft+i*h+h/2],{i,1,n-1}];
A=SparseArray[{Band[{1,2}]->hdiag,Band[{1,1}]->diag,Band[{2,1}]->hdiag},
{n,n}];


riesenie=LinearSolve[A,N[ps]];
 xs=Table[xleft+(i-1)*h,{i,1,n+2}];
ys=Join[{Leftbv},Table[riesenie[[i]],{i,1,n}],{Rightbv}];
Print[ys];
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}];

)

Okrsamoadj[n,a,b,ya,yb,p,q,f,presneriesenie];
g1=Plot[presneriesenie[x],{x,a,b},PlotRange->All,PlotStyle->RGBColor[0,1,0]];
g2=ListPlot[graf,PlotRange->All,PlotStyle->{RGBColor[1,0,1],PointSize[0.02]}];
Show[g1,g2]

(*modifikujeme na neumanove podm*)

f[x_]:=x^2+x+x*Sin[x]-Cos[x]-1;
n=50;
a=0;
b=π;
ya=0;
yb=0;(*hodnota derivacie*)
p[x_]:=x;
q[x_]:=x;
presneriesenie[x_]:=Sin[x]+x;

Okrsamoadjnp[n_,xleft_,xright_,Leftbv_,Rightbv_,p_,q_,f_,pres_]:=
(
h=N[(xright-xleft)/(n+1)];
ps=Join[{f[xleft+h]*h^2+p[xleft+h/2]*Leftbv},
Table[h^2*f[xleft+i*h],{i,2,n}],
{f[xleft+(n+1)*h]*h^2+p[xright-h/2]*Rightbv*2*h}];
diag=Table[p[xleft+i*h-h/2]+p[xleft+i*h+h/2]+h^2*q[xleft+i*h],{i,1,n+1}];
hdiag=Table[-p[xleft+i*h+h/2],{i,1,n}];
ddiag=Join[Table[-p[xleft+i*h+h/2],{i,1,n-1}],{-(p[xleft+(n+1)*h-h/2]+p[xleft+(n+1)*h+h/2])}];
A=SparseArray[{Band[{1,2}]->hdiag,Band[{1,1}]->diag,Band[{2,1}]->ddiag},
{n+1,n+1}];


riesenie=LinearSolve[A,N[ps]];
 xs=Table[xleft+(i-1)*h,{i,1,n+2}];
ys=Join[{Leftbv},Table[riesenie[[i]],{i,1,n+1}],{Rightbv}];
Print[ys];
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}];

)

Okrsamoadjnp[n,a,b,ya,yb,p,q,f,presneriesenie];
g1=Plot[presneriesenie[x],{x,a,b},PlotRange->All,PlotStyle->RGBColor[0,1,0]];
g2=ListPlot[graf,PlotRange->All,PlotStyle->{RGBColor[1,0,1],PointSize[0.02]}];
Show[g1,g2]