for {j in UNIT1}{
	let b1[j,1] := z1lo[j] ;
	let b1[j,NK1+1]:= z1up[j] ;
	let LIST := {1 , NK1+1} ;
	for { k in LIST}{
		let slope1[j,k]:= C1[j]*exp(b1[j,k]) ;
		let beta1[j,k] := C1[j]*exp(b1[j,k]) ;
		let LISTleft := {1} ;
		let LISTright:= {NK1+1} ;
	}
	for {c in 1..nk1}{
		for {k in LIST diff LISTright}{
			let lo := k ;
			let up := next(k,LIST) ;
			let new := (up+lo)/2 ;
			let LISTrightaux := {aux in LIST: aux >= up} ;
			let LISTleftaux := LIST diff LISTrightaux ;
			#let b1[j,new]:= (beta1[j,up]-beta1[j,lo] + slope1[j,lo]*b1[j,lo] - slope1[j,up]*b1[j,up])/(slope1[j,lo] - slope1[j,up]) ;
			let b1[j,new] := b1[j,1] + (new-1)*a1[j] ;
			let beta1[j,new] := C1[j]*exp(b1[j,new]) ;
			let slope1[j,new]:= C1[j]*exp(b1[j,new]) ; 
			let LISTaux := { new } ;
			let LISTleft := LISTleftaux union LISTaux  ;
			let LIST := LISTleft union LISTrightaux ;
		}
	}
	for {k in TMIP1}{
		let alpha1[j,k] := (beta1[j,k+1]-beta1[j,k])/(b1[j,k+1]-b1[j,k]) ;
	}
}
#display b1,beta1,alpha1 ;

for {j in UNIT2}{
	let b2[j,1] := z2lo[j] ;
	let b2[j,NK2+1]:= z2up[j] ;
	let LIST := {1 , NK2+1} ;
	for { k in LIST}{
		let slope2[j,k]:= C2[j]*exp(b2[j,k]) ;
		let beta2[j,k] := C2[j]*exp(b2[j,k]) ;
		let LISTleft := {1} ;
		let LISTright:= {NK2+1} ;
	}
	for {c in 1..nk2}{
		for {k in LIST diff LISTright}{
			let lo := k ;
			let up := next(k,LIST) ;
			let new := (up+lo)/2 ;
			let LISTrightaux := {aux in LIST: aux >= up} ;
			let LISTleftaux := LIST diff LISTrightaux ;
			#  let b2[j,new]:= (beta2[j,up]-beta2[j,lo] + slope2[j,lo]*b2[j,lo] - slope2[j,up]*b2[j,up])/(slope2[j,lo] - slope2[j,up]) ;
			let b2[j,new] := b2[j,1] + (new-1)*a2[j] ;
			let slope2[j,new]:= C2[j]*exp(b2[j,new]) ;
			let beta2[j,new] := C2[j]*exp(b2[j,new]) ;
			let LISTaux := { new } ;
			let LISTleft := LISTleftaux union LISTaux  ;
			let LIST := LISTleft union LISTrightaux ;
		}
	}
	for {k in TMIP2}{
		let alpha2[j,k] := (beta2[j,k+1]-beta2[j,k])/(b2[j,k+1]-b2[j,k]) ;
	}
}

for {j in UNIT3}{
	let b3[j,1] := z3lo[j] ;
	let b3[j,NK3+1]:= z3up[j] ;
	let LIST := {1 , NK3+1} ;
	for { k in LIST}{
		let slope3[j,k]:= C3[j]*exp(b3[j,k]) ;
		let beta3[j,k] := C3[j]*exp(b3[j,k]) ;
		let LISTleft := {1} ;
		let LISTright:= {NK3+1} ;
	}
	for {c in 1..nk3}{
		for {k in LIST diff LISTright}{
			let lo := k ;
			let up := next(k,LIST) ;
			let new := (up+lo)/2 ;
			let LISTrightaux := {aux in LIST: aux >= up} ;
			let LISTleftaux := LIST diff LISTrightaux ;
			let b3[j,new] := b3[j,1] + (new-1)*a3[j] ;
			let slope3[j,new]:= C3[j]*exp(b3[j,new]) ;
			let beta3[j,new] := C3[j]*exp(b3[j,new]) ;
			let LISTaux := { new } ;
			let LISTleft := LISTleftaux union LISTaux  ;
			let LIST := LISTleft union LISTrightaux ;
		}
	}
	for {k in TMIP3}{
		let alpha3[j,k] := (beta3[j,k+1]-beta3[j,k])/(b3[j,k+1]-b3[j,k]) ;
	}
}

