Reputation: 31
One way to improve engineering designs is by formulating the equations describing the design in the form of a minimization or maximization problem. This approach is called design optimization. Examples of quantities to be minimized are energy consumption and construction materials. Items to be maximized are useful life and capacity such as the vehicle weight that can be supported by a bridge. In this project, we consider the problem of minimizing the material cost associated with building a water tank. The water tank consists of a cylindrical part of radius r and height h, and a hemispherical top. The tank is to be constructed to hold 500 meter cubed when filled. The surface area of the cylindrical part is 2*pi*rh, and its volume is pi*r^2. The surface area of the hemispherical top is given by 2*pi*r^2, and is volume is given by 2*pi*r^3/3. The cost to construct the cylindrical part of the tank is $300 per square meter of surface area;the hemispherical part costs $400 per square meter. Use the fminbnd function to compute the radius that results in the least cost. Compute the corresponding height h.
I got the right answer but it is very chaotic. I created bunch of function. I wonder if I can created one function?... let's name it ONEFUN
function R = findR(x)
h = (1500-2.*pi*x.^3)./(3.*pi.*x.^2);
R = 2.*pi.*x.*(h) + 2.*pi.*x.^2+pi.*x.^2;
function H = findH(x)
H = (1500-2.*pi*x.^3)./(3.*pi.*x.^2);
function [Cc, Chs, Tc] = Costs(r,h) % Cc - Cost of Cylinder, Chs - Cost of Hemishpere,
%Tc - Total Cost
Cc = ((2.*pi.*r.*h) + (pi.*r.^2)).*300;
Chs = (2.*pi.*r.^2).*400;
Tc = Cc+Chc;
I thought of using switch, response but I have no idea how to do it.
function Anwsers
response = input('Type "find r", "find h", "costHS", "costC", "total": ','s');
response = lower(response);
switch response
case 'find r'
Radius = fminsearch(@ONEFUN, [1]);
case 'find h'
Hight = findH(r)
case 'costHS'
case 'costC'
case 'total'
otherwise
disp('You have not entered a proper choice.')
end
I would appreciate and help
Upvotes: 3
Views: 1980
Reputation: 69242
This is a typical problem of minimizing a function with constraints. That is, you want to minimize the Cost(R,H)
, while keeping the Volume(R,H)
fixed, and you have a simple (two-variable) equation for each of these.
For this you could use the matlab function fmincon.
The above is the most direct computational approach, but there are other ways to solve it using various degrees of incorporating the constraint into the solution analytically. You could, for example, do a full analytic solution, or solve the Volume
equation for H
, and then put this into to the Cost
equation (ie, Cost(R,H)->Cost(R)
) and then just minimize over R
, etc. The approach you used is within this partially analytic middle-ground, but it's a bit messier for it.
Upvotes: 0
Reputation: 20320
Doing it in one function is a bad idea. Lot's of simple function that do one thing each is good.
Most of the chaos from my point of view seems to be terse names, magic numbers, relying on operator precedence and duplication.
h = (1500- (2.*pi*x.^3)./(3.*pi.*x.^2));
for instance, I think ...
Why aren't you using the function of the same name? same code twice.
Where in Cthulhu's name do the numbers 1500, 300 and 400 come from?
Never been keen on single character function names myself, but that might be my lack of familiarity with expressing a problem mathematically.
Upvotes: 1