Channel Flow Rate Optimization
Prompt
Analytical Approach
To analytically solve this problem, we are going to use the method of constrained optimization via subsitution. The general premise of this is instead of using external functions as our constraints when optimizing a problem, we can instead substitute the constrains into the equation and then optimize. Solving for the optimal (minimizing) perimeter involves somehow substituting the constraint for area into the perimeter equation before optimizing. Solving for the optimal (minimizing) area involves subsituting the constraint of perimeter into the equation before optimizing. Optimizing both area and perimeter give us the optimal relationship between base and height, not a physical value. This optimized relationship (e.g. B = 2*H) is then subsituted into the flow rate equation to find the actual value for one of the variables that makes the flow rate function true. In effect, we are finding the optimized ratio between base and height using the perimeter and area equations that will allow us to determine the smallest possible values for base and height that satisfy the flow rate requirements.
Once we have determined the correct values for base and beight (in meters) from the flow rate equation, we can plug these into our cost equation to find the minimum cost. Remember, we do not need to optimize the cost function using this approach because we have already minimized base and height to give us the smallest possible area and perimeter that still satisfy the constraint of the flow rate equation. In effect, area and perimeter act as constraints for each other to find the optimal ratio of base to height which, when substituted into the flow rate equation, minimizes the final numerical values for B and H. These final numerical values represent the minimum base and height required that satisfy the flow rate constraint, area constraint, and perimeter constraint. Because we already minimized area and perimeter, we can easily determine the cost without needing to take any additional derivatives.
Source: https://help.innovyze.com/display/XDH2016v1/Rectangular+Channel
Relevant Equations
* Note that equations (3) and (4) are unique to this problem, where we are assuming a rectangular canal with P = B + 2H
Step 1: Finding the Base-Height Relationship that Defines the Minimized Perimeter
We want to minimize perimeter here (which is an optimization problem). However, a constraint of the perimeter is that the dimensions of the perimeter must create a cross-sectional area that satisfies A = B*H. So the area equation will be used here as a constraint. We are using the substitution method to optimize the perimeter, so our first step is to substitute area into the perimeter equation. We will then differentiate with respect to either base or height (doesn't matter) and set the equation equal to zero. We can then determine what the "optimal" relationship is.
Step 2: Plugging the Optimized Base:Height Relationship into Our Flow Rate Equation to Solve for the Actual Values in Meters
We take our given values n, s, and Q as well as our optimized relationship between base and height to solve for our actual B and H. Here, the flow rate equation effectively acts as a constraint for our optimization of the perimeter. Afterall, we want to minimize perimeter not flow rate.
Step 3: Optimizing Cost for our Canal
Looking at sub-section "b" in the prompt, we are now tasked with optimizing cost for our canal. We are given parameters that represent the cost per unit length (lining cost) of the perimeter, as well as the cost per unit cross-sectional area (excavation cost). Our objective is to determine a B and H relationship that minimizes cost while also satisfying the flow rate requirements from part (a).
The good news is, no further complex optimization is required! We know this by looking at the equation for cost above. The output "C" is determined by two inputs, the area and the perimeter of the canal. Most people's first guess would be to plug in equations 3 and 4 for area and perimeter into the above equation and optimize in terms of B and H. This approach, however, is incorrect. This is because it does not take into account the constraints on A and P (namely their relationship), and it also does not take into account the flow rate requirements.
The tricky yet convenient part of this is, we already know what the optimized relationships between A and P are with the constraints taken into account. This is because we found the relationship between B and H that minimized perimeter while also satisfying the flow rate requirements. We then used this relationship between B and H to determine the smallest cross-sectional area that satisfied the flow rate requirements, which is seen in equation 5 (told you we would come back to it!). There is another way to determine the minimized area that satisfies the perimeter constraint. We could use the same substitution method for constrained optimization but substitute the perimeter constraint into the differential equation that optimizes area (see the math in appendix II). This will give us the same result A = 2*(H^2 ) and P = 4H with a ratio of B = 2H. Because we are not solving for the actual base and height values, just the relationship between them that minimizes A and P, we can then plug our result into the flow rate equation to determine the numerical length based on the given Q, s, and n values,
Looking back at the equation above, we can see how minimizing the A term would bring the c1*A term to its smallest possible value, and minimizing P would also minimize the c2*P term (since c1 and c2 are constants). We cannot, however, let the "A" input approach any number it wants. It needs to satisfy the perimeter constraint, as the two terms are connected by the mutual sharing of B and H terms. This is also true in reverse, where the P term must only hold values that satisfy the relationship between A and P.
We cannot let both terms approach zero either, as at some point the area would get small enough torestrict flow below our flow rate requirements (acting as another constraint to the cost function).
Again, fortunately we already did all of this work. We already found the exact base and height dimensions that minimize perimeter while creating a canal that satisfies the flow rate requirements and the area-to-perimeter relationship. Using these known values, we can simply plug them into the cost equation to determine the smallest possible cost.
This may be in contrast with intuition. Given the fact that the cost of excavation is different from the cost of lining, why isn't there a new "optimized" base and height combination that minimize cost? Despite this reasoning, the answer is that regardless of what c1 and c2 are, we already found the smallest possible area that satisfies the flow rate, and the smallest perimeter that satisfies this area (and subsequently the smallest perimeter that satisfies the flow rate). Flow rate is particularly dependent on area, as we can see from the simplified equation with area and perimeter on one side. An increase in perimeter (holding area constant, as well as "n" and "s") would necessarily mean a less efficient shape (requiring excessive lining per unit area and non-optimized flow).
Numerical Approach
For my numerical approach I used MATLAB's built-in fmincon function. This function uses a combination of numerical methods to determine the input values that satisfy the given constraints. I set the problem up as a constrained optimization problem with one constraint in the form of a non-linear inequality. This nonlinear constraint combined the constrains of area, perimeter, and flow rate equations into one equation. Given our simplified flow rate equation from the analytical solution, I subtracted all terms to one side so that only specific combinations of base and height could satisfy the inequality when it was compared to zero. Because we had the equation for the flow rate relationship to base and height, moving all terms to one side and subtracting a small tolerance allows us to compare it to "less than or equal to zero".
See Appendix I for MATLAB implementation
Discussion and Error
The numerical solution converged after 18 iterations and provided an extremely small true error compared to the analytical solution. MATLAB's fmincon appears to work extremely well for this problem.
Appendix I: MATLAB code for numerical solution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name: Bryan Bennett
% Purpose: To provide a numerical method via MATLABs fmincon to optimize a
% complex function using multiple constraints
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all; clc;
%% Setting up optimization function
%Set linear equalities and inequalities
A = []; b = []; Aeq = []; beq = [];
%Set lower and upper bounds. Format is =[base height]
lb = [0 0]; ub = [10 10];
%Initial guesses
x0 = [1 1];
%Cost coefficients
c1 = 100; c2 = 50;
% Function to minimize
fun_perimeter = @(x) x(1)+ 2.*x(2);
fun_area = @(x) x(1).*x(2);
fun_cost = @(x) x(1).*x(2).*c1 + (x(1) + 2.*x(2)).*c2;
% Nonlinear constraints (to satisfy flow rate function)
nonlcon = @nlConstraint;
% Used to observe error and function details
options = optimoptions('fmincon','Display','iter','Algorithm','interior-point');
% Optimize perimeter to find base and height
p_optimization = fmincon(fun_perimeter,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
Base_p = p_optimization(1); Height_p = p_optimization(2);
Perimeter = perimeter(p_optimization);
% Optimize area to find base and height
a_optimization = fmincon(fun_area,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
Base_a = a_optimization(1); Height_a = a_optimization(2);
Area = area(a_optimization);
Cost = fun_cost(p_optimization);
%% Functions
function [c,ceq] = nlConstraint(x)
Q = 1;
n = 0.035;
s = 0.003;
tol = 0.00001;
% This is the contraint relationship adapted from flow rate equation
c = ((Q.*n)./(s.^0.5)).^3 - (((area(x)).^5)./((perimeter(x)).^2)) - tol;
ceq = 0;
end
function y = perimeter(x)
y= x(1)+ 2.*x(2);
end
function y = area(x)
y = x(1).*x(2);
end
Appendix II: Minimizing Area with Perimeter as a Constraint
Link: https://drive.google.com/file/d/1hVSP1eoAQAczw8NdnQBJM341c6efwUcG/view?usp=sharing