zara中国官网

zara中国官网This weblog is created for news in chemical engineering specially about processes like reactive distillation , oil and gas process and also bio research.Also you can find some news about optimization, ANFIS and stuff like that.

Wednesday, September 19, 2018

Orifice Bore size:


function FindOrificeBoreSizewithsecant
clc
clear all

% global D density v DeltaP l1 l2 Q kk P L1 L2




D = input ('D[=]in=')
if isempty(D)
    D=6
end
D=D*2.54/100; % m

disp('Please type the fluid type gas or liquid ')
disp('If you dont identify the fluid type the defult fluid is liquid ')


fluid= input ('fluid(gas or liquid)=')
if isempty(fluid)
    disp('Defult fluid is liquid ')
    fluid= 'liquid'

else
    Z= input ('Z factor=')

end

T= input ('Temperature[=]K =')
if isempty(T)
    T=292   % [=] K
end


P= input(' input pressure[=]atm=')
if isempty(P)

    switch fluid
        case 'gas'

            P=24   % [=] bar
        case 'liquid'

            P=5   % [=] bar

    end% [=] bar

end
P=P*101325;  % [=] pa

DeltaP= input(' alowable DeltaP[=]mH2O=')
if isempty(DeltaP)
    DeltaP=2.5  %[=] m H2O

end
DeltaP=DeltaP*101325/10;   % [=] pa


Qv = input ('Q[=]m3/hr=')
if isempty(Qv)


    switch fluid
        case 'gas'
            Qmax=pi*D^2/4*15; % velocity = 15[=] m3/s
            Qmin=pi*D^2/4*10; % Velocity = 10 [=] m3/s
            Qv=(Qmax+Qmin)/2*3600; %[=] m3/hr
            disp('fluid flow [=] m3/hr')
            Qperhour=Qv    % [=] m3/hr
            %%%%%%%%%%%%%%%% for gas %%%%%%%%%%
            disp('fluid flow [=] MMSCFD')
            Qst=288*P*Qv/(Z*101325*T)*24*35.3146667*1e-6 % MMSCFD
            Qmaxst=288*P*Qmax/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
            Qminst=288*P*Qmin/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
        case 'liquid'

            Qmax=pi*D^2/4*3; % velocity = 3 [=] m3/s
            Qmin=pi*D^2/4*0.5; % Velocity = 1 [=] m3/s
            Qv=(Qmax+Qmin)/2*3600; %[=] m3/hr
            disp('fluid flow [=] m3/hr')
            Qperhour=Qv

    end

else




    switch fluid
        case 'gas'
            Qmax=pi*D^2/4*15; % velocity = 3 [=] m3/s
            Qmin=pi*D^2/4*10; % Velocity = 1 [=] m3/s

            disp('fluid flow [=] m3/hr')
            Qperhour=Qv    % [=] m3/hr
            disp('fluid flow [=] MMSCFD')
            Qst=288*P*Qv/(Z*101325*T)*24*35.3146667*1e-6 % MMSCFD
            Qmaxst=288*P*Qmax/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
            Qminst=288*P*Qmin/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
        case 'liquid'

            Qmax=pi*D^2/4*3; % velocity = 3 [=] m3/s
            Qmin=pi*D^2/4*0.5; % Velocity = 1 [=] m3/s

            disp('fluid flow [=] m3/hr')
            Qperhour=Qv

    end
end


Q=Qv/3600;   % [=] m3/s


density= input ('density[=]kg/m3=')
if isempty(density)

    switch fluid
        case 'gas'

            density=18.7   % [=] kg/m3
        case 'liquid'

            density=840   % [=] kg/m3

    end
end

viscosity= input ('viscosity[=]cpoise=')

if isempty(viscosity)

    switch fluid
        case 'gas'

            viscosity=1.172e-2 % [=] cpoise
        case 'liquid'

            viscosity=0.63  % [=] cpoise

    end

end
viscosity=viscosity/100*0.1  % [=]kg/m.s


dynamic_viscosity=viscosity/density;
v=dynamic_viscosity   % [=] m2/s





% d0= input( 'guss orifice bore [=] in =')
% if isempty(d0)
%     dmin=0.1*D % [=] m
%     dmax=0.75*D % [=] m
%     d0=(dmin+dmax)/2 % [=] m
% end
% d0=d0*2.54/100; % m


kk= input( 'Cp/Cv=')

if isempty(kk)

    switch fluid
        case 'gas'

            kk=1.36
        case 'liquid'

            kk=1.007

    end
end


disp('please identify tapping type , corner tapping (1), flange tapping (2), D and D/2 tapping (3)')

tapping= input ('tapping=')

if isempty(tapping)
    tapping= 1;
end
switch tapping
    case 1
        L1=0
        L2=0
    case 2

        L1=0.0254/D
        L2=0.0254/D

    case 3

        L1=1
        L2=0.47

end



 dmin=0.1*D % [=] m
    dmax=0.75*D % [=] m


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ReD=4*Q/(v*D*pi);

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp ('uncertainity expansibility factor (epsil)')
uncertainityexpanfactor=3.5*DeltaP/(kk*P)

P2=P-DeltaP;

i=1;

QD(i)=Qmin;


