KBM[SlowEqus_List, AveModel_List, SlowVars_List, n_Integer] :=
	Block[{f, g, u, i, j, k, BCs, xs, T},
	f = Table[SlowEqus[[i, 1]],
                  {i, 1, Length[SlowEqus]}];
	g = {AveModel};
	BCs = Table[SlowEqus[[i, 2]],
                    {i, 1, Length[SlowEqus]}];
	BCs = Join[{0}, BCs];
	xs = SlowVars;
	T = {0};
	For[i=2, i<=Length[BCs], i++,
	    T = AppendTo[T, T[[i-1]] + BCs[[i]] ]];

	u = {FindUi[f, g[[1]], BCs]};

	If[n>1, For[k=2, k<=n, k++,
	    f = Table[-Sum[JacobiMatrix[u[[j, i]], xs] . g[[k - j]],
                    {j, 1, k - 1}] /.
                 {t -> t + T[[i]]}, {i, 1, Length[T]-1}];
	    g = AppendTo[g, Average[f, BCs]];
	    u = AppendTo[u, FindUi[f, g[[k]], BCs]] ]];
	{g, u}
	]
FindUi[fi_List, gi_List, BCs_List] := 
	Block[{uit, ui, uiDC, i, T},
	T = {0};
	For[i=2, i<=Length[BCs], i++,
	    T = AppendTo[T, T[[i-1]] + BCs[[i]] ]];
	uit = fi - Table[gi, {i, 1, Length[fi]}];
	ui = Matching[uit, BCs];
	uiDC = Average[ui, BCs];
        Table[Simplify[ui[[i]] - uiDC /. {t -> t - T[[i]]}],
              {i, 1, Length[ui]}]
	]
Average[f_List, BCs_List] :=
	Cancel[Sum[Integrate[f[[i]], 
                             {t, 0, BCs[[i+1]]}],
                   {i, 1, Length[f]}]/
               Sum[BCs[[i]], {i, 2, Length[BCs]}]]
JacobiMatrix[f_List, x_List] := 
	Transpose[Table[D[f, x[[i]]], {i, 1, Length[f]}]]
Matching[f_List, BCs_List] := Block[{ui, i, j},
	ui = Simplify[Integrate[f, t]];
	For[i=2, i<=Length[f], i++,
	    ui[[i]] = ui[[i]] -
                      Evaluate[ui[[i]] /. {t -> 0}] +
                      Evaluate[ui[[i-1]] /. {t -> BCs[[i]]}]];
	Simplify[ui] ]
KBMPWM[A_List, B_List, NetIn_List, SlowVars_List, 
       AbsBCs_List, n_Integer] :=
	Block[{xs, f, g, u, BCs, t, i, j, k},
	xs = SlowVars;
	BCs = {0};
	For[i=1, i<=Length[AbsBCs]-1, i++,
	    BCs = AppendTo[BCs, AbsBCs[[i+1]] - AbsBCs[[i]]]];
	f = Table[A[[i]] . xs + B[[i]] . NetIn,
                  {i, 1, Length[A]}];
	g = {Average[f, BCs]};
	u = {FindUi[f, g[[1]], BCs]};
	If[n>1, For[k=2, k<=n, k++,
	    f = Table[A[[i]] . u[[k-1, i]] -
                Sum[JacobiMatrix[u[[j, i]], xs] . g[[k - j]],
                    {j, 1, k - 1}] /.
                 {t -> t + AbsBCs[[i]]}, {i, 1, Length[A]}];
	    g = AppendTo[g, Average[f, BCs]];
	    u = AppendTo[u, FindUi[f, g[[k]], BCs]] ]];
	{g, u}
	]