Professional Documents
Culture Documents
BITS Pilani
Hyderabad y Campus p
BITS Pilani
Hyderabad Campus
MATLAB
A flagship software which was originally developed as a matrix library. A variety of numerical functions, symbolic computations, and visualization tools have been added to the matrix manipulations. MATLAB is a mathematical and graphical software package; it has numerical, graphical, and programming capabilities. It is a interactive tools. It consists of built-in functions to de many operations, and Toolboxes for specific operations such as, Curve-fitting Curve fitting Partial differential equations Optimization Linear System Analysis Control tool box
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus P-3
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-4
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-5
demo has demos of several options in MATLAB help will explain any command; help help will explain how help works helpbrowser opens a Help Window
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-6
Variable names
The name must begin with a letter of the alphabet. After that, the name can contain letters, digits, and the underscore character (e.g., value_1), but it cannot have a space. There is a limit to the length of the name; the built-in function namelengthmax tells how many characters this is. MATLAB is case-sensitive. That means that there is a difference between upper- and lowercase letters. So, variables called mynum, MYNUM, and Mynum are all different. different There are certain words called reserved words that cannot be used as variable names. Names of built-in functions can, but should not, be used as variable names. Variable names should always be mnemonics, means they should make some sense to be realistic representation.
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus P-8
Operators
Herearesomeofthecommon operatorsthatcanbeusedwith numericexpressions: +addition negation, negation subtraction *multiplication /division(dividedbye.g.10/5is2) \ division(dividedintoe e.g. g 5\10is2) ^exponentiation(e.g.,5^2is25)
Built-in Functions
Built-in functions
function(argument) sqrt(x) exp(x) abs(x) log(x) g( ) log10(x) factorial(x)
TRIGONEMETRIC
Rounding functions
Display formats
format short f h : 41.4286 format long: 41.42857142857143 f format t short h t e: 4.1429e+001 4 1429 +001 format long e: 4.142857142857143e+0001 f format t short h t g: 41.429 41 429 format long g: 41.4285714285714 format bank: 41 41.43 43 format compact: eliminates
empty lines
rem(x,y) returns the remainder after xis divided by y (similarto %function inC)
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-12
Built-in Functions
Try these functions >> abs(4) >> rem(13, ( , 5) ) >> sign(5) ans = 1 >> sign(3) ans = 1
Constants
pi i j inf NaN 3.14159. 1 1 infinity stands for not not a number; number ; e.g., e g the result of 0/0
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus P-13
Types
Integers int8, int16, int32, and int64 >> intmin(int8) ans = 128 >> intmax(int8) ans = 127
Float & Double B d By default, f l numbers b are stored d as the h type double d bl i in MATLAB. MATLAB Th There are, however, h many functions that convert values from one type to another. >> val = 6+3 >> vali = int32(val); ( ); >> whos Name Size Bytes Class Attributes val 1x1 8 double vali li 1 1 1x1 4 i t32 int32
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-14
Random Numbers
How to generate random numbers?
The function rand can be used to generate the real numbers >> rand ans = 0.9501 >> rand ans = 0 2311 0.2311 Rand only generates fractions between 0 and 1. How to generate random numbers between 0 to 10? >> low = 0; >> high = 10; >> rand*(highlow)+low and (high low) low
Randomnumbergeneratorsorfunctions arenottrulyrandom.processstartswith onenumber,calledaseed.Theinitialseed iseitherapredeterminedvalueoritis obtainedfromthebuiltinclockinthe computer.Then,basedonthisseed,a processdeterminesthenextrandom number.Theseareactuallycalledpseudo random;theyarenottrulyrandombecause thereisaprocessthatdeterminesthenext valueeachtime.
How to generate random integers Ho between 0 to 10? >> randi(IMAX,M,N) returns an M-by-N matrix containing pseudorandom integer values drawn from the discrete uniform distribution on 1:IMAX Try randi(100 1:IMAX. randi(100, 10, 10 2)
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-15
P-16
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
1 1 1 1 1 1 1
P-19
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
Summary
Common Pitfalls
Following are examples of very common errors: Putting g a space p in a variable name Confusing the format of an assignment statement as expression = variablename rather than variablename = expression The variable name must always be on the left. Using a built-in function name as a variable name, and then trying to use the function Confusing the two division operators / and \ Forgetting the operator precedence rules Confusing the order of arguments passed to functions, for example, to find the remainder of dividing 3 into 10 using rem(3,10) instead of rem(10,3) Not using different types of arguments when testing functions Attempting to create a matrix that does not have the same number of values on each row Forgetting to use parentheses to pass an argument to a function; for example, fix 2.3 instead of fix(2.3). MATLAB returns the ASCII equivalent for each character when h this hi mistake i k is i made. d (What ( h happens h is i that h is i that h it i is i interpreted i d as the function of a string; for example, fix(2.3)).
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus P-21
Summary
Programming Style Guidelines
Do not use names of built-in functions as variable names. If different sets of random numbers are desired, set the seed for the rand function. Do not use just a single index when referring to elements in a matrix; instead, use both the row and column indices. To be general, never assume that the dimensions of any array (vector or matrix) are k known. Instead, d use the h function f i length to determine the number of elements in a vector, and the function size for a matrix, for example: len = length(vec); [r c] = size(mat);
CHE F242 Numerical Methods for Chemical Engineers, BITS, Pilani-Hyderabad Campus
P-22