while QD(i)<=Qmax
    j=1;
    db(j)=dmin;

    while db(j)<=dmax
        Beta(j)=db(j)/D;
        M2=2*L2/(1-Beta(j));
        Pratio=(P2-101325)/(P-101325);
        if Pratio < 0.75
            epsil(j)=1;
        else

            epsil(j)=1-(0.351 + 0.256 *Beta(j)^4 +0.93*Beta(j)^8)*(1-(P2/P)^(1/kk));
        end

        %         epsil(j)=1;
        Re(i)=4*QD(i)/(v*D*pi);
        A=(19000*Beta(j)/Re(i))^0.8;

        if D<0 .0712="" p="">            Cc(j,i)= 0.5961+ 0.0261*Beta(j)^2- 0.216*Beta(j)^8 + 0.000521* (1e6*Beta(j)/Re(i))^0.7 + (0.0188 +0.0063*A)*(1e6/Re(i))^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta(j)^4/(1-Beta(j)^4)-0.031*(M2-0.8*M2^1.1)*Beta(j)^1.3;

            if Beta(j)>=0.1 && Beta(j) <0 .2="" p="">                Cuncertain(j,i)=(0.7-Beta(j))+0.9*(0.75-Beta(j))*(2.8-D/25.4);
            elseif Beta(j)>=0.2 && Beta(j)<=0.6

                if Beta(j) >0.5 && Re(i)<10000 p="">                    Cuncertain(j,i)=0.5+0.9*(0.75-Beta(j))*(2.8-D/25.4)+0.5;
                else
                    Cuncertain(j,i)=0.5+0.9*(0.75-Beta(j))*(2.8-D/25.4);
                end
            elseif Beta(j)>=0.6 && Beta(j)<=0.75

                if Beta(j) >0.5 && Re(i)<10000 p="">                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.9*(0.75-Beta(j))*(2.8-D/25.4)+0.5;
                else
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.9*(0.75-Beta(j))*(2.8-D/25.4);
                end


            elseif Beta(j)>0.75
                Cuncertain(j,i)=1;
            end

        else
            Cc(j,i)=0.5961+ 0.0261*Beta(j)^2- 0.216*Beta(j)^8 + 0.000521* (1e6*Beta(j)/Re(i))^0.7 + (0.0188 +0.0063*A)*(1e6/Re(i))^0.3 + ...
                (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta(j)^4/(1-Beta(j)^4)-0.031*(M2-0.8*M2^1.1)*Beta(j)^1.3 +0.011*(0.75-Beta(j))*(2.8-(D)/0.0254); % D shod be mm

            if Beta(j)>=0.1 && Beta(j) <0 .2="" p="">                Cuncertain(j,i)=(0.7-Beta(j));
            elseif Beta(j)>=0.2 && Beta(j)<=0.6
                if Beta(j) >0.5 && Re(i)<10000 p="">                    Cuncertain(j,i)=0.5+0.5;
                else
                    Cuncertain(j,i)=0.5;
                end

            elseif Beta(j)>=0.6 && Beta(j)<=0.75
                if Beta(j) >0.5 && Re(i)<10000 p="">                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.5;
                else
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5);
                end
            elseif Beta(j)>0.75
                Cuncertain(j,i)=1;
            end

        end
        Pressloss(j,i)=(sqrt(1-Beta(j)^4*(1-(Cc(j,i))^2))-Cc(j,i)*Beta(j)^2)/(sqrt(1-Beta(j)^4*(1-(Cc(j,i))^2))+Cc(j,i)*Beta(j)^2)*DeltaP;
        PresslossCoef(j,i)=(sqrt((1-Beta(j)^4*(1-Cc(j,i)^2))/(Cc(j,i)*Beta(j)^2))-1)^2;

        result(j,i)= QD(i)-epsil(j)*Cc(j)/(sqrt(1-Beta(j)^4))*pi/4*db(j)^2*sqrt(2*DeltaP/density);


        db(j+1)=db(j)+(dmax-dmin)/10;
        j=j+1;


    end

    QD(i+1)=QD(i)+(Qmax-Qmin)/10;
    i=i+1  ;


end
%--------------------------last ------------
dd=db(1:j-1);
QQ=QD(1:i-1);
Result=result(1:j-1,1:i-1);
CC=Cc(1:j-1,1:i-1);
Epsil=epsil(1:j-1);
BB=Beta(1:j-1);
PPressloss=Pressloss(1:j-1,1:i-1);
PPresslossCoef=PresslossCoef(1:j-1,1:i-1);
Ccuncertain=Cuncertain(1:j-1,1:i-1);
%------------------------------------------------------------------

CC;
QQ;
Result;
Re;
dd;
PPressloss;
PresslossCoef;
diameteritre=length(dd);
flowiter=length(QQ);
length(CC);
i;
j;

figure('name','Diameter versus Discharge coefficient')
for ii=1:i-1
    plot(dd*100/2.54,CC(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    b(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on
end
legend(b);
xlabel({'Diameter'})
ylabel({'Discharge coefficient'})


figure('nam', 'Diameter versus (Qideal-Qactual)')
for ii=1:i-1
    plot(dd*100/2.54,Result(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
plot(dd*100/2.54,0,'--rs')
xlabel({'Diameter'})
ylabel({'(Qideal-Qactual)'})

figure('nam', 'Diameter versus Pressure loss')
for ii=1:i-1
    plot(dd*100/2.54,PPressloss(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
% plot(dd*100/2.54,0,'--rs')
% ----
xlabel({'Diameter'})
ylabel({'Pressure loss'})


figure('nam', 'Diameter versus Pressure loss Coeficient')
for ii=1:i-1
    plot(dd*100/2.54,PPresslossCoef(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
xlabel({'Diameter'})
ylabel({'Pressure loss Coeficient'})

figure('nam', 'Diameter versus uncertainity of discharge coefficient')
for ii=1:i-1
    plot(dd*100/2.54,Ccuncertain(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
xlabel({'Diameter'})
ylabel({'uncertainity of discharge coefficient'})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('name', 'Diameter versus Error and uncertainity of discharge coefficient')
Beta
i=1;

error=10;



d(i)=dmin;    % Guss 1
db=d(i);

Beta=db/D;
M2=2*L2/(1-Beta);
Pratio=(P2-101325)/(P-101325);
if Pratio < 0.75
    epsil=1;
else

    epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/P)^(1/kk));
end

%         epsil(j)=1;
RRe=4*Q/(v*D*pi);
A=(19000*Beta/RRe)^0.8;

if D<0 .0712="" p="">    Ccc= 0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3;
    if Beta>=0.1 && Beta <0 .2="" p="">        Cuncertain=(0.7-Beta)+0.9*(0.75-Beta)*(2.8-D/25.4);
    elseif Beta>=0.2 && Beta<=0.6

        if Beta >0.5 && RRe<10000 p="">            Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
        end
    elseif Beta>=0.6 && Beta<=0.75

        if Beta >0.5 && RRe<10000 p="">            Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4);
        end


    elseif Beta>0.75
        disp('Beta is more than 0.75')
    end

else
    Ccc=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
        (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(D)/0.0254); % D shod be mm

    if Beta>=0.1 && Beta <0 .2="" p="">        Cuncertain=(0.7-Beta)
    elseif Beta>=0.2 && Beta<=0.6
        if Beta >0.5 && RRe<10000 p="">            Cuncertain=0.5+0.5;
        else
            Cuncertain=0.5;
        end

    elseif Beta>=0.6 && Beta<=0.75
        if Beta >0.5 && RRe<10000 p="">            Cuncertain=(1.667*Beta-0.5)+0.5;
        else
            Cuncertain=(1.667*Beta-0.5);
        end
    elseif Beta>0.75
        disp('Beta is more than 0.75')
    end

end

F(1)=Q- epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density); % [=] m3/s

subplot(2,1,1)
plot(db*100/2.54,error,'*')
hold on
xlabel({'Diameter'})
ylabel({'Error'})

subplot(2,1,2)
plot(db*100/2.54,Cuncertain,'*')
hold on
xlabel({'Diameter'})
ylabel({'uncertainity of discharge coefficient%'})




d(2)=dmax;   % Guss2
db=d(2);
Beta=db/D;
M2=2*L2/(1-Beta);
if Pratio < 0.75
    epsil=1;
else

    epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/P)^(1/kk));
end


RRe=4*Q/(v*D*pi);
A=(19000*Beta/RRe)^0.8;

if D<0 .0712="" p="">    Ccc= 0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3;
    if Beta>=0.1 && Beta <0 .2="" p="">        Cuncertain=(0.7-Beta)+0.9*(0.75-Beta)*(2.8-D/25.4);
    elseif Beta>=0.2 && Beta<=0.6

        if Beta >0.5 && RRe<10000 p="">            Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
        end
    elseif Beta>=0.6 && Beta<=0.75

        if Beta >0.5 && RRe<10000 p="">            Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4);
        end


    elseif Beta>0.75
        disp('Beta is more than 0.75')
    end
else
    Ccc=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
        (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(D)/0.0254); % D shod be mm

    if Beta>=0.1 && Beta <0 .2="" p="">        Cuncertain=(0.7-Beta)
    elseif Beta>=0.2 && Beta<=0.6
        if Beta >0.5 && RRe<10000 p="">            Cuncertain=0.5+0.5;
        else
            Cuncertain=0.5;
        end

    elseif Beta>=0.6 && Beta<=0.75
        if Beta >0.5 && RRe<10000 p="">            Cuncertain=(1.667*Beta-0.5)+0.5;
        else
            Cuncertain=(1.667*Beta-0.5);
        end
    elseif Beta>0.75
        disp('Beta is more than 0.75')
    end

end

F(2)=Q - epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density); % [=] m3/s

subplot(2,1,1)
plot(db*100/2.54,error,'*')
hold on
xlabel({'Diameter'})
ylabel({'Error'})

subplot(2,1,2)
plot(db*100/2.54,Cuncertain,'*')
hold on
xlabel({'Diameter'})
ylabel({'uncertainity of discharge coefficient%'})

% figure('name', 'Diameter versus Error and uncertainity of discharge coefficient')
i=2;
while error>0.0001

    d(i+1)=d(i)-F(i)*(d(i)-d(i-1))/(F(i)-F(i-1));
    db=d(i+1);
    Beta=db/D
    M2=2*L2/(1-Beta);
    if Pratio < 0.75
        epsil=1;
    else

        epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/P)^(1/kk));
    end

    RRe=4*Q/(v*D*pi);
    A=(19000*Beta/RRe)^0.8;

    if D<0 .0712="" p="">        Ccc= 0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3;

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta)+0.9*(0.75-Beta)*(2.8-D/25.4);
        elseif Beta>=0.2 && Beta<=0.6

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
            end
        elseif Beta>=0.6 && Beta<=0.75

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4);
            end


        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end


    else
        Ccc=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(D)/0.0254); % D shod be mm

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta);
        elseif Beta>=0.2 && Beta<=0.6
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end

        elseif Beta>=0.6 && Beta<=0.75
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5);
            end
        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end

    end

    F(i+1)=Q - epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density); % [=] m3/s

    error= abs(d(i)-d(i-1));

    i=i+1;





    subplot(2,1,1)
    plot(db*100/2.54,error,'*')
    hold on
    xlabel({'Diameter'})
    ylabel({'Error'})

    subplot(2,1,2)
    plot(db*100/2.54,Cuncertain,'*')
    hold on
    xlabel({'Diameter'})
    ylabel({'uncertainity of discharge coefficient%'})
