i = 1;
es = 1;
ea = 100;
xl = 0;
xu = 2;
c = @(x) 10 - 20 * (exp(-0.15 * x) - exp(-0.5 * x));
f = @(x) c(x) - 5;
xr1 = (xl + xu) / 2;
fprintf('
i
xl
f(xl)
xu
f(xu)
xr
f(xr)
f(xl)f(xr)
ea\n');
fprintf('%5d %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n', i, xl,
f(xl), xu, f(xu), xr1, f(xr1), f(xl) * f(xr1));
while ea > es
if f(xl) * f(xr1) < 0
xu = xr1;
elseif f(xl) * f(xr1) > 0
xl = xr1;
else
break;
end
xr2 = (xl + xu) / 2;
ea = abs((xr2 - xr1) / xr2) * 100;
i = i + 1;
fprintf('%5d %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n',
i, xl, f(xl), xu, f(xu), xr2, f(xr2), f(xl) * f(xr2), ea);
xr1 = xr2;
end
fprintf('\nPart (a): The distance downstream where oxygen level first falls
to 5 mg/L ');
fprintf('is approximately x = %.5f km\n\n', xr2);
i = 1;
es = 1;
ea = 100;
xl = 0;
xu = 10;
f1 = @(x) 3 * exp(-0.15 * x) - 10 * exp(-0.5 * x);
xr1 = (xl + xu) / 2;
fprintf('
i
xl
f(xl)
xu
f(xu)
xr
f(xr)
f(xl)f(xr)
ea\n');
fprintf('%5d %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n', i, xl,
f(xl), xu, f(xu), xr1, f(xr1), f(xl) * f(xr1));
while ea > es
if f1(xl) * f1(xr1) < 0
xu = xr1;
elseif f1(xl) * f1(xr1) > 0
xl = xr1;
else
break;
end
xr2 = (xl + xu) / 2;
ea = abs((xr2 - xr1) / xr2) * 100;
1
i = i + 1;
fprintf('%5d %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n',
i, xl, f(xl), xu, f(xu), xr2, f(xr2), f(xl) * f(xr2), ea);
xr1 = xr2;
end
fprintf('\nPart (b): The distance downstream where oxygen level is at a
minimum is approximately x = %.5f km\n', xr2);
fprintf('The minimum oxygen concentration at this location is c(x) = %.5f mg/
L\n', c(xr2));
i
xl
f(xl)
f(xl)f(xr)
ea
1
0.00000
5.00000
-0.41773
2
0.00000
5.00000
10.10573 100.00000
3
0.50000
2.02115
1.76616
33.33333
4
0.75000
0.87384
0.32594
14.28571
5
0.87500
0.37300
0.05199
6.66667
6
0.93750
0.13938
0.00371
3.22581
7
0.96875
0.02660
-0.00077
1.58730
8
0.96875
0.02660
-0.00003
0.80000
xu
f(xu)
xr
f(xr)
2.00000
-2.45878
1.00000
-0.08355
1.00000
-0.08355
0.50000
2.02115
1.00000
-0.08355
0.75000
0.87384
1.00000
-0.08355
0.87500
0.37300
1.00000
-0.08355
0.93750
0.13938
1.00000
-0.08355
0.96875
0.02660
1.00000
-0.08355
0.98438
-0.02880
0.98438
-0.02880
0.97656
-0.00118
Part (a): The distance downstream where oxygen level first falls to 5 mg/L is
approximately x = 0.97656 km
i
xl
f(xl)
f(xl)f(xr)
ea
1
0.00000
5.00000
-14.02816
2
0.00000
5.00000
-15.07845 100.00000
3
2.50000
-3.01569
10.03790
33.33333
4
2.50000
-3.01569
10.02250
20.00000
5
3.12500
-3.32345
11.15598
9.09091
6
3.43750
-3.35674
11.24365
4.34783
7
3.43750
-3.35674
11.26180
2.22222
8
3.43750
-3.35674
11.26633
1.12360
9
3.43750
-3.35674
11.26742
0.56497
xu
f(xu)
xr
f(xr)
10.00000
0.67216
5.00000
-2.80563
5.00000
-2.80563
2.50000
-3.01569
5.00000
-2.80563
3.75000
-3.32856
3.75000
-3.32856
3.12500
-3.32345
3.75000
-3.32856
3.43750
-3.35674
3.75000
-3.32856
3.59375
-3.34957
3.59375
-3.34957
3.51562
-3.35498
3.51562
-3.35498
3.47656
-3.35633
3.47656
-3.35633
3.45703
-3.35665
Part (b): The distance downstream where oxygen level is at a minimum is
2
approximately x = 3.45703 km
The minimum oxygen concentration at this location is c(x) = 1.64335 mg/L
Published with MATLAB® R2024b
3