#! /bin/sh

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

# Bourne shell script: dm2fr_m

# Transformation: descriptor matrix to frequency response

# Copyright (c) P.J.Gawthrop, 1996.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.14  2000/05/20 15:35:18  peterg
## Parameterised version
##
## Revision 1.13  1998/09/03 09:12:32  peterg
## Removed debugging lines
##
## Revision 1.12  1998/09/02 12:02:14  peterg
## Added mttinput paramater
##
## Revision 1.11  1998/08/11 13:32:42  peterg
## Lowercase mttLAST etc
##
## Revision 1.10  1998/07/27 20:27:25  peterg
## Now get parameters for simpar file.
##
## Revision 1.9  1998/02/26 15:12:26  peterg
## Removed parameter stuff
##
# Revision 1.8  1996/08/30  14:54:36  peter
# Took back lock.
#
## Revision 1.7  1996/08/24  14:23:00  peter
## Global parameter passing.
## Proper error handling.
#
## Revision 1.6  1996/08/16 14:26:07  peter
## Fixed multi-output bug.
## Made sure that u0 is ok.
##
## Revision 1.5  1996/08/15 16:23:02  peter
## Now uses W in place of w for consistancy with time responses.
##
## Revision 1.4  1996/08/15 11:52:42  peter
## Now creats a number of versions:
## fr 	complex frequency response
## lmfr 	log  magnitude of fr
## lpfr  	phase of fr
## nyfr  	real and imag parts - Nyquist style
## nifr 	Nichols style.
##
## Revision 1.3  1996/08/11 19:08:08  peter
## Parameter passing now enabled.
##
## Revision 1.2  1996/08/11 09:32:12  peter
## Now takes the numpar parameters correctly
##
## Revision 1.1  1996/08/10 14:11:11  peter
## Initial revision
##
###############################################################

echo Creating $1_fr.m
echo Creating $1_lmfr.m
echo Creating $1_lpfr.m
echo Creating $1_nyfr.m
echo Creating $1_nifr.m

#if [ "$2" = "" ]; 
#then
#  PARAMS='W=logspace(-1,2,100); u0=zeros(nu,1); u0(1)=1;'
#  echo Using default parameter $PARAMS
#else
#  PARAMS=$2;
#fi

$MATRIX << EOF > dm2fr_m.log 2>mtt_error.txt

# Data
  mttpar=$1_numpar;

# Simulation parameters
  simpar=$1_simpar;

  [nx,ny,nu,nz,nyz] = $1_def;

  %Defaults
  
  if (struct_contains(simpar,"wmin")==0)
    simpar.wmin = -1;
  end;

  if (struct_contains(simpar,"wmax")==0)
    simpar.wmax = 2
  end;

  if (struct_contains(simpar,"wsteps")==0)
    simpar.wsteps = 100;
  end;

  W = logspace(simpar.wmin,simpar.wmax,simpar.wsteps)';
  if (struct_contains(simpar,"mttinput")==0)
    mttinput = 1;
  end;
  mttu0 = zeros(nu,1);
  mttu0(mttinput) = 1;

  [n,m]=size(W);
  if m>n
    W=W';
  end;

  [n,m]=size(mttu0);
  if m>n
    mttu0=mttu0';
  end;

  [A,B,C,D,E] = $1_dm(mttpar);
  fr = dm2fr(A,B,C,D,E,W,mttu0);
  lw = log10(W);
  lmfr = log10(abs(fr));
  pfr = angle(fr)*180/pi;

% Complex frequency response
  write_matrix([W fr], "$1_fr");

% Log magnitude v log frequency
  write_matrix([lw lmfr], "$1_lmfr");

% Angle v log frequency
  write_matrix([lw pfr], "$1_lpfr");

% Nyquist style
  re = real(fr);
  im = imag(fr);
  nyq = [re(:,1) im(:,1)];
  for i = 2:ny
    nyq = [nyq re(:,i) im(:,i)];
  end;
  write_matrix(nyq, "$1_nyfr");

% Nichols style
  re = lmfr;
  im = pfr;
  nic = [im(:,1) re(:,1)];
  for i = 2:ny
    nic = [nic im(:,i) re(:,i)];
  end;
  write_matrix(nic, "$1_nifr");

EOF

# Now invoke the standard error handling,"
mtt_error mtt_error.txt