end
RRe
db
CALCULATION_ERROR=error
Pressloss=(sqrt(1-Beta^4*(1-(Ccc)^2))-Ccc*Beta^2)/(sqrt(1-Beta^4*(1-(Ccc)^2))+Ccc*Beta^2)*DeltaP;
PresslossCoef=(sqrt((1-Beta^4*(1-Ccc^2))/(Ccc*Beta^2))-1)^2;
volumeflow=epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density)*3600;
ORIFICEDIAMETER_in=db*100/2.54

BETA_RATIO=Beta




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check uncertainity for calculated beta in different Q

figure('name', 'Diameter versus uncertainity of discharge coefficient for constant calculated Beta in different Q')


M2=2*L2/(1-Beta);
QD=Qmin;


while QD<=Qmax
    if Pratio < 0.75
        epsil=1;
    else

        epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/P)^(1/kk));
    end

    RRe=4*QD/(v*D*pi);
    A=(19000*Beta/RRe)^0.8;

    if D<0 .0712="" p="">        Ccc= 0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3;

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta)+0.9*(0.75-Beta)*(2.8-D/25.4);
        elseif Beta>=0.2 && Beta<=0.6

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
            end
        elseif Beta>=0.6 && Beta<=0.75

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4);
            end


        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end


    else
        Ccc=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(D)/0.0254); % D shod be mm

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta);
        elseif Beta>=0.2 && Beta<=0.6
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end

        elseif Beta>=0.6 && Beta<=0.75
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5);
            end
        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end
        FF=QD - epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density); % [=] m3/s

        QD=QD+(Qmax-Qmin)/10;

        subplot(2,1,1)
        plot(QD*3600,FF,'*')
        hold on
        xlabel({'FLOW'})
        ylabel({'Error'})

        subplot(2,1,2)
        plot(QD*3600,Cuncertain,'*')
        hold on
        xlabel({'FLOW'})
        ylabel({'uncertainity of discharge coefficient%'})
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check uncertainity diffrent beta for constant flow

figure('name', 'Diameter versus uncertainity of discharge coefficient for constant calculated Beta in different Q')



QD=volumeflow;
dd=dmin;

while dd<=dmax
    Beta=dd/D;
    M2=2*L2/(1-Beta);
    if Pratio < 0.75
        epsil=1;
    else

        epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/P)^(1/kk));
    end

    RRe=4*QD/(v*D*pi);
    A=(19000*Beta/RRe)^0.8;

    if D<0 .0712="" p="">        Ccc= 0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3;

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta)+0.9*(0.75-Beta)*(2.8-D/25.4);
        elseif Beta>=0.2 && Beta<=0.6

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
            end
        elseif Beta>=0.6 && Beta<=0.75

            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5)+0.9*(0.75-Beta)*(2.8-D/25.4);
            end


        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end


    else
        Ccc=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(D)/0.0254); % D shod be mm

        if Beta>=0.1 && Beta <0 .2="" p="">            Cuncertain=(0.7-Beta);
        elseif Beta>=0.2 && Beta<=0.6
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end

        elseif Beta>=0.6 && Beta<=0.75
            if Beta >0.5 && RRe<10000 p="">                Cuncertain=(1.667*Beta-0.5)+0.5;
            else
                Cuncertain=(1.667*Beta-0.5);
            end
        elseif Beta>0.75
            disp('Beta is more than 0.75')
        end
        FF=QD - epsil*Ccc/(sqrt(1-Beta^4))*pi/4*db^2*sqrt(2*DeltaP/density); % [=] m3/s

        dd=dd+(dmax-dmin)/10;


        plot(dd*100/2.54,Cuncertain,'marker','^')
        hold on
        xlabel({'diameter'})
        ylabel({'uncertainity of discharge coefficient%'})
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%

function Findorificeflowratewithsecant

clc
clear all

% global D density v DeltaP l1 l2 Q kk P L1 L2




D = input ('D[=]in=')
if isempty(D)
    D=6
end
D=D*2.54/100; % m

disp('Please type the fluid type gas or liquid ')
disp('If you dont identify the fluid type the defult fluid is liquid ')


fluid= input ('fluid(gas or liquid)=')
if isempty(fluid)
    disp('Defult fluid is liquid ')
    fluid= 'liquid'

else
    Z= input ('Z factor=')

end

