Quiet@Remove["Global`*"];
SetDirectory[NotebookDirectory[]];
$HistoryLength=3;

(*reseni preurcenych soustav rovnic*)
Quiet@Remove["Global`*"];
SeedRandom[1];
rce=(100+Random[])*x+(150*Random[])*y==Random[]&/@Range[10];
norm[vel_]:=(vel^2);
proMin=Total[rce/.lhs_==rhs_:>norm[lhs-rhs]];
Simplify[0==D[proMin,#]&/@{x,y}]
Solve[%]

{bPom,A}=Normal[CoefficientArrays[rce,{x,y}]];
MatrixForm/@%;
b=-bPom;
Thread[A.{x,y}==b];
Thread[A\[Transpose].A.{x,y}==A\[Transpose].b]
Solve[%]

(*regrese, nejprve linearni*)
Quiet@Remove["Global`*"];
SeedRandom[1];
k=.5;
rn:=k*RandomReal[{-1,1}];
vyraz[x_]:=(x^2)-2x+1;
data={#,vyraz[#]+rn}&/@Range[-2,4,.1];
pl1=ListPlot[data,PlotStyle->{Red, PointSize[0.015]}];
norm[vel_]:=(vel^2);
err[{x_,y_}]:=norm[y-(a*(x^2)+b*x+c)];
proMin=Total[err/@data];
Simplify[0==D[proMin,#]&/@{a,b,c}];
Solve[%][[1]];
nahrada=a*(x^2)+b*x+c/.%
Fit[data,{1,x,(x^2)},x]
pl2=Plot[nahrada,{x,-2,4}];
Show[pl1,pl2]

Quiet@Remove["Global`*"];
SeedRandom[1];
k=.1;
rn:=k*RandomReal[{-1,1}];
vyraz[x_,y_]:=(x^2)-2x+1+x*y+(y^2)+3y;
data=Flatten[Table[{x,y,rn+vyraz[x,y]},{x,-2,4,.1},{y,-3,3,0.5}],1];
norm[vel_]:=(vel^2);
err[{x_,y_,z_}]:=norm[z-(a*(x^2)-b*x+c+d*x*y+e*(y^2)+f*y)];
proMin=Simplify[Total[err/@data]];
proms=Cases[proMin,_Symbol,{0,∞}]//Union;
0==D[proMin,#]&/@proms;
(a*(x^2)-b*x+c+d*x*y+e*(y^2)+f*y)/.Solve[%][[1]]
Fit[data,{1,x,(x^2),x*y,y,(y^2)},{x,y}]

(*regrese, ted nelinearni*)
Quiet@Remove["Global`*"];
SeedRandom[1];
k=.2;
rn:=k*RandomReal[{-1,1}];
vyraz[t_]:=3+5*Exp[((-t)/0.5)];
data={#,vyraz[#]+rn}&/@Range[0,3,0.1];
pl1=ListPlot[data,PlotStyle->{Red, PointSize[0.015]},AxesOrigin->{0,0}];
norm[vel_]:=(vel^2);
err[{t_,u_}]:=norm[u-(a+b*Exp[((-t)/τ)])];
proMin=Total[err/@data];
nahrada=(a+b*Exp[((-t)/τ)])/.NMinimize[proMin,{a,b,τ}][[2]]
pl2=Plot[nahrada,{t,0,3},PlotRange->All];
Show[pl1,pl2,PlotRange->All];
a+b*Exp[((-t)/τ)]/.FindFit[data,a+b*Exp[((-t)/τ)],{a,b,τ},t,Method->NMinimize]

(*synteza obvodu*)
Quiet@Remove["Global`*"];
prechod[odkud_,kam_,kde_,strmost_,var_]:=Module[{a,b,sol,rce},
rce={a+b==kam,a-b==odkud};
sol=Solve[rce,{a,b}][[1]];
a+b*Tanh[strmost*(var-kde)]/.sol
];
prenosIdeal[ω_]:=prechod[1,0.3,500,0.005,ω];
ωMin=1.;
ωMax=100000;
LogLogPlot[prenosIdeal[ω],{ω,ωMin,ωMax},AxesOrigin->{1,0.1},PlotStyle->Thick];
c=47*(10^(-6));
absPrenos[ω_]:=Abs[((R2+(1/(I*ω*c)))/(R1+(1/(I*ω*c))+R2))];
n=100;
omegy=ωMin*(((((ωMax)/(ωMin))))^((#-1)/n))&/@Range[n+1];
norm=((#)^2)&;
err[ω_]:=norm[prenosIdeal[ω]-absPrenos[ω]]
proMin=Total[err/@omegy];
min=NMinimize[proMin,{R1,R2}][[2]];
vyslednyPrenos=Abs[((R2+(1/(I*ω*c)))/(R1+(1/(I*ω*c))+R2))]/.min;
LogLogPlot[{prenosIdeal[ω],vyslednyPrenos},{ω,ωMin,ωMax},AxesOrigin->{1,0.1},PlotStyle->{{Red,Thick},{Blue,Thick}},GridLines->Automatic]