Appendix B. MATLAB Mfile
The following MATLAB mfile pertains to the waterhammer example in
Section 2.1. The program will work for other transfer functions by changing the definition of ‘H’ in the internal function ‘TransferFunctionTF’ at the end of the code. Note, for other transfer functions, it may be necessary to make changes in lines 6-8 of the code.
function TransferFunctionApproximation
warning off
format shortg
% Required inputs; you will have to guess the order, wmax, and wmin at first until
% you see the frequency response and decipher the necessary frequency range
dorder=19;wmin=1e-11;wmax=80000;wminplot=500;wmaxplot=1.25*wmax;
den=999.11;a=1319;r=0.01105;L=37.23;kvis=1.1839e-6;
Rn=5600; % you can specify Rn and then solve for Qe
Qe=kvis*pi*r*Rn/2 % you can specify Qe and then solve for Rn
absvis=den*kvis;
Dn=kvis*L/(a*r^2)
if Rn>=1187.6
n=1; %Desired number of segments in turbulence model.
%May need to be increased for larger % values of Dn
Pe=0.2414*(den^0.75)*(absvis^0.25)*L*(Qe^1.75)/(2*r)^4.75
else
n=1;
Pe=8*absvis*L/(pi*r^4)
end
norder=dorder-1;
nd=ceil(log10(wmax)-log10(wmin)); %Determines the number of decades in the frequency
% range and rounds up to the next integer
w=logspace(log10(wmin),log10(wmax),1000*nd); %Generates at least 1000 points per decade
Lw=length(w);
for k=1:Lw
s(k)=w(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
wt=ones(1,Lw); %weighting terms for curve fit
[numa,dena]=invfreqs(H,w,norder,dorder,wt,100); %curve fitting with 100 attempts if necessary
Gapprox=tf(numa,dena); %linear transfer function approximation from curve fitting frequency response
damp(Gapprox) %gives eigenvalues of the transfer function
% comparing the dcgain of the approximation and the original transfer functions
DCGain=dcgain(Gapprox)
% helps to determine if wmin was low enough
%
% Generate Freq. Resp. plots to determine the accuracy of curve fit.
if wminplot>=wmaxplot, error(‘wmin is too small; very low non-resonant peaks are messing up the calcultions. Increase wmin and try again.’),end;
ndp=ceil(log10(wmaxplot)-log10(wminplot)); %Determines the number of decades in the plotting
%frequency range and rounds up to the next integer
wp=logspace(log10(wminplot),log10(wmaxplot),500*ndp); %Generates at least 500 points per %decade for the plots
Lwp=length(wp);
Hc=freqs(numa,dena,wp); %generates frequency response of the computed transfer function
MHc=20*log10(abs(Hc)); %magnitudes of the frequency response in dB
AHc=angle(Hc)*180/pi; %angles of the frequency response in degrees
clear H s
% recompute the original function with same frequencies used for the transfer function approximation
for k=1:Lwp
s(k)=wp(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
MH=20*log10(abs(H));
AH=angle(H)*180/pi;
figure(1)
semilogx(wp,AH,’k’,wp,AHc,’r:‘,’LineWidth’,2)
title(‘Phase Angle Comparison Plots. Rn=5600’)
xlabel(‘Normalized frequency \omega/\omega_v ‘)
ylabel(‘Phase Angle, degrees’)
legend(‘Original function’,’Approximation’,’Location’,’Best’)
grid on
grid minor
figure(2)
semilogx(wp,MH,’k’,wp,MHc,’r:‘,’LineWidth’,2)
xlabel(‘Normalized frequency \omega/\omega_v ‘)
ylabel(‘Normalized transfer function magnitude, ( \DeltaP_b/P_e)/ ( \DeltaQ_b/Q_e) dB’)
%ylabel(‘Normalized transfer function magnitude, ( \DeltaQ_b/Q_e)/ ( \DeltaQ_a/Q_e) dB’)
legend(‘Original function’,’Approximation’,’Location’,’Best’)
title(‘Magnitude Comparison Plots, Rn=5600’)
grid on
grid minor
Cv=1; % 0 < Cv < 1 with 1 corresponding to 100% valve closure
Vct=0.008; % closure time in seconds if 100% closure
VCt=Vct*Cv; % Valve partially close time in seconds
% the negative sign is associated with the valve closing which is a negative change in flow
[ynorm,tnorm]=step(-Cv*Gapprox,0.014);
% to shorten the normalized simulation time to FT use step(Gapprox,FT)
t=tnorm*r^2/kvis; % unnormalized time s
for nt=1:length(t) % generate shaped valve closing as a function of time in sec.
if t(nt)<=VCt
u(nt)=.5+0.5*cos(pi*(t(nt)/VCt-1));end
if t(nt)>VCt
u(nt)=1;end
end
u=Cv*u; % 0 < Cv <=1 for partial valve closing
Pnorm=lsim(Gapprox,-u,tnorm);
figure(3)
plot(tnorm*r^2/kvis,Pnorm,’r’,’LineWidth’,2) % plot using normalized pressure and un-normalized time
grid
xlabel(‘time, sec.’)
ylabel(‘Normalized Pressure at Valve, \DeltaPv/Pe’)
title(‘Rn=5,600, L=37.23 m, Dn=2.737e-4, Pe=2,770 N/m^2’)
hold
den=999.11;a=1319;r=0.01105;L=37.23;Vo=0.30;
%__________________________________________________________________________________
n=2;
norder=dorder-1;
nd=ceil(log10(wmax)-log10(wmin)); %Determines the number of decades in the frequency % range and rounds up to the next integer
w=logspace(log10(wmin),log10(wmax),1000*nd); %Generates at least 1000 points per decade
Lw=length(w);
for k=1:Lw
s(k)=w(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
wt=ones(1,Lw); % weighting terms for curve fit
[numa,dena]=invfreqs(H,w,norder,dorder,wt,100); % curve fitting with 100 attempts if necessary
Gapprox=tf(numa,dena); % linear transfer function approximation from curve fitting the
% frequency response
damp(Gapprox) %gives eigenvalues of the transfer function
DCGain=dcgain(Gapprox) % comparing the dcgain of the approximation and the original %transfer functions
% helps to determine if wmin was low enough
Pnorm=lsim(Gapprox,-u,tnorm);
figure(3)
plot(tnorm*r^2/kvis,Pnorm,’k:‘,’LineWidth’,2) % plot using normalized pressure and
%un-normalized time
%__________________________________________________________________________________
n=3;
norder=dorder-1;
nd=ceil(log10(wmax)-log10(wmin)); %Determines the number of decades in the frequency %range and rounds up to the next integer
w=logspace(log10(wmin),log10(wmax),1000*nd); %Generates at least 1000 points per decade
Lw=length(w);
for k=1:Lw
s(k)=w(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
wt=ones(1,Lw); %weighting terms for curve fit
[numa,dena]=invfreqs(H,w,norder,dorder,wt,100); %curve fitting with 100 attempts if necessary
Gapprox=tf(numa,dena); %linear transfer function approximation from curve fitting frequency %response
damp(Gapprox) %gives eigenvalues of the transfer function
DCGain=dcgain(Gapprox) % comparing the dcgain of the approximation and the original %transfer functions; helps to determine if wmin was low enough
Pnorm=lsim(Gapprox,-u,tnorm);
figure(3)
plot(tnorm*r^2/kvis,Pnorm,’r-.’,’LineWidth’,2) % plot using normalized pressure and
%un-normalized time
%__________________________________________________________________________________
n=4;
norder=dorder-1;
nd=ceil(log10(wmax)-log10(wmin)); %Determines the number of decades in the frequency %range and rounds up to the next integer
w=logspace(log10(wmin),log10(wmax),1000*nd); %Generates at least 1000 points per decade
Lw=length(w);
for k=1:Lw
s(k)=w(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
wt=ones(1,Lw); %weighting terms for curve fit
[numa,dena]=invfreqs(H,w,norder,dorder,wt,100); %curve fitting with 100 attempts if necessary
Gapprox=tf(numa,dena); %linear transfer function approximation from curve fitting frequency %response
damp(Gapprox) %gives eigenvalues of the transfer function
DCGain=dcgain(Gapprox) % comparing the dcgain of the approximation and the original %transfer functions; helps to determine if wmin was low enough
ynorm=step(-Cv*Gapprox,tnorm); % to shorten the normalized simulation time to FT
% the negative sign is associated with the valve closing which is a negative change in flow
%step(Gapprox,FT)
Pnorm=lsim(Gapprox,-u,tnorm);
figure(3)
plot(tnorm*r^2/kvis,Pnorm,’k--‘,’LineWidth’,2) % plot using normalized pressure and
%un-normalized time
%__________________________________________________________________________________
n=5;
norder=dorder-1;
nd=ceil(log10(wmax)-log10(wmin)); %Determines the number of decades in the frequency %range and rounds up to the next integer
w=logspace(log10(wmin),log10(wmax),1000*nd); %Generates at least 1000 points per decade
Lw=length(w);
for k=1:Lw
s(k)=w(k)*1i;
[H(k)]=TransferFunctionTF(s(k));
end
wt=ones(1,Lw); %weighting terms for curve fit
[numa,dena]=invfreqs(H,w,norder,dorder,wt,100); %curve fitting with 100 attempts if necessary
Gapprox=tf(numa,dena); %linear transfer function approximation from curve fitting frequency %response
damp(Gapprox) %gives eigenvalues of the transfer function
DCGain=dcgain(Gapprox) % comparing the dcgain of the approximation and the original %transfer functions
% helps to determine if wmin was low enough
%
Pnorm=lsim(Gapprox,-u,tnorm);
figure(3)
plot(tnorm*r^2/kvis,Pnorm,’b’,’LineWidth’,2) % plot using normalized pressure and
%un-normalized time
legend(‘n = 1’,’n = 2’,’n = 3’,’n = 4’,’n = 5’)
hold off
Dn
Pe
function [H]=TransferFunctionTF( s ) % H is the transfer function to be approximated
B=2*besselj(1,j*sqrt(s))/(j*sqrt(s).*besselj(0,j*sqrt(s)));
sqr=sqrt(1-B);
RTOZ=0;g=Dn*s/sqr; %This is the case for laminar flow, Rn<1187.6
if Rn>=1187.6
RTOZ=Dn*sqr*(0.039544*Rn^0.75-8);
g=Dn*s/(n*sqr); % gamma
end
W(1,1)=cosh(g)+RTOZ*sinh(g)/n;
W(1,2)=-(sinh(g)+RTOZ*cosh(g)/n)/(8*Dn*sqr+RTOZ);
W(2,1)=-(8*Dn*sqr+RTOZ)*sinh(g);
W(2,2)=cosh(g);
X=W^n;
% X(i,j) corresponds to aij in the paper
H= X(1,2)/X(2,2); % waterhammer transfer function in Eq. (12)
% H=(X(1,2)*X(2,1)-X(1,1)*X(2,2))/(10*X(2,1)-X(1,1));%flow gain transfer function, Eq. (20)
end
end