disp('Beta should be less than 0.75')
BBeta=input( 'orifice bore Ratio  =')

if isempty(BBeta)
    BBeta=0.6
end
d= BBeta*D
dinch=d*100/2.54  %m


T= input ('Temperature[=]K =')
if isempty(T)
    T=292   % [=] K
end


P= input(' input pressure[=]bar=')
if isempty(P)
    
    switch fluid
        case 'gas'
            
            P=24   % [=] bar
        case 'liquid'
            
            P=5   % [=] bar
            
    end% [=] bar
    
end
P=P*101325;  % [=] pa

DeltaP= input(' alowable DeltaP[=]mH2O=')
if isempty(DeltaP)
    DeltaP=2.5  %[=] m H2O
    
end
DeltaP=DeltaP*101325/10;   % [=] pa





switch fluid
    case 'gas'
        Qmax=pi*D^2/4*50; % velocity = 3 [=] m3/s
        Qmin=pi*D^2/4*2; % Velocity = 1 [=] m3/s
        Qv=(Qmax+Qmin)/2*3600; %[=] m3/hr
        disp('fluid flow [=] m3/hr')
        Qperhour=Qv    % [=] m3/hr
        disp('fluid flow [=] MMSCFD')
        Qst=288*P*Qv/(Z*101325*T)*24*35.3146667*1e-6 % MMSCFD
        Qmaxst=288*P*Qmax/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
        Qminst=288*P*Qmin/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
    case 'liquid'
        
        Qmax=pi*D^2/4*5; % velocity = 3 [=] m3/s
        Qmin=pi*D^2/4*0.5; % Velocity = 1 [=] m3/s
        Qv=(Qmax+Qmin)/2*3600; %[=] m3/hr
        disp('fluid flow [=] m3/hr')
        Qperhour=Qv
        
end


Q0=Qv/3600;   % [=] m3/s


density= input ('density[=]kg/m3=')
if isempty(density)
    
    switch fluid
        case 'gas'
            
            density=18.7   % [=] kg/m3
        case 'liquid'
            
            density=840   % [=] kg/m3
            
    end
end

viscosity= input ('viscosity[=]cpoise=')

if isempty(viscosity)
    
    switch fluid
        case 'gas'
            
            viscosity=1.172e-2 % [=] cpoise
        case 'liquid'
            
            viscosity=0.63  % [=] cpoise
            
    end
    
end
viscosity=viscosity/100*0.1  % [=]kg/m.s


dynamic_viscosity=viscosity/density;
v=dynamic_viscosity   % [=] m2/s





kk= input( 'Cp/Cv=')

if isempty(kk)
    
    switch fluid
        case 'gas'
            
            kk=1.36
        case 'liquid'
            
            kk=1.007
            
    end
end


disp('please identify tapping type , corner tapping (1), flange tapping (2), D and D/2 tapping (3)')

tapping= input ('tapping=')

if isempty(tapping)
    tapping= 1;
end
switch tapping
    case 1
        L1=0
        L2=0
    case 2
        
        L1=0.0254/D
        L2=0.0254/D
        
    case 3
        
        L1=1
        L2=0.47
        
end

dmin=0.1*D % [=] m
dmax=0.8*D % [=] m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ReD=4*Q/(v*D*pi);

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%% plot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp ('uncertainity expansibility factor (epsil)')
uncertainityexpanfactor=3.5*DeltaP/(kk*P)

P2=P-DeltaP;

i=1;

QD(i)=Qmin;


while QD(i)<=Qmax
    j=1;
    db(j)=dmin;
    
    while db(j)<=dmax
        Beta(j)=db(j)/D;
        M2=2*L2/(1-Beta(j));
        Pratio=(P2-101325)/(P-101325);
        if Pratio < 0.75
            epsil(j)=1;
        else
            
            epsil(j)=1-(0.351 + 0.256 *Beta(j)^4 +0.93*Beta(j)^8)*(1-(P2/P)^(1/kk));
        end
        
        %         epsil(j)=1;
        Re(i)=4*QD(i)/(v*D*pi);
        A=(19000*Beta(j)/Re(i))^0.8;
        
        if D<0 .0712="" div="">
            Cc(j,i)= 0.5961+ 0.0261*Beta(j)^2- 0.216*Beta(j)^8 + 0.000521* (1e6*Beta(j)/Re(i))^0.7 + (0.0188 +0.0063*A)*(1e6/Re(i))^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta(j)^4/(1-Beta(j)^4)-0.031*(M2-0.8*M2^1.1)*Beta(j)^1.3;
            
            if Beta(j)>=0.1 && Beta(j) <0 .2="" div="">
                Cuncertain(j,i)=(0.7-Beta(j))+0.9*(0.75-Beta(j))*(2.8-D/25.4);
            elseif Beta(j)>=0.2 && Beta(j)<=0.6
                
                if Beta(j) >0.5 && Re(i)<10000 div="">
                    Cuncertain(j,i)=0.5+0.9*(0.75-Beta(j))*(2.8-D/25.4)+0.5;
                else
                    Cuncertain(j,i)=0.5+0.9*(0.75-Beta(j))*(2.8-D/25.4);
                end
            elseif Beta(j)>=0.6 && Beta(j)<=0.75
                
                if Beta(j) >0.5 && Re(i)<10000 div="">
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.9*(0.75-Beta(j))*(2.8-D/25.4)+0.5;
                else
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.9*(0.75-Beta(j))*(2.8-D/25.4);
                end
                
                
            elseif Beta(j)>0.75
                Cuncertain(j,i)=1;
            end
            
        else
            Cc(j,i)=0.5961+ 0.0261*Beta(j)^2- 0.216*Beta(j)^8 + 0.000521* (1e6*Beta(j)/Re(i))^0.7 + (0.0188 +0.0063*A)*(1e6/Re(i))^0.3 + ...
                (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta(j)^4/(1-Beta(j)^4)-0.031*(M2-0.8*M2^1.1)*Beta(j)^1.3 +0.011*(0.75-Beta(j))*(2.8-(D)/0.0254); % D shod be mm
            
            if Beta(j)>=0.1 && Beta(j) <0 .2="" div="">
                Cuncertain(j,i)=(0.7-Beta(j));
            elseif Beta(j)>=0.2 && Beta(j)<=0.6
                if Beta(j) >0.5 && Re(i)<10000 div="">
                    Cuncertain(j,i)=0.5+0.5;
                else
                    Cuncertain(j,i)=0.5;
                end
                
            elseif Beta(j)>=0.6 && Beta(j)<=0.75
                if Beta(j) >0.5 && Re(i)<10000 div="">
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5)+0.5;
                else
                    Cuncertain(j,i)=(1.667*Beta(j)-0.5);
                end
            elseif Beta(j)>0.75
                Cuncertain(j,i)=1;
            end
            
        end
        Pressloss(j,i)=(sqrt(1-Beta(j)^4*(1-(Cc(j,i))^2))-Cc(j,i)*Beta(j)^2)/(sqrt(1-Beta(j)^4*(1-(Cc(j,i))^2))+Cc(j,i)*Beta(j)^2)*DeltaP;
        PresslossCoef(j,i)=(sqrt((1-Beta(j)^4*(1-Cc(j,i)^2))/(Cc(j,i)*Beta(j)^2))-1)^2;
        
        result(j,i)= QD(i)-epsil(j)*Cc(j)/(sqrt(1-Beta(j)^4))*pi/4*db(j)^2*sqrt(2*DeltaP/density);
        
        
        db(j+1)=db(j)+(dmax-dmin)/10;
        j=j+1;
        
        
    end
    
    QD(i+1)=QD(i)+(Qmax-Qmin)/10;
    i=i+1  ;
    
    
