chapter 13 m-files.docx

Upload: santoshraju

Post on 14-Apr-2018

223 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/27/2019 Chapter 13 m-files.docx

    1/30

    1

    Chapter 13 m-filesfunction m = oe(varargin)

    %OE Computes the prediction error estimate of an Output Error model.

    % M = OE(Z,[nb nf nk]) or M = OE(Z,'nb',nb,'nf',nf,'nk',nk)

    % M : returns the estimated model in an IDPOLY object format

    % along with estimated covariances and structure information.

    % For the exact format of M see also help IDPOLY.

    % Z : The estimation data in IDDATA object format. See help IDDATA

    % [nb nf nk] are the orders and delays of the Output Error model

    % y(t) = [B(q)/F(q)] u(t-nk) + e(t)

    % For multi-input data, nb, nf and nk are row vectors with length equal to the number of input channels.

    % An alternative syntax is M = OE(Z,Mi), where Mi is an estimated model or created by IDPOLY.

    % The minimization is then initialized at the parameters given in Mi.

    % By M = OE(Z,nn,Property_1,Value_1, ...., Property_n,Value_n)

    % all properties associated with the model structure and the algorithm can be affected. See help IDPOLY for a list

    % of Property/Value pairs.

    % Copyright 1986-2001 The MathWorks, Inc.

    % $Revision: 1.13 $ $Date: 2001/04/06 14:22:28 $

    try

    [mdum,z] = pemdecod('oe',varargin{:});

    catch

    error(lasterr)

    end

    err = 0;

    if isempty(pvget(z,'Name'))

    z=pvset(z,'Name',inputname(1));

    end

    if isa(mdum,'idpoly')

    nd = pvget(mdum,'nd'); nc = pvget(mdum,'nc'); na = pvget(mdum,'na');

    if sum([nd na nc])~=0

    err = 1;

    end

    else

    err = 1;

    end

    if err

    error('This is not an OE model.')

    end

    % $$$ fixp = pvget(mdum,'FixedParameter'); $$$ if ~isempty(fixp) $$$

    % warning(sprintf(['To fix a parameter, first define a nominal

    % model.',... $$$ '\nNote that mnemonic Parameter Names can be set by

    % SETPNAME.'])) $$$ end

    try

    m = pem(z,mdum);

    catcherror(lasterr)

    end

    es = pvget(m,'EstimationInfo');

    es.Method = 'OE';

    m = pvset(m,'EstimationInfo',es);

  • 7/27/2019 Chapter 13 m-files.docx

    2/30

    2

    function m = bj(varargin)

    %BJ Computes the prediction error estimate of a Box-Jenkins model.

    % M = BJ(Z,[nb nc nd nf nk]) or

    % M = BJ(Z,'nb',nb,'nc',nc,'nd',nd,'nf',nf,'nk',nk)

    % (Omitted orders are taken as zero, and the argument order is arbitrary)

    % M : returns the estimated model in an IDPOLY object format

    % along with estimated covariances and structure information.

    % For the exact format of M see also help IDPOLY.

    % Z : The estimation data in IDDATA object format. See help IDDATA

    % [nb nc nd nf nk] are the orders and delays of the Box-Jenkins model

    % y(t) = [B(q)/F(q)] u(t-nk) + [C(q)/D(q)]e(t)

    % An alternative is M = BJ(Z,Mi), where

    % Mi is an estimated model or created by IDPOLY.

    % The minimization is then initialized at the parameters given in Mi.

    % By M = BJ(Z,nn,Property_1,Value_1, ...., Property_n,Value_n)

    % all properties associated with the model structure and the algorithm

    % can be affected. See HELP IDPOLY and IDPROPS ALGORITHM for a list of

    % Property/Value pairs.% Copyright 1986-2001 The MathWorks, Inc.

    % $Revision: 1.11 $ $Date: 2001/04/06 14:22:25 $

    try

    [mdum,z] = pemdecod('bj',varargin{:});

    catch

    error(lasterr)

    end

    err = 0;

    if isempty(pvget(z,'Name'))

    z=pvset(z,'Name',inputname(1));

    end

    if isa(mdum,'idpoly')

    if pvget(mdum,'na')~=0

    error('This is not a BJ model.')

    end

    end

    % $$$ fixp = pvget(mdum,'FixedParameter');

    % $$$ if ~isempty(fixp)

    % $$$ warning(sprintf(['To fix a parameter, first define a nominal model.',...

    % $$$ '\nNote that mnemonic Parameter Names can be set by SETPNAME.']))

    % $$$ end

    try

    m = pem(z,mdum);catch

    error(lasterr)

    end

    es = pvget(m,'EstimationInfo');

    es.Method = 'BJ';

    m = pvset(m,'EstimationInfo',es);

  • 7/27/2019 Chapter 13 m-files.docx

    3/30

    3

    % function G=pfract(Num,Den)% The command pfract(Num,Den)generates the partial fractions% of a ratio of polynomials with numerator Num and denominator Den.% The poles cannot be repeated but may be real or complex.% The output will be the partial fractions of Num/Den plus any nonzero

    % remainder if the numerator order is equal to the denominator order.% Note, the partial fraction outputs erroneously refer to input 1,% input 2, etc; ignore these comments.function G=pfract(Num,Den)n=length(Den)-1;[RR,PP,K]=residue(Num,Den);if K~=0 % Check to see if there is a remainder.

    'Remainder', Kendm=1;q=1;

    P(n)=PP(n);R(n)=RR(n);while m

  • 7/27/2019 Chapter 13 m-files.docx

    4/30

    4

    function [f,t] = PulseSeries(T,m,F,N,NumPPP)

    % T=pulse width, sec.% m is an integer% mT=time between pulses, sec. (period of function)% N=number of pulses% NumPPP=number of points per pulsedt=T/NumPPP;% time increment, sec.

    t=0:dt:T*m*N-2*dt;% time value

    for L=1:NumPPPf(L)=F;

    endfor L=NumPPP+1:m*NumPPP-1f(L)=0;end

    for k=1:N-1

    for L=(m*k)*NumPPP:(m*k+1)*NumPPP

    f(L)=F;endfor L=(m*k+1)*NumPPP+1:(m*(k+1))*NumPPP-1f(L)=0;end

    end%length(t)%length(f)plot(t,f,'r','Linewidth',4)xlabel('time, sec.')ylabel('pulse function amplitude')axis([0 N*m*T+T/2 0 1.1*F])end

    % Obtaining lower order transfer function approximations%Transfer approximation program%[numa,dena]=tfapprox(num,den,dorder,wmin,wmax)%Generates a transfer function approximation with numerator "numa" and%denominator "dena" of desired order "dorder"%over the frequency range of wmin rad/sec to wmax rad/sec starting with%a transfer function with numerator "num" and denominator "den".%The approximation is achieved by curve fitting the original transfer%function frequency response over the specified frequency range.%file name "tfapprox.m"%num=numerator polynomial of original transfer function

    %den=denominator polynomial of original transfer function%wmin=minimum frequency (rad/sec) for a good approximation%wmax=maximum frequency (rad/sec) for a good approximation%numa=numerator polynomial of the transfer function approximation%dena=denominator polynomial of the transfer function approximation%dorder=desired order of the transfer function approximationfunction [numa,dena]=tfapprox(num,den,dorder,wmin,wmax)warning offnorder=dorder-1;Gorig=tf(num,den);

  • 7/27/2019 Chapter 13 m-files.docx

    5/30

    5

    [mag,phase,w]=bode(Gorig,{wmin,wmax});H=freqs(num,den,w);[numa,dena]=invfreqs(H,w,norder,dorder);Gapprox=tf(numa,dena)wm=2*wmax;bode(Gorig,b,Gapprox,r--,{wmin,wm})

    % function [w,wc]=genfreqs(Wmin,Wmax,n)% This m-file generates n frequency values per decade w% over the frequency range of Wmin to Wmax rad/sec.% Also produced is an expanded set of frequencies wc% for purposes of comparison plotting.function [w,wc]=genfreqs(Wmin,Wmax,n)a=log10(Wmin);if a=Wmax

    breakelseend

    endendfor J=1:nd

    w1=wmin*10^(J-1);for I=1:n

    wc(I+(J-1)*n)=w1+10*w1*(I-1)/(n+1);end

    end

  • 7/27/2019 Chapter 13 m-files.docx

    6/30

    6

    % [NGCoshOverZSinh,GCoshOverZSinh] = CoshOverZSinh(Den,Beta,Mu,d,L );% Generates the normaolize and un-normalized coefficients of Cosh/ZSinh% Nu, absolute viscosity, Ns/m^2% Den, density, kg/m^3% Beta, bulk modulus, N/m^2% Mu, kinematic viscosity, m^2/s% d, line diameter, m% L, line length, m

    function [NGCoshOverZSinh,GCoshOverZSinh] = CoshOverZSinh( Den,Beta,Mu,d,L )r=d/2;Nu=Mu/Den;Dn=Nu*L*sqrt(Den/Beta)/r^2;if Dn>0.5

    Dn'Dn is greater than 0.5 which is outside the range for the computed coefficients'

    else if Dn

  • 7/27/2019 Chapter 13 m-files.docx

    7/30

    7

    % [NGZCoshOverSinh,GZCoshOverSinh] = ZCoshOverSinh(Den,Beta,Mu,d,L );

    % Generates the normaolize and un-normalized coefficients of ZCosh/Sinh% Nu, absolute viscosity, Ns/m^2% Den, density, kg/m^3% Beta, bulk modulus, N/m^2

    % Mu, kinematic viscosity, m^2/s% d, line diameter, m% L, line length, m

    function [NGZCoshOverSinh,GZCoshOverSinh] = ZCoshOverSinh(Den,Beta,Mu,d,L )

    format shortgr=d/2Nu=Mu/Den;

    Dn=Nu*L*sqrt(Den/Beta)/r^2RL=128*Mu*L/(pi*d^4)

    Wv=Nu/r^2a4=0.0891*Dn^4.2889

    a3=0.4371*Dn^3.8444a2=1.2967*Dn^2.1956

    a1=2.5051*Dn^1.7908b5=3e-18*Dn^4.0929b4=0.0035*Dn^4.038

    b3=0.1064*Dn^3.8022b2=0.2497*Dn^2.1196

    b1=1.2308*Dn^1.7072fprintf('Normalized Transfer Function, 8Dn^2ZCosh/[RLSinh]')

    NGZCoshOverSinh=tf([a4 a3 a2 a1 1],[b5 b4 b3 b2 b1 1 0])NGZCoshOverSinhzpk=zpk(NGZCoshOverSinh)

    [y,t]=impulse(NGZCoshOverSinh);

    plot(t,y,'r','LineWidth',2)xlabel('Normalized Time, \omega_v t')

    ylabel('8D_n^2Zcosh\Gamma/(R_Lsinh\Gamma)')title('Normalized Impulse Response')

    [normalized_poles]=roots([b5 b4 b3 b2 b1 1 0])figure

    fprintf('Un-Normalized Transfer Function, ZCosh/Sinh')GZCoshOverSinh=tf([a4/Wv^4 a3/Wv^3 a2/Wv^2 a1/Wv 1]*RL/(8*Dn^2),

    [b5/Wv^5 b4/Wv^5 b3/Wv^4 b2/Wv^3 b1/Wv^2 1/Wv 0])

    GZCoshOverSinhzpk=zpk(GZCoshOverSinh)[Poles]=roots([b5/Wv^5 b4/Wv^5 b3/Wv^4 b2/Wv^3 b1/Wv^2 1/Wv 0])

    [Y,T]=impulse(GZCoshOverSinh);plot(T,Y,'k','Linewidth',2)

    xlabel(' Time, seconds')

    ylabel('Zcosh\Gamma/(sinh\Gamma), (N/m^2)/(m^3/s)')

    title('Impulse Response, \Delta P_a/(\Delta Q_a) or -\Delta P_b/(\Delta Q_b)')

  • 7/27/2019 Chapter 13 m-files.docx

    8/30

    8

    % [NGOneOverZSinh,GOneOverZSinh] = OneOverZSinh(Den,Beta,Mu,d,L );

    % Generates the normaolize and un-normalized coefficients of 1/ZSinh% Nu, absolute viscosity, Ns/m^2% Den, density, kg/m^3% Beta, bulk modulus, N/m^2% Mu, kinematic viscosity, m^2/s% d, line diameter, m% L, line length, mfunction [NGOneOverZSinh,GOneOverZSinh] = OneOverZSinh(Den,Beta,Mu,d,L )

    r=d/2;Nu=Mu/Den;Dn=Nu*L*sqrt(Den/Beta)/r^2;if Dn>0.5

    Dn'Dn is greater than 0.5 which is outside the range for the computed coefficients'

    else if Dn

  • 7/27/2019 Chapter 13 m-files.docx

    9/30

    9

    % [NGZoverSinh,GZoverSinh] = ZoverSinh(Den,Beta,Mu,d,L );% Generates the normaolize and un-normalized coefficients of Z/sinh(Gamma)% Nu, absolute viscosity, Ns/m^2% Den, density, kg/m^3% Beta, bulk modulus, N/m^2% Mu, kinematic viscosity, m^2/s% d, line diameter, m% L, line length, m

    function [NGZoverSinh,GZoverSinh] = ZoverSinh(Den,Beta,Mu,d,L )format shortgr=d/2;Nu=Mu/Den;Dn=Nu*L*sqrt(Den/Beta)/r^2% Dissipation numberRL=128*Mu*L/(pi*d 4)% Steady state flow resistanceWv=Nu/r^2% Normalizing frequency, rad/secif Dn>0.5

    Dn'Dn is greater than 0.5 which is outside the range for the computed coefficients'

    else if Dn

  • 7/27/2019 Chapter 13 m-files.docx

    10/30

    10

    % function[PSD,f]=comppsd(Y,N,H)% This function generates N/2 unique values for the% PSD of Y(t) at a frequency resolution of 1/NH.The% zero frequency value represents the mean value % squared times 2NH. Note, the PSD isplotted for % all values of frequency. After generating the

    % plot, for better resolution, you can change% the scales of the plot using the command% axis([XMIN XMAX YMIN YMAX]).function[PSD,f]=comppsd(Y,N,H)[Y]=fft(Y);NO2=N/2;for m=1:NO2PSD(m)=2*H*abs(Y(m))^2/N;endf=0:1/(N*H):(N-2)/(2*N*H);plot(f,PSD)

    xlabel('Frequency (Hertz)')ylabel('PSD (units of y^2)/(cycles/sec)')return

  • 7/27/2019 Chapter 13 m-files.docx

    11/30

    11

    % function[Y,t]=rinput(N,H)

    % Generates N points in time Y(t) at a time interval of H with zero mean.% N must be a power of 2! The N points will be generated% so as to have a desired one-sided PSD defined in the function% dpsd(f). f is the frequency in Hertz. The frequency resolution% will be 1/NH. The smallest frequency will be 0 Hz and the% largest frequency will be 1/2H Hz. Note, the frequency 1/2H% is referred to as the folding frequency.% If you want this m-file to automatically plot y(t) and the desired PSD,% you need to remove the % signs on the comments at the end of this m-file.function[Y,t]=rinput(N,H)no2=N/2;% Step 1: Generate frequencies and PSD values at each frequencyf=0:1/(N*H):1/(2*H);%Note, the first frequency is zero but we don't%compute GY at f=0 to avoid potential computational problems. GY at f=0%should be zero for a zero mean process.GY(1)=0;for m1=2:no2+1;GY(m1)=dpsd(f(m1));end

    % Step 1: Generate N/2 random phase angles with uniform density% between 0 and 2piTH=random('unif',0,2*pi,no2,1);% Step 2: Generate the appropriate amplitude at each frequency using% the random phase angles.for m=2:no2;

    C=sqrt(GY(m)*N*H/2);CTH=cos(TH(m-1))*C;STH=sin(TH(m-1))*C;Y(m)=CTH+j*STH;k=N+2-m;Y(k)=CTH-j*STH;

    end

    % Step 3: Make sure the N/2+1 value is real so that the negative frequency% values will equal the positive frequency values.no2p1=no2+1;C=sqrt(GY(no2p1)*N*H/2);CTH=cos(TH(no2))*C;Y(no2p1)=CTH+j*0.e0;% Make sure the zero freq. value is zero to achieve a zero mean time series.Y(1)=0.e0+j*0.e0;% Generate Y(t), the inverse trans. of the N Fourier Transform values Y(f).[Y]=ifft(Y)% Rescale to get the correct time series corresponding to this desired PSD.for m=1:N;Y(m)=real(Y(m)/H);

    end% Plot the PSD and time seriest=0:H:(N-1)*H;plot(t,Y)xlabel('Time (sec)')ylabel('Output Variable Corresponding to Desired PSD')figureplot(f,GY)xlabel('Frequency (Hertz)')ylabel('Desired PSD')

  • 7/27/2019 Chapter 13 m-files.docx

    12/30

    12

    % function [GYT,f]=TheorPSD(G,N,H,M)

    % Given a symbolic transfer function G in terms of the% symbolic variable s, the number of data points N (power of 2)% and the time incriment H. M is the magnitude of the white noise

    % PSD corresponding to the input to the transfer function.% To automatically get the plot of the PSD, remove "%" in the% comment statements at the end of this m-file.function [GYT,f]=TheorPSD(G,N,H,M)syms s%G=(2.4*s+9)/(s^2+2.4*s+9);for m=1:N/2

    Z(m)=subs(G,s,j*2*pi*(m-1)/(N*H));GYT(m)=M*abs(Z(m))^2;f=0:1/(N*H):(N-2)/(2*N*H);

    end

    plot(f,GYT)xlabel('Frequency (Hertz)')ylabel('Theroetical PSD')

    % function [GYS]=psdsmooth(N,H,gd,GYT,f,m)% psdsmooth generates a smoothed plot of the PSD, GYS, at% the frequencies f in Hertz, of the output from the% discrete system gd. N is the number of simulation points% which must be a power of 2. H is the time step in seconds.% GYT is the theoretical PSD that hopefully the smoothed PSD

    % will converge to as the number, m, of terms averaged, increases.function [GYS]=psdsmooth(N,H,gd,GYT,f,m)[u]=rinput(N,H);[y]=lsim(gd,u);[GY,f]=comppsd(y,N,H);GYS=GY/m;for n=2:m

    [u]=rinput(N,H);[y]=lsim(gd,u);[GY,f]=comppsd(y,N,H);GYS=GYS+GY/m;

    end

    plot(f,GYS,f,GYT,'r--')xlabel('Frequency (Hertz)')ylabel('Power Spectral Density')legend('Smoothed PSD','Theoretical PSD')

  • 7/27/2019 Chapter 13 m-files.docx

    13/30

    13

    %function [Gxx,Gyy,Gxy,Mag,Phase,CohF,FRF,f]=csdf(N,x,y,H,Ns)%For N data points of x(kH) and y(kH), csdf computes smoothed%estimates of the magnitude (Mag), of the frequency response%function relating output y to input x, the angle (Phase) of the%frequency response function, FRF(f), and the coherence function (CohF)%representing the correlation between x and y. The frequency f

    %is in cycles/sec.%Note, the data points should be zero mean to avoid Delta functions%at zero frequency. Thus, the spectral density functions should be%zero at zero frequency which will cause problems with computing%the zero frequency transfer function data. To avoid these computation%problems, as shown below, the zero frequency transfer function%values are artifically set equal to the values at the first non-%zero frequency value.%The results are smoothed by averaging Ns neighboring values.function [Gxx,Gyy,Gxy,Mag,Phase,CohF,FRF,f]=csdf(N,x,y,H,Ns)N2=N/2;X=fft(x);Y=fft(y);if Ns==1

    f=0:1/(N*H):(N-2)/(2*N*H);for m=2:N2

    Gxy(m)=2*H*conj(X(m))*Y(m)/N; Gxx(m)=2*H*abs(X(m))^2/N;Gyy(m)=2*H*abs(Y(m))^2/N; FRF(m)=Gxy(m)/Gxx(m);Mag(m)=abs(Gxy(m))/Gxx(m);Phase(m)=atan2(imag(Gxy(m)),real(Gxy(m)))*180/pi;CohF(m)=abs(Gxy(m))^2/(Gxx(m)*Gyy(m));

    endGxy(1)=2*H*conj(X(1))*Y(1)/N; Gxx(1)=2*H*abs(X(1))^2/N;Gyy(1)=2*H*abs(Y(1))^2/N; FRF(1)=FRF(2);Mag(1)=Mag(2); Phase(1)=Phase(2); CohF(1)=CohF(2);

    else

    %compute the PSD estimates and smooth Ns neighboring valuesNQ=fix((N2-1)/Ns)*Ns+1;NR=N2-NQ;m=1;for I=2:Ns:NQ

    m=m+1; IQ1=I+Ns-1; gxy=complex(0,0); gxx=0; gyy=0;for K=I:IQ1

    gxy=2*H*conj(X(K))*Y(K)/(N*Ns)+gxy; gxx=2*H*abs(X(K))^2/(Ns*N)+gxx;gyy=2*H*abs(Y(K))^2/(Ns*N)+gyy;

    endGxx(m)=gxx; Gyy(m)=gyy; Gxy(m)=gxy;f(m)=(I+fix(Ns/2)-1)/(N*H);%compute the frequencies

    FRF(m)=gxy/gxx;%compute the frequency response functionMag(m)=abs(gxy)/gxx;%compute magnitude of freq. response functionPhase(m)=atan2(imag(gxy),real(gxy))*180/pi;%phase of freq. resp. fun.CohF(m)=abs(gxy)^2/(gxx*gyy);%compute coherance function

    end%Compute the zero frequency values.Gxy(1)=2*H*conj(X(1))*Y(1)/N;Gxx(1)=2*H*abs(X(1))^2/N;Gyy(1)=2*H*abs(Y(1))^2/N;FRF(1)=FRF(2);Mag(1)=Mag(2);Phase(1)=Phase(2);CohF(1)=CohF(2);end

  • 7/27/2019 Chapter 13 m-files.docx

    14/30

    14

    function [Num,Den]=frfcfit(frf,f,fmax,n)% function [num,den]=frfcfit(frf,f,fmax,n)% This program takes frequency response function complex values frf% at frequencies f (cycles/unit time) and curve fits this input data% out to maximum frequency fmax to get a transfer function of order n.

    % The output is the numerator and denominator polynomials Num and Den.warning offnn=n-1;Lf=length(f);for k=1:Lf-1

    if f(k+1)

  • 7/27/2019 Chapter 13 m-files.docx

    15/30

    15

    function r = random(name,a,b,c,d,e)%RANDOM Generates random numbers from a named distribution.% The appropriate syntax depends on the number of parameters in% the distribution you are using:%% R = RANDOM(NAME,A,M,N) returns an M-by-N array of random% numbers from the named distribution with parameter A.% R = RANDOM(NAME,A,B,M,N) returns an M-by-N array of random

    % numbers from the named distribution with parameters A, and B.% R = RANDOM(NAME,A,B,C,M,N) returns an M-by-N array of random% numbers from the named distribution with parameters A, B, and C.%% The name can be: 'beta' or 'Beta', 'bino' or 'Binomial',% 'chi2' or 'Chisquare', 'exp' or 'Exponential', 'f' or 'F',% 'gam' or 'Gamma', 'geo' or 'Geometric',% 'hyge' or 'Hypergeometric', 'logn' or 'Lognormal',% 'nbin' or 'Negative Binomial', 'ncf' or 'Noncentral F',% 'nct' or 'Noncentral t', 'ncx2' or 'Noncentral Chi-square',% 'norm' or 'Normal', 'poiss' or 'Poisson', 'rayl' or 'Rayleigh',% 't' or 'T', 'unif' or 'Uniform', 'unid' or 'Discrete Uniform',% 'weib' or 'Weibull'.%% Both M and N are integers. Alternatively you can omit N and% specify M as a vector of two integers. If you omit both M and% N, then R is a 1-by-1 array.

    % RANDOM calls many specialized routines that do the calculations.

    % D. Zwillinger 5-91, B. Jones 10-92% Copyright 1993-2000 The MathWorks, Inc.% $Revision: 2.11 $ $Date: 2000/05/26 18:53:31 $

    if ~isstr(name),error('The first argument must be the name of a probability distribution.');

    end

    % define the default parameter values (i.e., array dimensions)

    % Determine, and call, the appropriate subroutine

    if strcmp(deblank(name),'beta') | strcmp(deblank(name),'Beta')if nargin == 3r = betarnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = betarnd(a,b,c);

    elseif nargin == 5r = betarnd(a,b,c,d);

    elseerror('BETARND called with incorrect number of inputs.');

    end

    elseif strcmp(deblank(name),'bino') | strcmp(deblank(name),'Binomial')if nargin == 3r = binornd(a,b);

    elseif nargin == 4 & (length(c)>1)r = binornd(a,b,c);

    elseif nargin == 5r = binornd(a,b,c,d);

    elseerror('BINORND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'chi2') | strcmp(deblank(name),'Chisquare')

    if nargin == 2r = chi2rnd(a);

    elseif nargin == 3 & (length(b)>1)r = chi2rnd(a,b);

    elseif nargin == 4

  • 7/27/2019 Chapter 13 m-files.docx

    16/30

    16

    r = chi2rnd(a,b,c);else

    error('CHI2RND called with incorrect number of inputs.');end

    elseif strcmp(deblank(name),'exp') | strcmp(deblank(name),'Exponential')if nargin == 2r = exprnd(a);

    elseif nargin == 3 & (length(b)>1)r = exprnd(a,b);

    elseif nargin == 4r = exprnd(a,b,c);

    elseerror('EXPRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'f') | strcmp(deblank(name),'F')

    if nargin == 3r = frnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = frnd(a,b,c);

    elseif nargin == 5r = frnd(a,b,c,d);

    elseerror('FRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'gam') | strcmp(deblank(name),'Gamma')

    if nargin == 3r = gamrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = gamrnd(a,b,c);

    elseif nargin == 5r = gamrnd(a,b,c,d);

    elseerror('GAMRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'geo') | strcmp(deblank(name),'Geometric')

    if nargin == 2r = geornd(a);

    elseif nargin == 3 & (length(b)>1)

    r = geornd(a,b);elseif nargin == 4

    r = geornd(a,b,c);else

    error('GEORND called with incorrect number of inputs.');end

    elseif strcmp(deblank(name),'hyge') | strcmp(deblank(name),'Hypergeometric')if nargin == 4r = hygernd(a,b,c);

    elseif nargin == 5 & (length(d)>1)r = hygernd(a,b,c,d);

    elseif nargin == 6r = hygernd(a,b,c,d,e);

    elseerror('HYGERND called with incorrect number of inputs.');

    end

    elseif strcmp(deblank(name),'logn') | strcmp(deblank(name),'Lognormal'),if nargin == 3r = lognrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = lognrnd(a,b,c);

    elseif nargin == 5r = lognrnd(a,b,c,d);

    elseerror('LOGNRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'nbin') | strcmp(deblank(name),'Negative Binomial'),

  • 7/27/2019 Chapter 13 m-files.docx

    17/30

    17

    if nargin == 3r = nbinrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = nbinrnd(a,b,c);

    elseif nargin == 5r = nbinrnd(a,b,c,d);

    elseerror('NBINRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'ncf') | strcmp(deblank(name),'Noncentral F'),

    if nargin == 4r = ncfrnd(a,b,c);

    elseif nargin == 5 & (length(d)>1)r = ncfrnd(a,b,c,d);

    elseif nargin == 6r = ncfrnd(a,b,c,d,e);

    elseerror('NCFRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'nct') | strcmp(deblank(name),'Noncentral T'),

    if nargin == 3r = nctrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = nctrnd(a,b,c);

    elseif nargin == 5r = nctrnd(a,b,c,d);

    elseerror('NCTRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'ncx2')| strcmp(deblank(name),'Noncentral Chi-square'),

    if nargin == 3r = ncx2rnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = ncx2rnd(a,b,c);

    elseif nargin == 5r = ncx2rnd(a,b,c,d);

    elseerror('NCX2RND called with incorrect number of inputs.');

    end

    elseif strcmp(deblank(name),'norm') | strcmp(deblank(name),'Normal'),if nargin == 3r = normrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = normrnd(a,b,c);

    elseif nargin == 5r = normrnd(a,b,c,d);

    elseerror('NORMRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'poiss') | strcmp(deblank(name),'Poisson'),

    if nargin == 2r = poissrnd(a);

    elseif nargin == 3 & (length(b)>1)r = poissrnd(a,b);

    elseif nargin == 4

    r = poissrnd(a,b,c);else

    error('POISSRND called with incorrect number of inputs.');end

    elseif strcmp(deblank(name),'rayl') | strcmp(deblank(name),'Rayleigh'),if nargin == 2r = raylrnd(a);

    elseif nargin == 3 & (length(b)>1)r = raylrnd(a,b);

    elseif nargin == 4r = raylrnd(a,b,c);

  • 7/27/2019 Chapter 13 m-files.docx

    18/30

    18

    elseerror('RAYLRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'t') | strcmp(deblank(name),'T')

    if nargin == 2r = trnd(a);

    elseif nargin == 3 & (length(b)>1)r = trnd(a,b);

    elseif nargin == 4r = trnd(a,b,c);

    elseerror('TRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'unid') | strcmp(deblank(name),'Discrete Uniform')

    if nargin == 2r = unidrnd(a);

    elseif nargin == 3 & (length(b)>1)r = unidrnd(a,b);

    elseif nargin == 4r = unidrnd(a,b,c);

    elseerror('UNIDRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'unif') | strcmp(deblank(name),'Uniform'),

    if nargin == 3r = unifrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = unifrnd(a,b,c);

    elseif nargin == 5r = unifrnd(a,b,c,d);

    elseerror('UNIFRND called with incorrect number of inputs.');

    endelseif strcmp(deblank(name),'weib') | strcmp(deblank(name),'Weibull')

    if nargin == 3r = weibrnd(a,b);

    elseif nargin == 4 & (length(c)>1)r = weibrnd(a,b,c);

    elseif nargin == 5

    r = weibrnd(a,b,c,d);else

    error('WEIBRND called with incorrect number of inputs.');end

    elseerror('Sorry, the Statistics Toolbox does not support this distribution.');

    end

  • 7/27/2019 Chapter 13 m-files.docx

    19/30

    19

    function [errorcode, rows, columns] = rndcheck(nargs,nparms,arg1,arg2,arg3,arg4,arg5)%RNDCHECK error checks the argument list for the random number generators.

    % B.A. Jones 1-22-93% Copyright 1993-2000 The MathWorks, Inc.% $Revision: 2.7 $ $Date: 2000/05/26 18:53:39 $

    sizeinfo = nargs - nparms;errorcode = 0;

    if nparms == 3[r1 c1] = size(arg1);[r2 c2] = size(arg2);[r3 c3] = size(arg3);

    end

    if nparms == 2[r1 c1] = size(arg1);[r2 c2] = size(arg2);

    end

    if sizeinfo == 0if nparms == 1

    [rows columns] = size(arg1);end

    if nparms == 2scalararg1 = (prod(size(arg1)) == 1);scalararg2 = (prod(size(arg2)) == 1);if ~scalararg1 & ~scalararg2

    if r1 ~= r2 | c1 ~= c2errorcode = 1;return;

    endendif ~scalararg1

    [rows columns] = size(arg1);elseif ~scalararg2

    [rows columns] = size(arg2);

    else[rows columns] = size(arg1);

    endend

    if nparms == 3scalararg1 = (prod(size(arg1)) == 1);scalararg2 = (prod(size(arg2)) == 1);scalararg3 = (prod(size(arg3)) == 1);

    if ~scalararg1 & ~scalararg2if r1 ~= r2 | c1 ~= c2

    errorcode = 1;return;

    endend

    if ~scalararg1 & ~scalararg3if r1 ~= r3 | c1 ~= c3

    errorcode = 1;return;

    endend

    if ~scalararg3 & ~scalararg2if r3 ~= r2 | c3 ~= c2

    errorcode = 1;

  • 7/27/2019 Chapter 13 m-files.docx

    20/30

    20

    return;end

    endif ~scalararg1

    [rows columns] = size(arg1);elseif ~scalararg2[rows columns] = size(arg2);else

    [rows columns] = size(arg3);end

    endend

    if sizeinfo == 1scalararg1 = (prod(size(arg1)) == 1);if nparms == 1

    if prod(size(arg2)) ~= 2errorcode = 2;return;

    endif ~scalararg1 & arg2 ~= size(arg1)

    errorcode = 3;return;

    endif (arg2(1) < 0 | arg2(2) < 0 | arg2(1) ~= round(arg2(1)) | arg2(2) ~= round(arg2(2))),

    errorcode = 4;return;

    endrows = arg2(1);columns = arg2(2);

    end

    if nparms == 2if prod(size(arg3)) ~= 2

    errorcode = 2;return;

    endscalararg2 = (prod(size(arg2)) == 1);if ~scalararg1 & ~scalararg2

    if r1 ~= r2 | c1 ~= c2errorcode = 1;return;

    endendif (arg3(1) < 0 | arg3(2) < 0 | arg3(1) ~= round(arg3(1)) | arg3(2) ~= round(arg3(2))),

    errorcode = 4;return;

    endif ~scalararg1

    if any(arg3 ~= size(arg1))errorcode = 3;return;

    end[rows columns] = size(arg1);

    elseif ~scalararg2

    if any(arg3 ~= size(arg2))errorcode = 3;return;

    end[rows columns] = size(arg2);

    elserows = arg3(1);columns = arg3(2);

    endend

  • 7/27/2019 Chapter 13 m-files.docx

    21/30

    21

    if nparms == 3if prod(size(arg4)) ~= 2

    errorcode = 2;return;

    endscalararg1 = (prod(size(arg1)) == 1);scalararg2 = (prod(size(arg2)) == 1);scalararg3 = (prod(size(arg3)) == 1);

    if (arg4(1) < 0 | arg4(2) < 0 | arg4(1) ~= round(arg4(1)) | arg4(2) ~= round(arg4(2))),errorcode = 4;return;

    end

    if ~scalararg1 & ~scalararg2if r1 ~= r2 | c1 ~= c2

    errorcode = 1;return;

    endend

    if ~scalararg1 & ~scalararg3if r1 ~= r3 | c1 ~= c3

    errorcode = 1;return;

    endend

    if ~scalararg3 & ~scalararg2if r3 ~= r2 | c3 ~= c2

    errorcode = 1;return;

    endendif ~scalararg1

    if any(arg4 ~= size(arg1))errorcode = 3;return;

    end

    [rows columns] = size(arg1);elseif ~scalararg2

    if any(arg4 ~= size(arg2))errorcode = 3;return;

    end[rows columns] = size(arg2);

    elseif ~scalararg3if any(arg4 ~= size(arg3))

    errorcode = 3;return;

    end[rows columns] = size(arg3);

    elserows = arg4(1);columns = arg4(2);

    endend

    end

    if sizeinfo == 2if nparms == 1

    scalararg1 = (prod(size(arg1)) == 1);if ~scalararg1

    [rows columns] = size(arg1);if rows ~= arg2 | columns ~= arg3

    errorcode = 3;

  • 7/27/2019 Chapter 13 m-files.docx

    22/30

    22

    return;end

    endif (arg2 < 0 | arg3 < 0 | arg2 ~= round(arg2) | arg3 ~= round(arg3)),

    errorcode = 4;return;

    endrows = arg2;

    columns = arg3;end

    if nparms == 2scalararg1 = (prod(size(arg1)) == 1);scalararg2 = (prod(size(arg2)) == 1);if ~scalararg1 & ~scalararg2

    if r1 ~= r2 | c1 ~= c2errorcode = 1;return;

    endendif ~scalararg1

    [rows columns] = size(arg1);if rows ~= arg3 | columns ~= arg4

    errorcode = 3;return;

    endelseif ~scalararg2

    [rows columns] = size(arg2);if rows ~= arg3 | columns ~= arg4

    errorcode = 3;return;

    endelse

    if (arg3 < 0 | arg4 < 0 | arg3 ~= round(arg3) | arg4 ~= round(arg4)),errorcode = 4;return;

    endrows = arg3;columns = arg4;

    endendif nparms == 3

    scalararg1 = (prod(size(arg1)) == 1);scalararg2 = (prod(size(arg2)) == 1);scalararg3 = (prod(size(arg3)) == 1);

    if ~scalararg1 & ~scalararg2if r1 ~= r2 | c1 ~= c2

    errorcode = 1;return;

    endendif ~scalararg1 & ~scalararg3

    if r1 ~= r3 | c1 ~= c3errorcode = 1;

    return;end

    endif ~scalararg3 & ~scalararg2

    if r3 ~= r2 | c3 ~= c2errorcode = 1;return;

    endendif ~scalararg1

    [rows columns] = size(arg1);

  • 7/27/2019 Chapter 13 m-files.docx

    23/30

    23

    if rows ~= arg4 | columns ~= arg5errorcode = 3;return;

    endelseif ~scalararg2

    [rows columns] = size(arg2);if rows ~= arg4 | columns ~= arg5

    errorcode = 3;

    return;end

    elseif ~scalararg3[rows columns] = size(arg3);if rows ~= arg4 | columns ~= arg5

    errorcode = 3;return;

    endelse

    if (arg4 < 0 | arg5 < 0 | arg4 ~= round(arg4) | arg5 ~= round(arg5)),errorcode = 4;return;

    endrows = arg4;columns = arg5;

    endend

    endfunction [y]=unifnums(N,a,b)for I=1:N

    y(I)=unifrnd(a,b);end

    function r = unifrnd(a,b,m,n)%UNIFRND Random matrices from continuous uniform distribution.% R = UNIFRND(A,B) returns a matrix of random numbers chosen% from the continuous uniform distribution on the interval from A to B.% The size of R is the common size of A and B if both are matrices.% If either parameter is a scalar, the size of R is the size of the other% parameter. Alternatively, R = UNIFRND(A,B,M,N) returns an M by N matrix.% Copyright 1993-2000 The MathWorks, Inc.

    % $Revision: 2.8 $ $Date: 2000/05/26 18:53:55 $if nargin < 2,

    error('Requires at least two input arguments.');endif nargin == 2

    [errorcode rows columns] = rndcheck(2,2,a,b);endif nargin == 3

    [errorcode rows columns] = rndcheck(3,2,a,b,m);endif nargin == 4

    [errorcode rows columns] = rndcheck(4,2,a,b,m,n);endif errorcode > 0

    error('Size information is inconsistent.');end% Initialize R to zero.r = zeros(rows,columns);r = a + (b-a) .* rand(rows,columns);% A is the lower limit, so it must be less than B.k = find(a >= b);if any(k)

    tmp = NaN;r(k) = tmp(ones(size(k)));

    end

  • 7/27/2019 Chapter 13 m-files.docx

    24/30

    24

    function [h,ww] = freqs(b,a,w)

    %FREQS Laplace-transform (s-domain) frequency response.% H = FREQS(B,A,W) returns the complex frequency response vector H% of the filter B/A:% nb-1 nb-2% B(s) b(1)s + b(2)s + ... + b(nb)% H(s) = ---- = -------------------------------------% na-1 na-2% A(s) a(1)s + a(2)s + ... + a(na)%% given the numerator and denominator coefficients in vectors B and A.% The frequency response is evaluated at the points specified in% vector W (in rad/s). The magnitude and phase can be graphed by% calling FREQS(B,A,W) with no output arguments.%% [H,W] = FREQS(B,A) automatically picks a set of 200 frequencies W on% which the frequency response is computed. FREQS(B,A,N) picks N% frequencies.%% See also LOGSPACE, POLYVAL, INVFREQS, and FREQZ.

    % Author(s): J.N. Little, 6-26-86% T. Krauss, 3-19-93, default plots and frequency vector% Copyright 1988-2000 The MathWorks, Inc.% $Revision: 1.9 $ $Date: 2000/06/09 22:04:39 $

    error(nargchk(2,3,nargin));error(nargoutchk(0,2,nargout));

    if ~any(size(b)

  • 7/27/2019 Chapter 13 m-files.docx

    25/30

    25

    ax = gca;subplot(212), semilogx(w,phase),set(gca,'xgrid','on','ygrid','on')set(gca,'xlim',[w(1) w(length(w))])xlabel('Frequency (rad/s)')ylabel('Phase (degrees)')axes(ax)

    elseif nargout == 1,h = hh;

    elseif nargout == 2,h = hh;ww = w;

    end% end freqs

    function w=freqint(a,b,c,d,npts)

    %FREQINT Auto-ranging algorithm for Bode frequency response% W=FREQINT(A,B,C,D,Npts)% W=FREQINT(NUM,DEN,Npts)

    % Andy Grace 7-6-90

    % Was Revision: 1.9, Date: 1996/07/25 16:43:37

    % Generate more points where graph is changing rapidly.% Calculate points based on eigenvalues and transmission zeros.

    [na,ma] = size(a);

    if (nargin==3)&(na==1), % Transfer function form.npts=c;ep=roots(b);tz=roots(a);

    else % State space formif nargin==3, npts=c; [a,b,c,d] = tf2ss(a,b); end

    ep=[eig(a)];tz=tzero(a,b,c,d);

    end

    if isempty(ep), ep=-1000; end

    % Note: this algorithm does not handle zeros greater than 1e5ez=[ep(find(imag(ep)>=0));tz(find(abs(tz)=0))];

    % Round first and last frequencies to nearest decadeinteg = abs(ez)abs(real(ez))));

    % Oscillatory poles and zerosif ~isempty(ez)

    f=w;npts2=2+8/ceil((diffzp+eps)/10);

  • 7/27/2019 Chapter 13 m-files.docx

    26/30

    26

    [dum,ind]=sort(-abs(real(ez)));z=[];for i=ind'

    r1=max([0.8*imag(ez(i))-3*abs(real(ez(i))),10^lowfreq]);r2=1.2*imag(ez(i))+4*abs(real(ez(i)));z=z(find(z>r2|zr2|f

  • 7/27/2019 Chapter 13 m-files.docx

    27/30

    27

    % OR%function [b,a]=invfreqs(g,w,'complex',nb,na,wf,maxiter,tol,pf)error(nargchk(4,9,nargin))if isstr(varargin{1})

    realStr = lower(varargin{1});varargin(1) = [];

    elserealStr = 'real';

    endgaussFlag = length(varargin)>3; % run Gauss-Newton algorithm or not?if length(varargin)rg g=g.';end[rwf,cwf]=size(wf);if cwf>rwf wf=wf';end

    nm=max(na+1,nb+nk);

  • 7/27/2019 Chapter 13 m-files.docx

    28/30

    28

    indb=nb:-1:1; indg=na+1:-1:1; inda=na:-1:1;

    OM=ones(1,length(w));for kom=1:nm-1

    OM=[OM;(i*w).^kom];end

    %% Estimation in the least squares case:%

    Dva=(OM(inda,:).').*(g*ones(1,na));Dvb=-(OM(indb,:).');D=[Dva Dvb].*(wf*ones(1,na+nb));R=D'*D;Vd=D'*((-g.*OM(na+1,:).').*wf);if realFlag

    R=real(R);Vd=real(Vd);

    endth=R\Vd;

    a=[1 th(1:na).'];b=[zeros(1,nk) th(na+1:na+nb).'];

    if ~gaussFlag,return,end

    % Now for the iterative minimization

    if isempty(maxiter), maxiter = 30; endif isempty(tol)

    tol=0.01;end% Stabilizing the denominator:a=apolystab(a,realFlag);

    % The initial estimate:

    GC=((b*OM(indb,:))./(a*OM(indg,:))).';e=(GC-g).*wf;Vcap=e'*e; t=[a(2:na+1) b(nk+1:nk+nb)].';if (verb),

    clc, disp([' INITIAL ESTIMATE'])disp(['Current fit: ' num2str(Vcap)])disp(['par-vector'])disp(t)

    end

    %

    % ** the minimization loop **%gndir=2*tol+1;l=0;st=0;while [norm(gndir)>tol l

  • 7/27/2019 Chapter 13 m-files.docx

    29/30

    29

    % * compute Gauss-Newton search directione=(GC-g).*wf;R=D3'*D3;Vd=D3'*e;if realFlag

    R=real(R);Vd=real(Vd);

    endgndir=R\Vd;% * search along the gndir-direction *ll=0;,k=1;V1=Vcap+1;while [V1 > Vcap ll

  • 7/27/2019 Chapter 13 m-files.docx

    30/30

    % HydBrkSys.m

    % Simulation example in ASME paper of a hydraulic% brake line and brake cylinder with input pressure% step input%Nu=7.6179e-6;Den=855.24;Beta=1.8246e9;Mu=Den*Nu;L=20;d=0.003175;RL=128*Mu*L/(pi*d^4);Vc=L*pi*d^2/4;[NGOneOverZSinh,GOneOverZSinh] = OneOverZSinh(Den,Beta,Mu,d,L );[NGCoshOverZSinh,GCoshOverZSinh] = CoshOverZSinh(Den,Beta,Mu,d,L );G1=feedback(tf([Beta/Vc],[1 0]),GCoshOverZSinh);G=series(GOneOverZSinh,G1);G=zpk(G);G=minreal(G)damp(G)[P,t]=step(G);figureplot(t,P,'r','LineWidth',2)xlabel('Time, sec.')ylabel('Brake Cylinder Pressure \DeltaP_b per unit \DeltaP_a')

    % HydFract.m

    % Simulation example in ASME paper of hydraulic fracturing% The input to the line is a series of flow pulses% Of interest is the corresponding flow rate at the output% end of the line; the output flow amplitude will be% very dependent on the input pulse frequencyNu=1.5e-6;Den=1000;Beta=350000*6895;Mu=Den*Nu;L=5000;d=0.1;RL=128*Mu*L/(pi*d^4);[NGZOverSinh,GZOverSinh] = ZoverSinh(Den,Beta,Mu,d,L );[NGZCoshOverSinh,GZCoshOverSinh] = ZCoshOverSinh(Den,Beta,Mu,d,L );G1=feedback(tf([1/(6*RL)],[1]),GZCoshOverSinh);G=series(GZOverSinh,G1);

    G=zpk(G);G=minreal(G)damp(G)figure[Mag,Ph,w]=bode(G,{0.2 3});plot(w(:),Mag(:),'r','LineWidth',2)xlabel('frequency, rad/sec')ylabel('magnitude of transfer function')T=1;F=1;m=12;N=10;NumPPP=100;[f,t] = PulseSeries(T,m,F,N,NumPPP);[Y,t]=lsim(G,f,t);figureplot(t,f,'r',t,Y,'k:','LineWidth',2)

    ylabel('pipe input and output flow rates per unit input of m^3/s')xlabel('time, sec.')legend('\DeltaQ_a','\DeltaQ_b','Location','SouthWest')