Psor[u_,f_,n_,omega_,eps_]:=Module[{error,y,ni},
ni=0;
error=1.0;
While[error>eps*eps,
ni++;
error=0.0;
u[-n]=0.0;
u[n]=0.0;
For[i=-n+1,i<n,i++,
y=(u[i-1]+u[i+1])/2.0;
y=Max[f[i],u[i]+omega*(y-u[i])];
(*uz to robi spesl pre maticu -1, 2, -1  na diangonale - zosit Pg 4 6.4.09*)
error+=(u[i]-y)*(u[i]-y);
(*zastavime ak suma rozdielu 2och rieseni posebe ^2 je ≤ eps^2 *)
(*If[Mod[i,35]==0,Print["error ",error]];*)
u[i]=y]];
Print["error ",error];
Print["ni  ",ni];
]

n=80;
h=2.0/(2n);
obstacle[x_]=Max[0,0.5-x^4];
Do[x[i]=i*h,{i,-n,n}];
Do[f[i]=obstacle[i*h],{i,-n,n}];
Do[u[i]=f[i],{i,-n,n}];
g1=Plot[obstacle[x],{x,-1,1},PlotStyle->{RGBColor[1,0,0],Thickness[0.005]}];
g2=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1]];
gi=Show[g1,g2,PlotRange->All]

omega=1;
eps=0.000001;
Psor[u,f,n,omega,eps]

g3=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1]];
g4=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1],Joined->True];
gs=Show[g1,g2,g3,g4]

(*kolko iteracii bolo treba?;

pre n=20  iter 66; cas 7.437934057706975`*^-13 ;
pre n=40  iter 216 cas 9.84060592698628`*^-13  ;
pre n=80  iter 801 cas 9.970890500539697`*^-13

omega bolo 1.0
*)

n=20;
h=2.0/(2n);
obstacle[x_]=Max[0,(0.1/(x^2+2))*Cos[16x]];
Do[x[i]=i*h,{i,-n,n}];
Do[f[i]=obstacle[i*h],{i,-n,n}];
Do[u[i]=f[i],{i,-n,n}];
g1=Plot[obstacle[x],{x,-1,1},PlotStyle->{RGBColor[1,0,0],Thickness[0.005]}];
g2=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1]];
gi=Show[g1,g2,PlotRange->All];
omega=1.6;
eps=0.000001;
Psor[u,f,n,omega,eps]
g3=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1]];
g4=ListPlot[Table[{x[i],u[i]},{i,-n,n}],PlotStyle->RGBColor[0,0,1],Joined->True];
gs=Show[g1,g2,g3,g4,PlotRange->All]

Plot[x*Cos[10x],{x,-1,1}]