%
% - File name : nonlin_multivar_1.m (Generated by Hee Jung : fall/1999)
% - Solve nonlinear equations of several variables (example 1-2 of Norton text (p.29-31))
% - Problem to solve: find the height and empty weight of an open-top, cylindrical
% container for a desired volume (see the text for details of the problem setup)
% - Method 1 : use MATLAB's symbolic solve command.
% - Source of help on this solve command can be found in several ways:
% 1) Pratap's MATLAB 5 book
% 2) type help solve in matlab command window
% 3) type helpdesk in matlab command window -> click on 'symbolic math toolbox ref.'
% in the web pages -> click the 'solve' function
%
clear % removes all variables from the workspace
%
% Define the input variables (make sure the consistency of units)
%
volume = 1000; % volume in cm^3
D = 12; % diameter in cm
thickness = 0.1; % wall thickness in cm
density = 7.75; % density of material in g/cc
%
% Solve the four equations(eq1,eq2,eq3,eq4) with four unknowns(var1,var2,var3,var4)
% syntax of 'solve' function : solve('eq1','eq2','eq3','eq4','var1,var2,var3,var4')
% where unknown variables are basearea, H, surface, and weight
%
% Define equations and unknown variables (following equations are from text)
eq1 = 'volume = basearea*H';
eq2 = 'basearea = pi*(D/2)^2';
eq3 = 'surface = basearea + pi*D*H';
eq4 = 'weight = surface*thickness*density';
vars = 'basearea,H,surface,weight';
% Solve the problem with solve command
A= solve(eq1,eq2,eq3,eq4,vars)
% The answer MATLAB gives for this command is more or less useless. it is
% A = H: [1x1 sym], basearea: [1x1 sym], surface: [1x1 sym], weight: [1x1 sym]
% To get useful answer, you type:
A.H, A.basearea, A.surface, A.weight
% The output of the above command is :
% 4*volume/pi/D^2 (=H), 1/4*pi*D^2 (=basearea),
% 1/4*(pi*D^3+16*volume)/D (=surface)
% 1/4*(pi*D^3+16*volume)/D*thickness*density (=weight)
% The next step is to get numbers instead of just formulas. Do this with eval command
% you type :
H = eval(A.H)
basearea = eval(A.basearea)
surface = eval(A.surface)
weight = eval(A.weight)
% you'll get
% H = 8.8419 (cm), basearea = 113.0973 (cm^2),
% surface = 446.4307 (cm^2), weight = 345.9838 (g)
% If you get multiple solutions, you should judge which solution(s) is/are reasonable.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% < Note >
% 1. The order of equations & unknown variables in using 'solve'
% doesn't matter.
% 2. If you don't include unknown variable names in using 'solve',
% you'll get the following message.
% 'Explicit solution could not be found'
% 3. If you try
% [output1,output2] = solve('eq1','eq2','var1,var2'),
% the output list is all out of order, so don't use this syntax.
% 4. If you give space betwen variable names(e.g. 'basearea, H, surface, weight'),
% you will get error messages.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%