end
%--------------------------last ------------
dd=db(1:j-1);
QQ=QD(1:i-1);
Result=result(1:j-1,1:i-1);
CC=Cc(1:j-1,1:i-1);
Epsil=epsil(1:j-1);
BB=Beta(1:j-1);
PPressloss=Pressloss(1:j-1,1:i-1);
PPresslossCoef=PresslossCoef(1:j-1,1:i-1);
Ccuncertain=Cuncertain(1:j-1,1:i-1);
%------------------------------------------------------------------

CC;
QQ;
Result;
Re;
dd;
PPressloss;
PresslossCoef;
diameteritre=length(dd);
flowiter=length(QQ);
length(CC);
i;
j;

figure('name','Diameter versus Discharge coefficient')
for ii=1:i-1
    plot(dd*100/2.54,CC(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    b(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on
end
legend(b);
xlabel({'Diameter'})
ylabel({'Discharge coefficient'})


figure('nam', 'Diameter versus (Qideal-Qactual)')
for ii=1:i-1
    plot(dd*100/2.54,Result(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
plot(dd*100/2.54,0,'--rs')
xlabel({'Diameter'})
ylabel({'(Qideal-Qactual)'})

figure('nam', 'Diameter versus Pressure loss')
for ii=1:i-1
    plot(dd*100/2.54,PPressloss(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
% plot(dd*100/2.54,0,'--rs')
% ----
xlabel({'Diameter'})
ylabel({'Pressure loss'})


figure('nam', 'Diameter versus Pressure loss Coeficient')
for ii=1:i-1
    plot(dd*100/2.54,PPresslossCoef(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
xlabel({'Diameter'})
ylabel({'Pressure loss Coeficient'})

figure('nam', 'Diameter versus uncertainity of discharge coefficient')
for ii=1:i-1
    plot(dd*100/2.54,Ccuncertain(:,ii),'linestyle','-','marker','^','markeredgecolor',[rand(1,3)],'markersize',5)
    a(1,ii)={['qq=',num2str(QQ(ii)*3600)]};
    hold on;
end
legend(a)
xlabel({'Diameter'})
ylabel({'uncertainity of discharge coefficient'})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('name', 'Diameter versus Error and uncertainity of discharge coefficient')
i=1;
d
error=10;

switch fluid
    case 'gas'
        
        Q(1)=pi*D^2/4*5;    % Guss 1
    case 'liquid'
        
        Q(1)=pi*D^2/4*0.5;    % Guss 1
        
end


QB=Q(i);

M2=2*L2/(1-BBeta);
Pratio=(P2-101325)/(P-101325);
if Pratio < 0.75
    epsil=1;
else
    
    epsil=1-(0.351 + 0.256 *BBeta^4 +0.93*BBeta^8)*(1-(P2/P)^(1/kk));
end

%         epsil(j)=1;
RRe=4*QB/(v*D*pi);
A=(19000*BBeta/RRe)^0.8;

if D<0 .0712="" div="">
    Cc= 0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3;
    if BBeta>=0.1 && BBeta <0 .2="" div="">
        Cuncertain=(0.7-BBeta)+0.9*(0.75-BBeta)*(2.8-D/25.4);
    elseif BBeta>=0.2 && BBeta<=0.6
        
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4);
        end
    elseif BBeta>=0.6 && BBeta<=0.75
        
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4);
        end
        
        
    elseif BBeta>0.75
        disp('Beta is more than 0.75')
    end
    
else
    Cc=0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
        (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3 +0.011*(0.75-BBeta)*(2.8-(D)/0.0254); % D shod be mm
    
    if BBeta>=0.1 && BBeta <0 .2="" div="">
        Cuncertain=(0.7-BBeta);
    elseif BBeta>=0.2 && BBeta<=0.6
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=0.5+0.5;
        else
            Cuncertain=0.5;
        end
        
    elseif BBeta>=0.6 && BBeta<=0.75
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=(1.667*BBeta-0.5)+0.5;
        else
            Cuncertain=(1.667*BBeta-0.5);
        end
    elseif BBeta>0.75
        disp('Beta is more than 0.75')
    end
    
end

F(1)=Q(i)- epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density); % [=] m3/s


subplot(2,1,1)
    plot(QB*3600,error,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'Error'})
    
    subplot(2,1,2)
    plot(QB*3600,Cuncertain,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'uncertainity of discharge coefficient%'})
    
    
switch fluid
    case 'gas'
        
        Q(2)=pi*D^2/4*10;    % Guss 1
    case 'liquid'
        
        Q(2)=pi*D^2/4*1;    % Guss 1
        
end

QB=Q(2);

M2=2*L2/(1-BBeta);
if Pratio < 0.75
    epsil=1;
else
    
    epsil=1-(0.351 + 0.256 *BBeta^4 +0.93*BBeta^8)*(1-(P2/P)^(1/kk));
end


RRe=4*QB/(v*D*pi);
A=(19000*BBeta/RRe)^0.8;

if D<0 .0712="" div="">
    Cc= 0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3;
    if BBeta>=0.1 && BBeta <0 .2="" div="">
        Cuncertain=(0.7-BBeta)+0.9*(0.75-BBeta)*(2.8-D/25.4);
    elseif BBeta>=0.2 && BBeta<=0.6
        
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4);
        end
    elseif BBeta>=0.6 && BBeta<=0.75
        
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
        else
            Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4);
        end
        
        
    elseif BBeta>0.75
        disp('Beta is more than 0.75')
    end
else
    Cc=0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
        (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3 +0.011*(0.75-BBeta)*(2.8-(D)/0.0254); % D shod be mm
    
    if BBeta>=0.1 && BBeta <0 .2="" div="">
        Cuncertain=(0.7-BBeta);
    elseif BBeta>=0.2 && BBeta<=0.6
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=0.5+0.5;
        else
            Cuncertain=0.5;
        end
        
    elseif BBeta>=0.6 && BBeta<=0.75
        if BBeta >0.5 && RRe<10000 div="">
            Cuncertain=(1.667*BBeta-0.5)+0.5;
        else
            Cuncertain=(1.667*BBeta-0.5);
        end
    elseif BBeta>0.75
        disp('Beta is more than 0.75')
    end
    
end

F(2)=Q(2) - epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density); % [=] m3/s


subplot(2,1,1)
    plot(QB*3600,error,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'Error'})
    
    subplot(2,1,2)
    plot(QB*3600,Cuncertain,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'uncertainity of discharge coefficient%'})

% figure('name', 'Diameter versus Error and uncertainity of discharge coefficient')
i=2;
while error>0.0001
    
    Q(i+1)=Q(i)-F(i)*(Q(i)-Q(i-1))/(F(i)-F(i-1));
   QB=Q(i+1);
    BBeta
    M2=2*L2/(1-BBeta);
    if Pratio < 0.75
        epsil=1;
    else
        
        epsil=1-(0.351 + 0.256 *BBeta^4 +0.93*BBeta^8)*(1-(P2/P)^(1/kk));
    end
    
    RRe=4*QB/(v*D*pi);
    A=(19000*BBeta/RRe)^0.8;
    
    if D<0 .0712="" div="">
        Cc= 0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3;
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta)+0.9*(0.75-BBeta)*(2.8-D/25.4);
        elseif BBeta>=0.2 && BBeta<=0.6
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4);
            end
        elseif BBeta>=0.6 && BBeta<=0.75
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4);
            end
            
            
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        
        
    else
        Cc=0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3 +0.011*(0.75-BBeta)*(2.8-(D)/0.0254); % D shod be mm
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta);
        elseif BBeta>=0.2 && BBeta<=0.6
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end
            
        elseif BBeta>=0.6 && BBeta<=0.75
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5);
            end
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        
    end
    
    F(i+1)=Q(i+1) - epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density); % [=] m3/s
    
    error= abs(Q(i)-Q(i-1));
    
    i=i+1;
    
    
    
    
    
    subplot(2,1,1)
    plot(QB*3600,error,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'Error'})
    
    subplot(2,1,2)
    plot(QB*3600,Cuncertain,'*')
    hold on
    xlabel({'FLOW'})
    ylabel({'uncertainity of discharge coefficient%'})
