I am simulating water level at every 1 minute interval from an event of rainfall. I am using Jacobian method of 5 row and 5 column
1 view (last 30 days)
Show older comments
I am simulating a biorention swale wherein rainfall runoff is coming into catch basin and catch basin is connected to a pipe (upper pipe) which is perforated and water is flowing into a sand filled trench through the perforation. At the bottom of the trench there is another perforated pipe (lower pipe) which is draining water from the trench to city sewer through the lower pipe. In my problem I have 5 mass balance equation which I put as F1 matrix and I developed Jacobian matrix by differentiating those 5 equation. I want to run the model for 12 hrs period even though the rainfall period is 4 hrs.I wrote the problem is as follows:
clc
clear all
format long
% Solve nonlinear system F(x)=0 using NewtonÌs method
% input data
%Rainfall data
i_n=[0.00000099
0.00000101
0.00000102
0.00000103
0.00000105
0.00000106
0.00000107
0.00000108
0.00000110
0.00000111
0.00000111
0.00000112
0.00000114
0.00000115
0.00000117
0.00000118
0.00000119
0.00000121
0.00000122
0.00000124
0.00000125
0.00000127
0.00000129
0.00000131
0.00000133
0.00000135
0.00000137
0.00000139
0.00000141
0.00000143
0.00000145
0.00000148
0.00000151
0.00000154
0.00000157
0.00000159
0.00000162
0.00000165
0.00000168
0.00000171
0.00000174
0.00000179
0.00000184
0.00000189
0.00000193
0.00000198
0.00000203
0.00000208
0.00000213
0.00000217
0.00000222
0.00000232
0.00000242
0.00000251
0.00000261
0.00000271
0.00000281
0.00000290
0.00000300
0.00000310
0.00000320
0.00000357
0.00000394
0.00000431
0.00000468
0.00000505
0.00000542
0.00000579
0.00000616
0.00000652
0.00000689
0.00000992
0.00001294
0.00001596
0.00001898
0.00002200
0.00002502
0.00002805
0.00003107
0.00003409
0.00003711
0.00003429
0.00003147
0.00002864
0.00002582
0.00002300
0.00002018
0.00001736
0.00001453
0.00001171
0.00000889
0.00000850
0.00000811
0.00000772
0.00000733
0.00000693
0.00000654
0.00000615
0.00000576
0.00000537
0.00000498
0.00000484
0.00000470
0.00000457
0.00000443
0.00000429
0.00000415
0.00000401
0.00000387
0.00000374
0.00000360
0.00000286
0.00000282
0.00000277
0.00000273
0.00000268
0.00000263
0.00000259
0.00000254
0.00000250
0.00000245
0.00000241
0.00000237
0.00000234
0.00000231
0.00000228
0.00000225
0.00000222
0.00000218
0.00000215
0.00000212
0.00000209
0.00000206
0.00000204
0.00000202
0.00000199
0.00000197
0.00000194
0.00000192
0.00000190
0.00000187
0.00000185
0.00000183
0.00000181
0.00000180
0.00000178
0.00000176
0.00000175
0.00000173
0.00000171
0.00000170
0.00000167
0.00000166
0.00000164
0.00000163
0.00000161
0.00000160
0.00000158
0.00000157
0.00000155
0.00000154
0.00000153
0.00000151
0.00000150
0.00000149
0.00000148
0.00000146
0.00000145
0.00000144
0.00000143
0.00000142
0.00000140
0.00000139
0.00000138
0.00000137
0.00000136
0.00000135
0.00000134
0.00000133
0.00000133
0.00000132
0.00000131
0.00000130
0.00000129
0.00000128
0.00000127
0.00000126
0.00000125
0.00000125
0.00000124
0.00000123
0.00000122
0.00000121
0.00000121
0.00000120
0.00000119
0.00000118
0.00000118
0.00000117
0.00000116
0.00000116
0.00000115
0.00000114
0.00000114
0.00000113
0.00000112
0.00000112
0.00000111
0.00000111
0.00000110
0.00000109
0.00000109
0.00000108
0.00000108
0.00000107
0.00000106
0.00000106
0.00000105
0.00000105
0.00000104
0.00000104
0.00000103
0.0000010400
0.0000010300
0.0000010200
0.0000010100
0.0000010000
0.0000009900
0.0000009800
0.0000009700
0.0000009600
0.0000009500
0.0000009400
0.0000009300
0.0000009200
0.0000009100
0.0000009000
0
0
0
0
0];
A_catmt=385;
%disp(i_n1)
QET_n=0;
QET_n1=0;
g=9.1;
W=3.55;
V=.68;
K_ns=.0000018;
K_uns=0.0084;
K=.1;
r=0.8;
s=1;
rs=r*s;
I_n=1;
I_n1=2;
d_p1=0.15;
d_p2=0.2;
%Hdes=dead storage height of catc basin
Hdes_n1=0.8;
Hdes_n=0.8;
L1=35;
L2=17.33;
L3=16;
fi=.98;
I_a=0;
%area of bioretention cell calculation
A_bc=L1*.30;
A_cb=0.61^2;
%cross sectional area
A_p1=pi*d_p1^2/4;
A_p2=pi*d_p2^2/4;
A_mh=pi*.8^2/4;
A_mhexit=pi*.2^2/4;
H_bc=0.8;
D_p1=.15;
D_p2=0.2;
D_mhexit=0.2;
C_d=0.65;
C=0.65;
i_n1=0;
%time step
delta_t=60;
% heights at t=0, boundary situation
H1_n = 0;
H2_n = 0;
H3_n = 0;
H4_n = 0;
H5_n = 0;
% constant value of H(primes)
% substitue height variables into x variables in order to eliminate square root expression in orifice equation
X1_n = sqrt(H1_n);
X2_n = sqrt(H2_n);
X3_n = H3_n;
X4_n = sqrt(H4_n);
X5_n = sqrt(H5_n);
% set initial iteration for the next time step, variables X_n1
X1_n1_old = [1
1
1
1
1];
X1_n1 = X1_n1_old(1,1);
X2_n1 = X1_n1_old(2,1);
X3_n1 = X1_n1_old(3,1);
X4_n1 = X1_n1_old(4,1);
X5_n1 = X1_n1_old(5,1);
fprintf('step X1_n1 X2_n1 X3_n1 X4_n1 X5_n1 X1_n X2_n X3_n X4_n X5_n \n')
% set stopping conditions and maximum iteration runs
error = 1*10^(-5);
iter=0;
itermax=5;
aa1=0.5*delta_t*A_p1*sqrt(2*g)*(C_d/D_p1)*sqrt(C_d/1.656)
aa2=A_cb
aa3=A_cb*Hdes_n1
aa4=0.5*delta_t*fi*A_catmt
bb1=.6*0.5*delta_t*2756
bb2=.6*0.5*delta_t*245.47
bb3=.6*0.5*delta_t*6.75
bb4=0.1177*L1
cc1=0.2*L2*W
cc2=delta_t*(L2+W)*K_ns*r*s
cc3=0.5*delta_t*QET_n
cc4=0.5*delta_t*0.0113*C*sqrt(2*g)
dd1=0.1177*L3
dd2=0.5*delta_t*A_p2*sqrt(2*g)*(C_d/D_p2)*sqrt(C_d/1.656)
ee1=0.5*delta_t*A_mhexit*sqrt(2*g)*(C_d/D_mhexit)*sqrt(C_d/1.656)
ee2=A_mh
%X1_n=0.13670; X2_n=-1.15261; X3_n=-0.00013; X4_n=-0.02598; X5_n=-0.07425;
% X1_n1=0.11496; X2_n1=-0.13479; X3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n1_old(1,1);
X2_n1 = X1_n1_old(2,1);
X3_n1 = X1_n1_old(3,1);
X4_n1 = X1_n1_old(4,1);
X5_n1 = X1_n1_old(5,1);
F1=[aa1*X1_n1^3+aa2*X1_n1^2+aa3+aa1*X1_n^3-aa2*X1_n^2-aa3-aa4*i_n(kk+1,1)-aa4*i_n(kk,1)
aa1*X1_n1^3-bb1*X2_n1^6+bb2*X2_n1^4-bb3*X2_n1^2-bb4*X2_n1^2+aa1*X1_n^3-bb1*X2_n^6+bb2*X2_n^4-bb3*X2_n^2+bb4*X2_n^2
bb1*X2_n1^6-bb2*X2_n1^4+bb3*X2_n1^2-cc1*X3_n1-cc2*X3_n1+bb1*X2_n^6-bb2*X2_n^4+bb3*X2_n^2-cc2*X3_n+cc1*X3_n-cc3*QET_n1-cc3*QET_n-cc4*X3_n1^1.5-cc4*X3_n^1.5
dd1*X4_n1^2+dd2*X4_n1^4-cc4*X3_n1^1.5+dd2*X4_n^4-cc4*X3_n^1.5-dd1*X4_n^2
ee1*X5_n1^3+ee2*X5_n1^2-dd2*X4_n1^3-ee2*X5_n^2+ee2*X5_n^3-dd2*X4_n^3];
end
fprintf('%8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f %8.5f\n',kk,X1_n1,X2_n1,X3_n1,X4_n1,X5_n1,X1_n,X2_n,X3_n,X4_n,X5_n)
X1_n = X1_n1;
X2_n = X2_n1;
X3_n = X3_n1;
X4_n = X4_n1;
X5_n = X5_n1;
end
Please help me if any one can to run the model simultaneously for 12 hrs or more. I sincerely appreciate your help. Anticipated thanks.
Zulhash
0 Comments
Answers (1)
Walter Roberson
on 1 Mar 2011
If your rainfall data is per minute, then your line
for kk=1:241
would be stepping minute by minute over 240 minutes = 4 hours.
In two places in the body of the "for kk" loop, you reference
i_n(kk+1,1)
Replace those references by initializing
i_n_kk = 0;
if kk+1 <= length(i_n); i_n_kk = i_n(kk); end
then use i_n_kk instead of i_n(kk+1,1) . This will use 0 for all later rain data after the end of your i_n matrix.
2 Comments
Walter Roberson
on 2 Mar 2011
You can get better accuracy in your solutions by replacing
inv(J1)*F
with
J1\F
However, even with that change, you will start getting warnings of singularity at exactly the same place.
Is your data really only available to 3 decimal places, or is that an artifact of the way you displayed it after you read it in from some file? You may wish to switch to "format long g" instead of "format long" and re-examine the rainfall data you obtained.
Unfortunately, with those equations, I have no "feel" for what is being calculated, and so am not able to advise much on what is going on.
In your first line of your construction of F1, you have replaced one occurrence of i_n(kk,1) with i_n_kk, but you missed the second replacement on that line.
Also, your logic for i_n_kk is off a bit. You should move
i_n_kk = 0
down one line to inside the "for kk" loop. You would then have the "if" statement and the assignment to i_n_kk for the situation where kk is in range. Your "end" statement for that "if" should go after that assignment rather than being where it is at the second last line of your code.
It appears that your code is falling over the _second_ time i_n(kk) is 0, which suggests that the first time it is 0, the values you are calculating go wacko, and then are just too far out of range to calculate by the second time around. I suggest you examine more carefully your mathematics for the case where i_n first becomes 0.
See Also
Categories
Find more on Monte Carlo Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!