for {(i,h,j) in PROUTES: j in STAGES3}{
	let b6[i,h,j,1] := z6lo[i,h,j] ;
	let b6[i,h,j,NK6+1]:= z6up[i,h,j] ;
	let LIST := {1 , NK6+1} ;
	for { k in LIST}{
		let slope6[i,h,j,k]:= exp(t1[i,h,j]+b6[i,h,j,k])/(T0[i,h,j]+exp(t1[i,h,j]+b6[i,h,j,k])) ;
		let beta6[i,h,j,k] := log(T0[i,h,j]+exp(t1[i,h,j]+b6[i,h,j,k])) ;
		let LISTleft := {1} ;
		let LISTright:= {NK6+1} ;
	}
	for {c in 1..nk6}{
		for {k in LIST diff LISTright}{
			let lo := k ;
			let up := next(k,LIST) ;
			let new := (up+lo)/2 ;
			let LISTrightaux := {aux in LIST: aux >= up} ;
			let LISTleftaux := LIST diff LISTrightaux ;
			# let b6[i,h,j,new]:= (beta6[i,h,j,up]-beta6[i,h,j,lo] + slope6[i,h,j,lo]*b6[i,h,j,lo] - slope6[i,h,j,up]*b6[i,h,j,up])/(slope6[i,h,j,lo] - slope6[i,h,j,up]) ;
			let b6[i,h,j,new] := b6[i,h,j,1] + (new-1)*a6[i,h,j] ;
			let slope6[i,h,j,new]:= exp(t1[i,h,j]+b6[i,h,j,new])/(T0[i,h,j]+exp(t1[i,h,j]+b6[i,h,j,new])) ;
			let beta6[i,h,j,new] := log(T0[i,h,j]+exp(t1[i,h,j]+b6[i,h,j,new])) ;
			let LISTaux := { new } ;
			let LISTleft := LISTleftaux union LISTaux  ;
			let LIST := LISTleft union LISTrightaux ;
		}
	}
	for {k in TMIP6}{
		let alpha6[i,h,j,k] := (beta6[i,h,j,k+1]-beta6[i,h,j,k])/(b6[i,h,j,k+1]-b6[i,h,j,k]) ;
	}
}

for {(i,h) in HP}{
	let b7[i,h,1] := z7lo[i,h] ;
	let b7[i,h,NK7+1]:= z7up[i,h] ;
	let LIST := {1 , NK7+1} ;
	for { k in LIST}{
		let slope7[i,h,k]:= exp(b7[i,h,k]+log(d[i])-log(coef_delta)) ;
		let beta7[i,h,k] := exp(b7[i,h,k]+log(d[i])-log(coef_delta)) ;
		let LISTleft := {1} ;
		let LISTright:= {NK7+1} ;
	}
	for {c in 1..nk7}{
		for {k in LIST diff LISTright}{
			let lo := k ;
			let up := next(k,LIST) ;
			let new := (up+lo)/2 ;
			let LISTrightaux := {aux in LIST: aux >= up} ;
			let LISTleftaux := LIST diff LISTrightaux ;
			#let b7[i,h,new]:= (beta7[i,h,up]-beta7[i,h,lo] + slope7[i,h,lo]*b7[i,h,lo] - slope7[i,h,up]*b7[i,h,up])/(slope7[i,h,lo] - slope7[i,h,up]) ;
			let b7[i,h,new]:= b7[i,h,1] + (new-1)*a7[i,h] ;
			let slope7[i,h,new]:= exp(b7[i,h,new]+log(d[i])-log(coef_delta)) ;
			let beta7[i,h,new] := exp(b7[i,h,new]+log(d[i])-log(coef_delta)) ;
			let LISTaux := { new } ;
			let LISTleft := LISTleftaux union LISTaux  ;
			let LIST := LISTleft union LISTrightaux ;
		}
	}
	for {k in TMIP7}{
		let alpha7[i,h,k]:= (beta7[i,h,k+1]-beta7[i,h,k])/(b7[i,h,k+1]-b7[i,h,k]) ;
	}
}
#display b7,beta7,slope7,alpha7 ;