end


CALCULATION_ERROR=error;
Pressloss=(sqrt(1-BBeta^4*(1-(Cc)^2))-Cc*BBeta^2)/(sqrt(1-BBeta^4*(1-(Cc)^2))+Cc*BBeta^2)*DeltaP;
PresslossCoef=(sqrt((1-BBeta^4*(1-Cc^2))/(Cc*BBeta^2))-1)^2;
volumeflow=epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density)*3600;
ORIFICEDIAMETER_in=d*100/2.54

BETA_RATIO=BBeta
% Flowrate=Q(i-1)


switch fluid
    case 'gas'
        
        Qv=QB*3600 ; %[=] m3/hr
        disp('fluid flow [=] m3/s')
        Qpersecond=QB
        disp('fluid flow [=] m3/hr')
        Qperhour=Qv   % [=] m3/hr
        disp('fluid flow [=] MMSCFD')
        Qst=288*P*Qv/(Z*101325*T)*24*35.3146667*1e-6 % MMSCFD
        Qmaxst=288*P*Qmax/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
        Qminst=288*P*Qmin/(Z*101325*T)*24*35.3146667*3600*1e-6 % MMSCFD
    case 'liquid'
        
        Qmaxperhour=pi*D^2/4*5*3600 % velocity = 3 [=] m3/hr
        Qminperhour=pi*D^2/4*0.5*3600 % Velocity = 1 [=] m3/hr
        Qv=QB*3600; %[=] m3/hr
        disp('fluid flow [=] m3/hr')
        Qperhour=Qv
        disp('fluid flow [=] m3/s')
        Qpersecond=QB
        
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check uncertainity for  beta in different Q

figure('name', 'Diameter versus uncertainity of discharge coefficient for constant calculated Beta in different Q')


M2=2*L2/(1-BBeta);
QD=Qmin;


while QD<=Qmax
    if Pratio < 0.75
        epsil=1;
    else
        
        epsil=1-(0.351 + 0.256 *BBeta^4 +0.93*BBeta^8)*(1-(P2/P)^(1/kk));
    end
    
    RRe=4*QD/(v*D*pi);
    A=(19000*BBeta/RRe)^0.8;
    
    if D<0 .0712="" div="">
        Cc= 0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3;
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta)+0.9*(0.75-BBeta)*(2.8-D/25.4);
        elseif BBeta>=0.2 && BBeta<=0.6
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
            end
        elseif BBeta>=0.6 && BBeta<=0.75
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4);
            end
            
            
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        
        
    else
        Cc=0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3 +0.011*(0.75-BBeta)*(2.8-(D)/0.0254); % D shod be mm
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta);
        elseif BBeta>=0.2 && BBeta<=0.6
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end
            
        elseif BBeta>=0.6 && BBeta<=0.75
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5);
            end
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        FF=QD - epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density); % [=] m3/s
        
        QD=QD+(Qmax-Qmin)/10;
        
        subplot(2,1,1)
        plot(QD*3600,FF,'*')
        hold on
        xlabel({'FLOW'})
        ylabel({'Error'})
        
        subplot(2,1,2)
        plot(QD*3600,Cuncertain,'*')
        hold on
        xlabel({'FLOW'})
        ylabel({'uncertainity of discharge coefficient%'})
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check uncertainity diffrent beta for constant flow

figure('name', 'Diameter versus uncertainity of discharge coefficient for constant calculated Beta in different Q')



QD=volumeflow;
dd=dmin;

