80 likes | 159 Views
MATLAB Numerical Basics. Roots of Polynominals. MATLAB can find all roots (both real and imaginary) of polynominals.
E N D
Roots of Polynominals • MATLAB can find all roots (both real and imaginary) of polynominals. Store coefficients in a vector v = [ a1 a2 … an an+1 ]. Use roots builtin function with vector as argument. Return value is a vector containing all n roots of the polynominal
>> roots( [ 1 0 -17 12 ] ) ans = -4.4389 3.7102 0.7286
Roots of Functions • MATLAB can find a single root for any function for which a MATLAB function file can be written. One of the builtin methods for doing this is fzero. fzero( @fname , approx_root ) fname is the name of the function for which a root is to be located. It can have only one argument, the function’s variable. approx_root is an initial guess for the value of the root.
Van der Waals Equation P = Pressure ( 10 atm ) T = Temperature ( 250°K ) R = Gas Constant ( 0.082 liter-atm/gmole-°K ) V = Specific Volume ( liter/gmole ) Ammonia a = 4.19 atm (liter/gmole)2 b = 0.0372 liter/gmole
Minimums • MATLAB has builtin procedures to find minimums in functions. This is an important tool in optimizations. One such tool is fminbnd. It finds the minimum of a MATLAB function between two bounds. fminbnd( @fname , x1 , x2 ) fname is the name of the MATLAB function to be optimized. This function has one argument, the independent variable, and must be written to accept vector arguments. x1 is the lower bound of the minimum x2 is the upper bound of the minimum
Minimize x3 -17x + 12 between 0 and 10 m-file simple.m: function y = simple( x ) y = x.^3 -17*x + 12; >> fminbnd(@simple , 0 , 10) ans = 2.3805
Global Variables • Normally variables in functions are local only to that function. • Many times one would like to define constants to be used in several functions or be able to change the value of a variable in a function without editing the function. This would be useful for instance in changing the temperature or pressure in a VDW calculation. • The global statement in MATLAB allows variables to be shared. • The global statement must be used in each MATALB area where the sharing is to occur. • The global statement must be executed before the variable is used. Gilat, Section 6.3