1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| %1.Define data
%1.0.Simple data
rho=1000;
g=9.8;
mu=0.001;
eps=0.001;
NN=9;
NE=14;
error=0.0001;
%1.1.Conectivity matrix
E= [1 -1 0 0 0 0 0 0 0;
1 0 -1 0 0 0 0 0 0;
1 0 0 -1 0 0 0 0 0;
0 1 0 -1 0 0 0 0 0;
0 1 0 0 -1 0 0 0 0;
0 0 1 0 0 -1 0 0 0;
0 0 0 1 0 -1 0 0 0;
0 0 0 1 -1 0 0 0 0;
0 0 1 0 0 0 -1 0 0;
0 0 0 0 0 1 -1 0 0;
0 0 0 0 0 1 0 -1 0;
0 0 0 0 0 0 1 -1 0;
0 0 0 0 0 0 0 1 -1;
0 0 0 1 0 0 0 0 -1];
%1.2.Lengths vector
L=[600;400;900;400;400;350;600;400;300;500;300;350;600;300];
%1.3.Diameters vector
D=[0.5;0.5;0.5;0.5;0.4;0.5;0.5;0.4;0.3;0.3;0.3;0.3;0.3;0.3];
%1.4.Inflows at the nodes vector. The unknown inflows will be set to zero
%initially, and then will change its value (C2, C3, C5)
C=[-0.2;0;0;0;0;0;0;-0.5;-0.3];
%1.5.Piezometric heights vector.
H=[0;200;200;0;500;0;0;0;0];
%1.6.Boolean vector that determines whether we know the inflow Ci or the
%piezometric height Hi (1 for known C and 0 for known H)
BOOL=[1;0;0;1;0;1;1;1;1];
%1.7.Valves f vector
F=[0;0;0;0;10;0;0;15;0;0;0;0;10;0];
%1.8.Initialise Q vector
Q=ones(NE,1);
%1.9.Initialise Lambdas vector
LA=zeros(NE,1);
for i=1:NE
LA(i)=(-2*log(eps/(D(i)*3.7))/log(10))^-2;
end
%2.Problem solution
BOOL2=1;
iteration=1;
RELQ=zeros(NE,100000);
RELX=zeros(NE,100000);
MEMX=zeros(NN,100000);
MEMQ=[Q,zeros(NE,100000-1)];
RESIDUALS=zeros(NE,100000);
while BOOL2==1
%2.1.Define K matrix (diagonal, such that E*H=K*Q)
K=diag([zeros(1,NE)]);
for i=1:NE
K(i,i)=8*abs(Q(i))*(LA(i)*L(i)/D(i)+F(i))/(g*pi^2*D(i)^4);
end
%Calculate residuals for the problem (from the previous iteration
RESIDUALS(:,iteration)=abs(Q-((K\E)*H));
%2.2.Define M matrix as tras(E)*inv(K)*E
M=E'*(K\E);
%2.3.Use of the algorithm (shown in class) to find the A matrix.
A=zeros(NN,NN);
for j=1:NN
%Known C(j)
if BOOL(j)==1
for i=1:NN
A(i,j)=M(i,j);
end
%Known H(j)
else
for i=1:NN
if i==j
A(i,j)=-1;
end
end
end
end
%2.4.Use of the algorithm to find b vector
b=zeros(NN,1);
for i=1:NN
for j=1:NN
b(i)=b(i)-M(i,j)*(H(j)*(1-BOOL(j)));
end
if BOOL(i)==1
b(i)=b(i)+C(i);
end
end
%Solve the system of equations A*x=b
x=A\b;
%Set the values of x into the correspondent vector (either H or C)
for i=1:NN
if BOOL(i)==1
H(i)=x(i);
else
C(i)=x(i);
end
end
%Compute Q
Q=K\E*H;
%Store values of Q and X in two matrices
for i=1:NE
MEMQ(i,iteration+1)=Q(i);
end
for i=1:NN
MEMX(i,iteration+1)=x(i);
end
%Calculate relative increments for Q and X
for i=1:NE
RELQ(i,iteration)=abs((MEMQ(i,iteration+1)-MEMQ(i,iteration))/MEMQ(i,iteration+1));
end
for i=1:NN
RELX(i,iteration)=abs((MEMX(i,iteration+1)-MEMX(i,iteration))/MEMX(i,iteration+1));
end
%Compare relative increments and residuals with the error limit
BOOL2=0;
if max(RELQ(:,iteration))>=error
BOOL2=1;
end
if max(RELX(:,iteration))>=error
BOOL2=1;
end
if max(RESIDUALS(:,iteration))>=error
BOOL2=1;
end
%Recalculate lambda before next iteration
RE=zeros(NE,1);
for i=1:NE
RE(i)=4*rho*abs(Q(i))/(pi*mu*D(i));
end
for j=1:10
for i=1:NE
LA(i)=(-2*log((eps/(D(i)*3.7))+(2.51/(RE(i)*sqrt(LA(i)))))/log(10))^-2;
end
end
iteration=iteration+1;
if (iteration==100000)
BOOL2=0;
end
end
PLOT1=zeros(iteration,1);
PLOT2=zeros(iteration,1);
PLOT3=zeros(iteration,1);
for i=1:iteration
PLOT1(i)=max(RELQ(:,i));
PLOT2(i)=max(RELX(:,i));
PLOT3(i)=max(RESIDUALS(:,i));
end
semilogy(PLOT1)
semilogy(PLOT2)
semilogy(PLOT3) |
Partager