(*eulerova metoda*)

f[x_,y_]:=x*y^2
h=0.05;
x0=0;
y0=1;
koniec=1;

Numries[x_,y_,f_,h_]:=y+h*f[x,y]
Pres[x_]:=2/(2-x^2)

x[0]=x0;
y[0]=y0;
n=koniec/h;

Do[{x[i]=x0+i*h,y[i]=Numries[x[i-1],y[i-1],f,h]},{i,1,n,1}]
data=Table[{x[i],y[i]},{i,0,n}];
data1=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}];
vysledky=Table[{x[i],y[i],Pres[x[i]], Abs[(y[i]-Pres[x[i]])]},{i,0,n}];
maxerror=Max[data1];

PaddedForm[TableForm[vysledky],{12,4}]

g=Plot[Pres[x],{x,0,2},PlotRange->{{0,1},{0,2}},PlotStyle->{RGBColor[1,0,0],Thickness[0.005]}]

graf1=ListPlot[data,PlotRange->{{0,1},{0,2}},PlotStyle->{RGBColor[0,0,1]}]

Show[g,graf1]

(*vypocet EOC rad chyby*)
h=0.001;
Do[{hz=h/j,
n=koniec/hz,
Do[{x[i]=x0+i*hz,
y[i]=Numries[x[i-1],y[i-1],f,hz]},
{i,1,n,1}],
data=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}],
error[j]=Max[data]},{j,1,2,1}]

a1=error[1]
b1=error[2]
rad=N[Log[2,a1/b1]]

sol4=DSolve[{a'[t]==a[t]+Cos[t*a[t]],a[0]==0},a[t],t]

sol5=NDSolve[{a'[t]==a[t]+Cos[t*a[t]],a[0]==0},a,{t,0,2*Pi}]

zmathem=Plot[{a[t]/.sol5},{t,0,2*Pi}]

f[x_,y_]:=y+Cos[y*x]
h=N[2*Pi/1000];
x0=0;
y0=0;
koniec=2*Pi;

Numries[x_,y_,f_,h_]:=y+h*f[x,y]
Pres[x_]:=2/(2-x^2)

x[0]=x0;
y[0]=y0;
n=koniec/h;

Do[{x[i]=x0+i*h,y[i]=Numries[x[i-1],y[i-1],f,h]},{i,1,n,1}]
data=Table[{x[i],y[i]},{i,0,n}];
data1=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}];
vysledky=Table[{x[i],y[i],Pres[x[i]], Abs[(y[i]-Pres[x[i]])]},{i,0,n}];
maxerror=Max[data1];

graf2=ListPlot[data,PlotStyle->{RGBColor[0,1,0]}]

Show[graf2,zmathem]

(*samostanta praca*)

solPresne=DSolve[{a'[t]==a[t]^2,a[0]==-4},a[t],t]

f[x_,y_]:=y^2;
h=0.01;
x0=0;
y0=-4;
L=1;
koniec=L;

Numries[x_,y_,f_,h_]:=y+h*f[x,y]
Pres[x_]:=-4/(1+4x)

x[0]=x0;
y[0]=y0;
n=koniec/h;

Do[{x[i]=x0+i*h,y[i]=Numries[x[i-1],y[i-1],f,h]},{i,1,n,1}]
data=Table[{x[i],y[i]},{i,0,n}];
data1=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}];
vysledky=Table[{x[i],y[i],Pres[x[i]], Abs[(y[i]-Pres[x[i]])]},{i,0,n,10}];
maxerror=Max[data1];

PaddedForm[TableForm[vysledky],{n,4}]

graf2=ListPlot[data,PlotStyle->{RGBColor[0,1,0]}]

Do[{hz=h/j,
n=koniec/hz,
Do[{x[i]=x0+i*hz,
y[i]=Numries[x[i-1],y[i-1],f,hz]},
{i,1,n,1}],
dataY=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}],
error1[j]=Max[dataY]},{j,1,2,1}]

a1=error1[1]
b1=error1[2]
rad=N[Log[2,a1/b1]]

f[x_,y_]:=y^2;
h=0.005;
x0=0;
y0=-4;
L=1;
koniec=L;

x[0]=x0;
y[0]=y0;
n=koniec/h;

Do[{x[i]=x0+i*h,y[i]=Numries[x[i-1],y[i-1],f,h]},{i,1,n,1}]
dataX=Table[{x[i],y[i]},{i,0,n}];
dataX1=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}];
vysledky1=Table[{x[i],y[i],Pres[x[i]], Abs[(y[i]-Pres[x[i]])]},{i,0,n,10}];
maxerror=Max[data1];

PaddedForm[TableForm[vysledky1],{n,4}]

graf4=ListPlot[dataX,PlotStyle->{RGBColor[0,0,1]}]

graf3=Plot[Pres[x],{x,0,L},PlotStyle->{RGBColor[1,0,0],Thickness[0.005]}]

Show[graf4,graf2,graf3]

Do[{hz=h/j,
n=koniec/hz,
Do[{x[i]=x0+i*hz,
y[i]=Numries[x[i-1],y[i-1],f,hz]},
{i,1,n,1}],
dataZ=Table[Abs[(y[i]-Pres[x[i]])],{i,0,n}],
error2[j]=Max[dataZ]},{j,1,2,1}]

a1=error2[1]
b1=error2[2]
rad=N[Log[2,a1/b1]]

(*Metoda runge-kutta*)