#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: smo2ssl_r
# observer-form state matrices to Kobserver gain
# P.J.Gawthrop  Jan 1998
# Copyright (c) P.J.Gawthrop 1998

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.1  1998/01/23 09:31:03  peterg
## Initial revision
##
###############################################################

Nu=`mtt_getsize $1 u`
Ny=`mtt_getsize $1 y`

if [ "$Nu" = "1" ]; then
  if [ "$Ny" = "1" ]; then
    blurb=' for this siso system'    
  else
    blurb=" using first output of $Ny"
  fi
else
  if [ "$Ny" = "1" ]; then
    blurb=" using first input of $Nu"
  else
    blurb=" using first input of $Nu and using first output of $Ny"
  fi
fi


# Inform user
echo Creating $1_ssl.r $blurb

# Remove the old log file
rm -f smo2ssl_r.log

# Use reduce to accomplish the transformation
reduce >smo2ssl_r.log << EOF

in "$1_def.r";
in "$1_smo.r";

%Read the formatting function
in "$MTTPATH/trans/reduce_matrix.r";

OFF Echo;
OFF Nat;


%%%%  Observer design %%%%%
% gain in Observer form:
matrix MTTl_o(MTTNx,1);
matrix alpha_o(9,1);
alpha_o(1,1) := alpha_o1;
alpha_o(2,1) := alpha_o2;
alpha_o(3,1) := alpha_o3;
alpha_o(4,1) := alpha_o4;
alpha_o(5,1) := alpha_o5;
alpha_o(6,1) := alpha_o6;
alpha_o(7,1) := alpha_o7;
alpha_o(8,1) := alpha_o8;
alpha_o(9,1) := alpha_o9;

for i := 1:MTTNx DO
 MTTL_o(i,1) := alpha_o(i,1) + MTTA_o(i,1);

% Gain in physical form
MTTL := MTTT_o*MTTL_o;

%Create the output file
OUT "$1_ssl.r";

%Write out the matrices.


write "%  - Observer Gain matrix - observer form";
MTT_Matrix := MTTL_o$ 
MTT_Matrix_name := "MTTL_o"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNy$
Reduce_Matrix()$

write "%  - Gain matrix - physical form";
MTT_Matrix := MTTL$ 
MTT_Matrix_name := "MTTL"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNy$
Reduce_Matrix()$

write "END;";

SHUT "$1_ssl.r";
quit;

EOF

# Now invoke the standard error handling.
mtt_error_r smo2ssl_r.log
