function [A,B,C,D]=linall(fun,st,x,u,para,xpert,upert,coeff1,coeff2,coeff3,coeff4,coeff5)   
%LINALL	Linearization routine used by DLINMOD.
%	LINALL Obtains linear models from systems of ordinary differential 
%	equations (ODEs) and difference equations. No conversions 
%	are performed between sample times. This file is used by the 
%	function DLINMOD.
%
%	See also, DLINMOD, LINMOD, LINMOD2.  

%	Copyright (c) 1990-94 by The MathWorks, Inc.

%	Andrew Grace 11-12-90.
 

% ---------------Options--------------------
eval(['sizes =',fun, ';']);
eval(['[sizes,x0,str,ts, tsx] =',fun,'([],[],[],0);']);
if ~isempty(tsx), tsx = tsx(:,1); end
sizes=[sizes(:); zeros(6-length(sizes),1)];
nxz=sizes(1)+sizes(2); nu=sizes(4); ny=sizes(3);
nx=sizes(1);

if nargin<2, error('Must give sample time to convert to.'); end
if nargin<3, x=[]; end
if nargin<4, u=[]; end
if nargin<5, para=[]; end
if nargin<6, xpert=[]; end
if nargin<7, upert=[]; end

if isempty(u), u=zeros(nu,1); end
if isempty(x), x=zeros(nxz,1); end
if isempty(para) , para=[0;0]; end
if para(1)==0, para(1)=1e-5; end
if isempty(upert), upert=para(1)+1e-3*para(1)*abs(u); end
if isempty(xpert), xpert=para(1)+1e-3*para(1)*abs(x); end
t = st;

% ------------------String for eval--------------- 
outstr=[];
if  exist('coeff1')  % Put in coeffs
		for i=1:nargin-6
			outstr=[outstr,',coeff',num2str(i)];
		end
end
outstr=[outstr,')'];
fundx=['dx=',fun,'(t, x,u,6',outstr, ';'];
funds=['ds=',fun,'(t, x,u,7',outstr, ';'];
funy=['y=',fun, '(t, x,u,8',outstr, ';'];

A=zeros(nxz,nxz); B=zeros(nxz,nu); C=zeros(ny,nxz); D=zeros(ny,nu);

% Initialization
y=0;
eval(fundx);
eval(funds);
dall = [dx; ds];
if ny > 0, eval(funy); end
olddall=dall; oldu=u; oldy=y; oldx=x;

% A amd C matrices
for i=1:nxz;
	x(i)=x(i)+xpert(i);
	eval(fundx);
	eval(funds);
	dall = [dx; ds];
 	A(:,i)=(dall-olddall)./xpert(i);
	if ny > 0
		eval(funy);
		C(:,i)=(y-oldy)./xpert(i);
	end
	x=oldx;
end

% B and D matrices
for i=1:nu
	u(i)=u(i)+upert(i);
	eval(fundx);
	eval(funds);
	dall = [dx; ds];
	if ~isempty(B),
		B(:,i)=(dall-olddall)./upert(i);
	end
	if ny > 0
		eval(funy); 
		D(:,i)=(y-oldy)./upert(i);
	end
	u=oldu;
end
