consider the following question: The velocity of water, 𝑣 (m/s), discharged from a…
- User
consider the following question:
The velocity of water, 𝑣 (m/s), discharged from a cylindrical tank through a long pipe can be computed as 𝑣 = √2𝑔𝐻 tanh (√2𝑔𝐻 2𝐿 𝑡) where 𝑔 = 9.81 m/s2, 𝐻 = initial head (m), 𝐿 = pipe length (m), and 𝑡 = elapsed time (s). a. Graphically determine the head needed to achieve 𝑣 = 4 m/s, in 3 s for 5 m-long pipe. b. Determine the head for 𝑣 = 3 − 6 m/s (at 0.5 m/s increments) and times of 1 to 3 s (at 0.5 s increments) via the bisection method with stopping criteria of 0.1% estimated relative error. c. Perform the same series of solutions with the regula falsi method where you choose the same starting points and convergence tolerance. d. Which numerical method converged faster?
starting with b. change the tolerance of the code so the estimated relative error is used instead which is 0.1%. the estimated relative error is as follows: abs((X^n - x^(n-1))/X^(n-1)) where X^n is is the estimated numerical solution in the last iteration and X^(n-1) is the estimated numerical solution in the preceding iteration. Also X = (a + b)/2
Write all code in matlab. Only answer b. use estimated relative error and the provided formula
Add to the following code: function Lab2_Q1_test() %Part A: g = 9.81; L = 5; t = 3; v_targ = 4;
%Evaluation of different H values to find v. H_values = linspace(0, 20, 1000); v_values = arrayfun(@(H) Velocity(H, g, L, t), H_values);
%Creating graph of calculated values of velocity. figure plot(H_values, v_values); hold on plot(H_values, v_targ * ones(1, 1000), ‘-’); ylabel(‘Velocity [m/s]’); xlabel(‘Head [m]’); legend(‘v(H)’, ‘v(4)’);
%Finding closest H value to target velocity (4 m/s). diff = abs(v_targ - v_values); min_diff = min(diff); min_diff_idx = find(diff == min_diff,1, ‘first’); H_graphical = H_values(min_diff_idx); disp([‘a) ‘, num2str(H_graphical) , ’ m’])
%Part B: v_range = 3:0.5:6; t_range = 1:0.5:3;
%Assigning initial values. H_a = 0; H_b = 20; tol = 0.001; %fix tolerance
for i = 1:length(v_range) for j = 1:length(t_range) + 1 if j == 1 Bisection_result(i, j) = v_range(i); else [head, iter] = Bisection(v_range(i), t_range(j-1), g, L, H_a, H_b, tol); Bisection_result(i, j) = head; Bisection_iter(i,j-1) = iter; end end end
disp(‘b) Head values’) disp([’ Velocity’, ’ t = 1.0’,’ t = 1.5’, ’ t = 2.0’, ’ t = 2.5’,’ t = 3.0’]); disp(Bisection_result) disp(‘b) Number of iterations’) disp(Bisection_iter)
end
%Uses formula to calculate velocity. function v = Velocity(H, g, L, t) v = sqrt(2gH) * tanh( (sqrt(2gH) / (2*L)) * t); end
function [H_c, iter] = Bisection(v_range, t, g, L, H_a, H_b, tol) iter = 0; H_c = (H_a + H_b) / 2;
while abs(Velocity(H_c, g, L, t) - v_range) / v_range > tol iter = iter + 1; if Velocity(H_c, g, L, t) < v_range H_a = H_c; else H_b = H_c; end H_c = (H_a + H_b) / 2; end
end
- Assistant
Here's the updated code for part b using the estimated relative error tolerance of 0.1%:
matlab - User
now answer part C in similarly
- Assistant
Here's the updated code for part C using the Regula Falsi method: