////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CDC-ECC'05                                                                                                 //
// Title : Interval Analysis in Dioid : Application to Robust Open-Loop Control for Timed Event Graphs        //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

mode(7);

    Am = smatrix(3,3); // state matrix
    Ap = smatrix(3,3);
    
    Bm = smatrix(3,2); // 
    Bp = smatrix(3,2);
    
    Cm = smatrix(1,3);
    Cp = smatrix(1,3);
    

	//state matrix declaration A = [Am,Ap]
    Am(1,1)=series(eps,[2 2],e); 
	Am(2,2)=series(eps,[3 3],e);
	Am(3,1)=series(eps,[0 3],e);
	Am(3,2)=series(eps,[0 2],e);
	Am(3,3)=series(eps,[3 2],e)
	
	//state matrix declaration
	Ap(1,1)=series(eps,[1 5],e);
	Ap(2,2)=series(eps,[2 3],e);
	Ap(3,1)=series(eps,[0 4],e);
	Ap(3,2)=series(eps,[0 6],e);
	Ap(3,3)=series(eps,[1 3],e)
	
	
	//Input and Output matrices B = [Bm,Bp] and C=[Cm,Cp]
	Bm(1,1)=series(eps,e,e);
	Bm(2,2)=series(eps,e,e)
	
	Bp(1,1)=series(eps,e,e);
	Bp(2,2)=series(eps,e,e)
	
	
	Cm(1,3)=series(eps,e,e);
	Cp(1,3)=series(eps,e,e)
	
	//Transfer matrix H = [Hm,Hp]
	
	Hm = Cm*stargd(Am)*Bm
	Hp = Cp*stargd(Ap)*Bp
	
	
	//output trajectory desired : Z = [Zm,Zp] >= Y=[Ym,Yp]=[Hm,Hp]*[Um,Up]
	Zm = series(eps,[0 18 ; 2 20 ; 3 25 ; 4 32 ; 5 35 ; 6 37 ; 7 %inf],[0 %inf])
	Zp = series(eps,[0 23 ; 2 28 ; 3 35 ; 4 38 ; 5 41 ; 6 45 ; 7 %inf],[0 %inf])
	
	
	//Greatest reachable  target interval Zproj = [Zmproj,Zpproj] = [Hm,Hp]*([Hm,Hp]\[Zm,Zp])
	Zmproj = Hm*( (Hm\Zm) ^ (Hp \ Zp) )
	Zpproj = Hp*( Hp \ Zp)
	
	
	//Greatest Input control U = [Um,Up]
	Um = (Hm \ Zm )  ^  (Hp \ Zp)
	Up = Hp \ Zp
	
	
	//Check if the output is equal to desired output Z
	Zm == Hm*Um
	Zp == Hp*Up
	
	
	//Check if the output is equal to greatest reachable target output Zproj
	Zmproj == Hm*Um
	Zpproj == Hp*Up
	
