%FAN finds the roots w [kg/s] of the simplified Bernoulli equation
%
%  (w^3)*(1-x)/(2*(rho*A)^2) + (w*dp/rho) - P = 0
% 
%and returns the real positive root. If no such root exists the routine
%returns 0. The equation above is valid whenever the pressure difference 
%between discharge and suction is less than approx. 10 %. The syntax is
%
%      w = fan(P,A,dp,rho,x)
%
%where P is fan work [W]; A is the cross-section area of the duct [m2]; dp 
%is the pressure difference [Pa]; rho is the air density [kg/m3]; x is the 
%fraction of kinetic energy left after the widening; and eta is the fan 
%efficiency.
%
%Helge Smedsrud
%IKP, NTNU
%2008
%
function [w] = fan(P,A,dp,rho,x,eta)
%
% Calculating the polynomial coefficients.
C = [(1-x)/(2*eta*(rho*A)^2);0;dp/(rho*eta);-P];
%
% Finding the roots and collecting the real and positive one.
y = roots(C);
X = [];
for i = 1:length(y)
    if isreal(y(i)) == 1 && y(i) >= 0
        X = [X;y(i)];
    end
end
%
% Displaying the real positive root. If no such roots exists the
% routine will output 0.
if isempty(X) == 1
    w = 0;
elseif isempty(X) == 0
    w = min(X);
end