while dd<=dmax
    BBeta=dd/D;
    M2=2*L2/(1-BBeta);
    if Pratio < 0.75
        epsil=1;
    else
        
        epsil=1-(0.351 + 0.256 *BBeta^4 +0.93*BBeta^8)*(1-(P2/P)^(1/kk));
    end
    
    RRe=4*QD/(v*D*pi);
    A=(19000*BBeta/RRe)^0.8;
    
    if D<0 .0712="" div="">
        Cc= 0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3;
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta)+0.9*(0.75-BBeta)*(2.8-D/25.4);
        elseif BBeta>=0.2 && BBeta<=0.6
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=0.5+0.9*(0.75-Beta)*(2.8-D/25.4);
            end
        elseif BBeta>=0.6 && BBeta<=0.75
            
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5)+0.9*(0.75-BBeta)*(2.8-D/25.4);
            end
            
            
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        
        
    else
        Cc=0.5961+ 0.0261*BBeta^2- 0.216*BBeta^8 + 0.000521* (1e6*BBeta/RRe)^0.7 + (0.0188 +0.0063*A)*(1e6/RRe)^0.3 + ...
            (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*BBeta^4/(1-BBeta^4)-0.031*(M2-0.8*M2^1.1)*BBeta^1.3 +0.011*(0.75-BBeta)*(2.8-(D)/0.0254); % D shod be mm
        
        if BBeta>=0.1 && BBeta <0 .2="" div="">
            Cuncertain=(0.7-BBeta);
        elseif BBeta>=0.2 && BBeta<=0.6
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=0.5+0.5;
            else
                Cuncertain=0.5;
            end
            
        elseif BBeta>=0.6 && BBeta<=0.75
            if BBeta >0.5 && RRe<10000 div="">
                Cuncertain=(1.667*BBeta-0.5)+0.5;
            else
                Cuncertain=(1.667*BBeta-0.5);
            end
        elseif BBeta>0.75
            disp('Beta is more than 0.75')
        end
        FF=QD - epsil*Cc/(sqrt(1-BBeta^4))*pi/4*d^2*sqrt(2*DeltaP/density); % [=] m3/s
        
        dd=dd+(dmax-dmin)/10;
        
       
       plot(dd*100/2.54,Cuncertain,'marker','^')
        hold on
        xlabel({'diameter'})
        ylabel({'uncertainity of discharge coefficient%'})
    end
end

Calculate Orifice Coefficient for gas orifice:


clear all
clc
format long

disp(' Dear colleague , First of all Thank you for using this program. Then Please first check the number of your data and make two excell file. 1)INPUT_COND and 2) COND_RESULT in 2003 ecxell worksheet')

disp('Flange Type, T[=] K, P[=] barg, density [=] kg/m3, viscosity[=] CP, kk=Cp/Cv, Beta=d/D, D[=]in')

disp('Please check the number of your data and then please check line 80 to 84 and arrange the excell file first')
disp(' Contact mail: khazraee.masoom@alumni.ut.ac.ir')


% T=308.1
% p=3.5
% density=5.089
% viscosity=1.113e-2
% kk=1.213
% Beta=input('Beta=')
Beta=0.5525
% D=input('Pipe Diameter[=]in =')
D=12.0617
% disp('Orifice Delta Pressure [=] inH2O =100')
DeltaP=100  % in H2O

COND=xlsread('INPUT_COND.xls', 1)
[nn,mm]=size(COND)

for j=1:nn
    j
    pp=COND(j,1)*0.986923  % atm
    T=COND(j,2)
    kk=COND(j,3)
    viscosity=COND(j,4)
    Z=COND(j,5)
    density=COND(j,6)
 
    [QActual_m3_hr, QQ, Orifice_Coefficient,StQ]= Calculate_C_Oriffice (T,pp,density,viscosity,kk,Beta,D,DeltaP,Z)
 
    ACTUALFLOW_m3_s(j)=QQ
    StFlow_MMSCFD(j)=StQ
    ORIFICE_COEFFICIENT(j)=Orifice_Coefficient
end


X=[COND(:,1)*0.986923  COND(:,2)];  % Independent Variable Matrix
Y=[ORIFICE_COEFFICIENT'];

modelfun=@(b,X)b(1).*X(:,1)+b(2).*X(:,2)+b(3).*X(:,1).^2+b(4).*X(:,2).^2+b(5).*X(:,1).*X(:,2)+b(6);
% initial gauss
b0 = [7.5; -0.000046465; -0.000439; 0.00000001847;  -0.000010064;0.0272194];   % Initial Parameter Estimates
% Perform a nonlinear regression
     
                                                 
% SSECF = @(b) sum((Y(:) - modelfun(b,X)).^2);                           % Sum-Squared-Error Cost Function
% [b_estd, SSE] = fminsearch(SSECF, b0)                            % Estimate Parameters
% i_fit = modelfun(b_estd,X)                                            % Generate Fit Line


[betaa,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(X, Y(:), modelfun, b0,'ErrorModel','proportional')
XX(:,1)=COND(:,1)*0.986923;%18:1:27  % atm
XX(:,2)=COND(:,2);%293:3:320  % K
Orifice_coficient_fit = modelfun(betaa,XX)                                             % Generate Fit Line
STFLOW=Orifice_coficient_fit*sqrt(100);

for i=1:nn
    FLOW_ERROR(i)=(StFlow_MMSCFD(i)-STFLOW(i))*100/StFlow_MMSCFD(i)
end

NNN=[COND(:,1)*0.986923  COND(:,2) COND(:,3) COND(:,4) COND(:,6) ACTUALFLOW_m3_s' ORIFICE_COEFFICIENT'  StFlow_MMSCFD' STFLOW FLOW_ERROR'];

MMM={'P[=]atm', 'T', 'CP/CV', 'viscosity', 'density', 'Actualflow-m3/s', 'Oriffice-Coef',  'StFlow_MMSCFD' , 'STFLOW(fit)' , 'FLOW_ERROR'};

BBBB={'b(1)', 'b(2)', 'b(3)', 'b(4)', 'b(5)', 'b(6)','MSE' }
BBB=[betaa(1) betaa(2) betaa(3) betaa(4) betaa(5) betaa(6) MSE ]

xlswrite('COND_RESULT',MMM,1,'A1:J1')
xlswrite('COND_RESULT',NNN,1,'A2:J61')

xlswrite('COND_RESULT',BBBB,1,'L1:R1')
xlswrite('COND_RESULT',BBB,1,'L2:R2')

savefile = 'COND_RESULT';


save(savefile)
figure(1)
plot3(COND(:,1),COND(:,2),ORIFICE_COEFFICIENT')
figure(2)
plot3(XX(:,1),XX(:,2), Orifice_coficient_fit)

 TESTORIFICECO = modelfun(betaa,[19.5, 308.1])
 QTEST1=sqrt(DeltaP)*TESTORIFICECO
 INH2OTEST1=DeltaP



%%%%%%%%%%%%%%%%%%%%

function [QActual_m3_hr, QQ, Orifice_Coefficient, StQ]= Calculate_C_Oriffice (T,pp,density,viscosity,kk,Beta,D,DeltaP,Z)
D
% global T p density Beta D viscosity kk DeltaP

DD=D*2.54/100; % m
% DeltaPP=DeltaP*101325/10;   % [=] pa
%DeltaPP=DeltaP*9806.38;  %[=] pa  very cearful unit calculation
DeltaPP=DeltaP*248.84; % pa
PP=pp*101325; %P[=] pa
P2=PP-DeltaPP; % pa
L1=0.0254/DD
L2=0.0254/DD
M2=2*L2/(1-Beta);
Pratio=(P2-101325)/(PP-101325);
viscosity=viscosity/100*0.1  % [=]kg/m.s
dynamic_viscosity=viscosity/density;
v=dynamic_viscosity   % [=] m2/s


%%%%%%%%%%%      Solve Q    Secant method %%%%%%%%%%%%%%%%%%

i=1;
d=Beta*DD
error=10;



Q(1)=pi*DD^2/4*5;    % Guss 1 velocity 5 m/s
QB=Q(i);

M2=2*L2/(1-Beta);
Pratio=(P2-101325)/(PP-101325);
if Pratio < 0.75
    epsil=1;
else
    
    epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/PP)^(1/kk));
end

%         epsil(j)=1;
Re=4*QB/(v*DD*pi);
A=(19000*Beta/Re)^0.8;



C=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/Re)^0.7 + (0.0188 +0.0063*A)*(1e6/Re)^0.3 + ...
    (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(DD)/0.0254); % D shod be mm





F(1)=Q(i)- epsil*C/(sqrt(1-Beta^4))*pi/4*(Beta*DD)^2*sqrt(2*DeltaPP/density); % [=] m3/s






Q(2)=pi*DD^2/4*10;    % Guss 1 velocity 10 m/s



QB=Q(2);

M2=2*L2/(1-Beta);
if Pratio < 0.75
    epsil=1;
else
    
    epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/PP)^(1/kk));
end


Re=4*QB/(v*DD*pi);
A=(19000*Beta/Re)^0.8;


C=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/Re)^0.7 + (0.0188 +0.0063*A)*(1e6/Re)^0.3 + ...
    (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(DD)/0.0254); % D shod be mm



F(2)=Q(2) - epsil*C/(sqrt(1-Beta^4))*pi/4*(Beta*DD)^2*sqrt(2*DeltaPP/density); % [=] m3/s


i=2;
while error>0.0001
    
    Q(i+1)=Q(i)-F(i)*(Q(i)-Q(i-1))/(F(i)-F(i-1));
    QB=Q(i+1);
    Beta;
    M2=2*L2/(1-Beta);
    if Pratio < 0.75
        epsil=1;
    else
        
        epsil=1-(0.351 + 0.256 *Beta^4 +0.93*Beta^8)*(1-(P2/PP)^(1/kk));
    end
    
    Re=4*QB/(v*DD*pi);
    A=(19000*Beta/Re)^0.8;
    
    
    C=0.5961+ 0.0261*Beta^2- 0.216*Beta^8 + 0.000521* (1e6*Beta/Re)^0.7 + (0.0188 +0.0063*A)*(1e6/Re)^0.3 + ...
        (0.043 + 0.08 *exp(-10*L1)-0.123*exp(-7*L1))*(1-0.11*A)*Beta^4/(1-Beta^4)-0.031*(M2-0.8*M2^1.1)*Beta^1.3 +0.011*(0.75-Beta)*(2.8-(DD)/0.0254); % D shod be mm
    
    
    F(i+1)=Q(i+1) - epsil*C/(sqrt(1-Beta^4))*pi/4*(Beta*DD)^2*sqrt(2*DeltaPP/density); % [=] m3/s
    
    error= abs(Q(i)-Q(i-1));
    
    i=i+1;
    
    
    
    
end
Q=Q*3600; %m3/hr
[n,m]=size(Q);

QQ=Q(n,m)/3600; % m3/s
Z
T

StQ=(Q(n,m)*PP*(15+273)/(Z*T*101325))*(100/(2.54*12))^3*24*10^-6 % MMSCFD

% Orifice_Coefficient=QQ/sqrt(DeltaPP);   % QQ m3/s
Orifice_Coefficient=StQ/sqrt(DeltaP); % StQ MMSCFD and DeltaP[=] inH2O
QActual_m3_hr=Q(n,m);

Monday, November 28, 2016

Calculation , Perforated Tee Distributor

Calculation , Perforated Tee Distributor

clc

clear
all



L=3/2;h=3;

% number of perforation in each section

D=8; %inch

d=0.5; %inch

ro=1000;

branch=2;

c=0.6;

Qf=250;
%m3/hr % *1/2 because of 2 branchs

P=0.04; %bar

e=0.0018; % inch

qhole=1;%m3/hr flow from final perforation

% dL= L/(n-1);% lenght difference between perforations

A= 3.14*((D*0.0254)^2)/4; % Area of pipe

a= 3.14*((d*0.0254)^2)/4; % Area of Perforation

p(1)=P*101325;% 1bar= 101325 Pa

Q(1)=Qf/branch;%entrance flow in each branch

aatotal=Q(1)/(c*sqrt(2*p(1)/ro)*3600)

nn=(aatotal/a)/h

nn=round(nn)

nn=nn+1

dL= L/(nn-1);
% lenght difference between perforations

u(1)=(Q(1)/A)/3600;

Re(1)=ro*u(1)*(D*0.0254)/0.0008;

f(1)=((1/(-1.8*log10(0.27*(e/D)+(6.5/Re(1))))))^2;

DPsolidwallpipe=f(1)*(L/(D*0.0254))*((ro*u(1)^2)/(2))

holeditanceZ=dL-d*0.0254
% distance between each perforation in z direction

holedistanceR=(D-h*d)/h %distance between perforations in R direction



distanceratioZ=(holeditanceZ/0.0254)/D

dholee =[5/16,7/16,1/2,3/4]

for b=1:4

aholee(b)=3.14*(dholee(b)*0.0254)^2/4

end



q(1)=qhole/3600



for j=2:10



q(j)=q(j-1)-q(1)/10

end



% q(11)=q(10)-q(1)/10;

q

for b=1:4

for j=1:10

vradial(b,j)=q(j)/aholee(b);

end

end

for
b=1:4

for j=1:10

DeltapHole(j,b)=((ro/2)*((q(j)/aholee(b))/c)^2);

aatotal(b,j)=(Qf/branch)/(c*sqrt(2*DeltapHole(j,b)/ro)*3600)

n(b,j)=(aatotal(b,j)/aholee(b))/h

dL(b,j)= L/(n(b,j)-1);

end

end

nn=n(1,:)

max(real(nn))

size(nn)





for b=1:4

% b=3

for j=1:10

j

QQ(j,1)=q(j);

pp(j,1)=DeltapHole(j,b);

k=1;

x(1)=0;

for k=1:round(n(b,j))

k;

u(j,k)=(QQ(j,k)/A)/3600 ;
% Fluid Velocity in Pipe

Re(j,k)=ro*u(j,k)*(D*0.0254)/0.0008;

f(j,k)=((1/(-1.8*log10(0.27*(e/D)+(6.5/Re(j,k))))))^2;

dp(j,k)=f(j,k)*(1/(D*0.0254))*((ro*u(j,k)^2)/(2));

dpl(j,k)=dp(j,k)*dL(b,j);



pp(j,k+1)=pp(j,k)+(dp(j,k)*dL(b,j));

QQ(j,k+1)=QQ(j,k)+h*q(j);

Qtotal(b,j)=QQ(j,k)+QQ(j,k+1);

x(k+1)=x(k)+dL(b,j);



holeditanceZ(b,j)=(dL(b,j)-dholee(b)*0.0254)*100;
% distance between each perforation in z directioncm

holedistanceR(b,j)=(D-h*dholee(b))/h; %distance between perforations in R direction

distanceratioZ(b,j)=(holeditanceZ(b,j)/2.54)/D;



end

numberholes(b,j)=round(n(b,j))



% figure(1)

pinterance(b,j)=pp(j,round(n(b,j)))/(1000*9.8);









end



end



numberholes

qhole=q*3600



holeditanceZ

holedistanceR

distanceratioZ

vradial

pinterance





xlswrite(
'perforatedpipe.xlsx', numberholes,'numberholes','E1')

% save('A1','A1')

% load('A1','A1')

xlswrite('perforatedpipe.xlsx', holeditanceZ,'holedistanceZ','E1')

xlswrite('perforatedpipe.xlsx', holedistanceR,'holedistanceR','E1')

xlswrite('perforatedpipe.xlsx', distanceratioZ,'distanceratioZ','E1')

xlswrite('perforatedpipe.xlsx', pinterance,'pinterance','E1')