THE MATLAB ENVIRONMENT ASSIGNMENT
2.2 ASSIGNMENT
Assignment refers to assigning values to variable names. This results in the storage of the values in the memory location corresponding to the variable name.2.2.1 Scalars
The assignment of values to scalar variables is similar to other computer languages. Try typing a = 4 Note how the assignment echo prints to confirm what you have done: a = 4 Echo printing is a characteristic of MATLAB. It can be suppressed by terminating the com- mand line with the semicolon ; character. Try typing A = 6; You can type several commands on the same line by separating them with commas or semicolons. If you separate them with commas, they will be displayed, and if you use the semicolon, they will not. For example, a = 4,A = 6;x = 1; a = 4 MATLAB treats names in a case-sensitive manner—that is, the variable a is not the same as A . To illustrate this, enter a and then enter A See how their values are distinct. They are distinct names. 2.2 ASSIGNMENT 27 We can assign complex values to variables, since MATLAB handles complex arith- metic automatically. The unit imaginary number √ −1 is preassigned to the variable i . Consequently, a complex value can be assigned simply as in x = 2+i4 x = 2.0000 + 4.0000i It should be noted that MATLAB allows the symbol j to be used to represent the unit imag- inary number for input. However, it always uses an i for display. For example, x = 2+j4 x = 2.0000 + 4.0000i There are several predefined variables, for example, pi . pi ans = 3.1416 Notice how MATLAB displays four decimal places. If you desire additional precision, enter the following: format long Now when pi is entered the result is displayed to 15 significant figures: pi ans = 3.14159265358979 To return to the four decimal version, type format short The following is a summary of the format commands you will employ routinely in engi- neering and scientific calculations. They all have the syntax: format type . type Result Example short Scaled fixed-point format with 5 digits 3.1416 long Scaled fixed-point format with 15 digits for double and 7 digits for single 3.14159265358979 short e Floating-point format with 5 digits 3.1416e+000 long e Floating-point format with 15 digits for double and 7 digits for single 3.141592653589793e+000 short g Best of fixed- or floating-point format with 5 digits 3.1416 long g Best of fixed- or floating-point format with 15 digits for double 3.14159265358979 and 7 digits for single short eng Engineering format with at least 5 digits and a power that is a multiple of 3 3.1416e+000 long eng Engineering format with exactly 16 significant digits and a power 3.14159265358979e+000 that is a multiple of 3 bank Fixed dollars and cents 3.142.2.2 Arrays, Vectors and Matrices
An array is a collection of values that are represented by a single variable name. One- dimensional arrays are called vectors and two-dimensional arrays are called matrices. The scalars used in Section 2.2.1 are actually matrices with one row and one column. Brackets are used to enter arrays in the command mode. For example, a row vector can be assigned as follows: a = [1 2 3 4 5] a = 1 2 3 4 5 Note that this assignment overrides the previous assignment of a = 4 . In practice, row vectors are rarely used to solve mathematical problems. When we speak of vectors, we usually refer to column vectors, which are more commonly used. A column vector can be entered in several ways. Try them. b = [2;4;6;8;10] or b = [2 4 6 8 10] or, by transposing a row vector with the operator, b = [2 4 6 8 10] The result in all three cases will be b = 2 4 6 8 10 A matrix of values can be assigned as follows: A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 In addition, the Enter key carriage return can be used to separate the rows. For example, in the following case, the Enter key would be struck after the 3, the 6 and the ] to assign the matrix: A = [1 2 3 4 5 6 7 8 9] 2.2 ASSIGNMENT 29 Finally, we could construct the same matrix by concatenating i.e., joining the vectors representing each column: A = [[1 4 7] [2 5 8] [3 6 9]] At any point in a session, a list of all current variables can be obtained by entering the who command: who Your variables are: A a ans b x or, with more detail, enter the whos command: whos Name Size Bytes Class A 3x3 72 double array a 1x5 40 double array ans 1x1 8 double array b 5x1 40 double array x 1x1 16 double array complex Grand total is 21 elements using 176 bytes Note that subscript notation can be used to access an individual element of an array. For example, the fourth element of the column vector b can be displayed as b4 ans = 8 For an array, Am,n selects the element in m th row and the n th column. For example, A2,3 ans = 6 There are several built-in functions that can be used to create matrices. For example, the ones and zeros functions create vectors or matrices filled with ones and zeros, respectively. Both have two arguments, the first for the number of rows and the second for the number of columns. For example, to create a 2 × 3 matrix of zeros: E = zeros2,3 E = 0 0 0 0 0 0 Similarly, the ones function can be used to create a row vector of ones: u = ones1,3 u = 1 1 12.2.3 The Colon Operator
The colon operator is a powerful tool for creating and manipulating arrays. If a colon is used to separate two numbers, MATLAB generates the numbers between them using an increment of one: t = 1:5 t = 1 2 3 4 5 If colons are used to separate three numbers, MATLAB generates the numbers between the first and third numbers using an increment equal to the second number: t = 1:0.5:3 t = 1.0000 1.5000 2.0000 2.5000 3.0000 Note that negative increments can also be used t = 10:-1:5 t = 10 9 8 7 6 5 Aside from creating series of numbers, the colon can also be used as a wildcard to se- lect the individual rows and columns of a matrix. When a colon is used in place of a spe- cific subscript, the colon represents the entire row or column. For example, the second row of the matrix A can be selected as in A2,: ans = 4 5 6 We can also use the colon notation to selectively extract a series of elements from within an array. For example, based on the previous definition of the vector t : t2:4 ans = 9 8 7 Thus, the second through the fourth elements are returned.2.2.4 The
linspace and logspace Functions The linspace and logspace functions provide other handy tools to generate vectors of spaced points. The linspace function generates a row vector of equally spaced points. It has the form linspacex1, x2, n 2.2 ASSIGNMENT 31 which generates n points between x1 and x2 . For example linspace0,1,6 ans = 0 0.2000 0.4000 0.6000 0.8000 1.0000 If the n is omitted, the function automatically generates 100 points. The logspace function generates a row vector that is logarithmically equally spaced. It has the form logspacex1, x2, n which generates n logarithmically equally spaced points between decades 10 x1 and 10 x2 . For example, logspace-1,2,4 ans = 0.1000 1.0000 10.0000 100.0000 If n is omitted, it automatically generates 50 points.2.2.5 Character Strings
Aside from numbers, alphanumeric information or character strings can be represented by enclosing the strings within single quotation marks. For example, f = Miles ; s = Davis; Each character in a string is one element in an array. Thus, we can concatenate i.e., paste together strings as in x = [f s] x = Miles Davis Note that very long lines can be continued by placing an ellipsis three consecutive periods at the end of the line to be continued. For example, a row vector could be entered as a = [1 2 3 4 5 ... 6 7 8] a = 1 2 3 4 5 6 7 8 However, you cannot use an ellipsis within single quotes to continue a string. To enter a string that extends beyond a single line, piece together shorter strings as in quote = [Any fool can make a rule, ... and any fool will mind it] quote = Any fool can make a rule, and any fool will mind it These operators will work in calculator fashion. Try 2pi ans = 6.2832 Also, scalar real variables can be included: y = pi4; y 2.45 ans = 0.5533 Results of calculations can be assigned to a variable, as in the next-to-last example, or sim- ply displayed, as in the last example. As with other computer calculation, the priority order can be overridden with paren- theses. For example, because exponentiation has higher priority then negation, the follow- ing result would be obtained: y = -4 2 y = -16 Thus, 4 is first squared and then negated. Parentheses can be used to override the priorities as in y = -4 2 y = 162.3 MATHEMATICAL OPERATIONS
Operations with scalar quantities are handled in a straightforward manner, similar to other computer languages. The common operators, in order of priority, are Exponentiation – Negation Multiplication and division \ Left division 2 + – Addition and subtraction 2 Left division applies to matrix algebra. It will be discussed in detail later in this book. 2.3 MATHEMATICAL OPERATIONS 33Parts
» Applied Numerical Methods with MATLAB fo
» Motivation 1 1.2 Part Organization 2 Overview 123 2.2 Part Organization 124
» Overview 205 3.2 Part Organization 207 Overview 321 4.2 Part Organization 323
» Overview 459 5.2 Part Organization 460 Applied Numerical Methods with MATLAB fo
» New Chapters. Overview 547 6.2 Part Organization 551
» New Content. Overview 547 6.2 Part Organization 551
» New Homework Problems. Overview 547 6.2 Part Organization 551
» Numerical methods greatly expand the
» Numerical methods allow you to use
» PART ORGANIZATION Applied Numerical Methods with MATLAB fo
» CONSERVATION LAWS IN ENGINEERING AND SCIENCE
» NUMERICAL METHODS COVERED IN THIS BOOK
» CASE STUDY IT’S A REAL DRAG CASE STUDY continued
» CASE STUDY continued Applied Numerical Methods with MATLAB fo
» Use calculus to verify that Eq. 1.9 is a solution of
» Use calculus to solve Eq. 1 for the case where the ini-
» The following information is available for a bank account:
» Rather than the nonlinear relationship of Eq. 1.7, you
» For the free-falling bungee jumper with linear drag
» For the second-order drag model Eq. 1.8, compute the
» The amount of a uniformly distributed radioactive con-
» A storage tank Fig. P contains a liquid at depth y
» For the same storage tank described in Prob. 1.9, sup-
» Apply the conservation of volume see Prob. 1.9 to sim-
» THE MATLAB ENVIRONMENT ASSIGNMENT
» MATHEMATICAL OPERATIONS Applied Numerical Methods with MATLAB fo
» GRAPHICS Applied Numerical Methods with MATLAB fo
» OTHER RESOURCES Applied Numerical Methods with MATLAB fo
» CASE STUDY EXPLORATORY DATA ANALYSIS CASE STUDY continued
» Manning’s equation can be used to compute the veloc-
» It is general practice in engineering and science that
» You contact the jumpers used to generate the data in
» Figure Pa shows a uniform beam subject to a lin-
» M-FILES Applied Numerical Methods with MATLAB fo
» INPUT-OUTPUT Applied Numerical Methods with MATLAB fo
» STRUCTURED PROGRAMMING Applied Numerical Methods with MATLAB fo
» NESTING AND INDENTATION Applied Numerical Methods with MATLAB fo
» PASSING FUNCTIONS TO M-FILES
» CASE STUDY BUNGEE JUMPER VELOCITY CASE STUDY continued
» Economic formulas are available to compute annual
» Two distances are required to specify the location of a
» Develop an M-file to determine polar coordinates as
» Develop an M-file function that is passed a numeric
» Manning’s equation can be used to compute the velocity
» The volume V of liquid in a hollow horizontal cylinder of
» Develop a vectorized version of the following code:
» Based on Example 3.6, develop a script to produce an
» ERRORS Applied Numerical Methods with MATLAB fo
» Digital computers have magnitude and precision limits on their ability to represent
» Arithmetic Manipulations of Computer Numbers
» TRUNCATION ERRORS Applied Numerical Methods with MATLAB fo
» TOTAL NUMERICAL ERROR Applied Numerical Methods with MATLAB fo
» BLUNDERS, MODEL ERRORS, AND DATA UNCERTAINTY
» a Applied Numerical Methods with MATLAB fo
» OVERVIEW Applied Numerical Methods with MATLAB fo
» ROOTS IN ENGINEERING AND SCIENCE
» GRAPHICAL METHODS Applied Numerical Methods with MATLAB fo
» BRACKETING METHODS AND INITIAL GUESSES
» BISECTION Applied Numerical Methods with MATLAB fo
» FALSE POSITION Applied Numerical Methods with MATLAB fo
» CASE STUDY GREENHOUSE GASES AND RAINWATER CASE STUDY continued
» Locate the first nontrivial root of sinx = x
» Determine the positive real root of lnx
» A total charge Q is uniformly distributed around a ring-
» For fluid flow in pipes, friction is described by a di-
» Perform the same computation as in Prob. 5.22, but for
» SIMPLE FIXED-POINT ITERATION Applied Numerical Methods with MATLAB fo
» NEWTON-RAPHSON Applied Numerical Methods with MATLAB fo
» SECANT METHODS Applied Numerical Methods with MATLAB fo
» BRENT’S METHOD Applied Numerical Methods with MATLAB fo
» MATLAB FUNCTION: Applied Numerical Methods with MATLAB fo
» POLYNOMIALS Applied Numerical Methods with MATLAB fo
» CASE STUDY PIPE FRICTION CASE STUDY continued
» CASE STUDY continued CASE STUDY continued
» Use a fixed-point iteration and b the Newton-
» Use a the Newton-Raphson method and b the modi-
» Develop an M-file for the secant method. Along with
» Develop an M-file for the modified secant method.
» Differentiate Eq. E6.4.1 to get Eq. E6.4.2. a
» Perform the identical MATLAB operations as those
» In control systems analysis, transfer functions are
» Use the Newton-Raphson method to find the root of Given a
» INTRODUCTION AND BACKGROUND Applied Numerical Methods with MATLAB fo
» MULTIDIMENSIONAL OPTIMIZATION Applied Numerical Methods with MATLAB fo
» CASE STUDY EQUILIBRIUM AND MINIMUM POTENTIAL ENERGY
» Employ the following methods to find the minimum of
» Consider the following function:
» Develop a single script to a generate contour and
» The head of a groundwater aquifer is described in
» Recent interest in competitive and recreational cycling
» The normal distribution is a bell-shaped curve defined by
» Use the Applied Numerical Methods with MATLAB fo
» Given the following function:
» The specific growth rate of a yeast that produces an
» A compound A will be converted into B in a stirred
» Develop an M-file to locate a minimum with the
» Develop an M-file to implement parabolic interpola-
» Pressure measurements are taken at certain points
» The trajectory of a ball can be computed with
» The deflection of a uniform beam subject to a linearly
» The torque transmitted to an induction motor is a func-
» The length of the longest ladder that can negotiate
» MATRIX ALGEBRA OVERVIEW Applied Numerical Methods with MATLAB fo
» SOLVING LINEAR ALGEBRAIC EQUATIONS WITH MATLAB
» CASE STUDY CURRENTS AND VOLTAGES IN CIRCUITS CASE STUDY continued
» Five reactors linked by pipes are shown in Fig. P.
» SOLVING SMALL NUMBERS OF EQUATIONS
» MATLAB M-file: Operation Counting
» PIVOTING Applied Numerical Methods with MATLAB fo
» TRIDIAGONAL SYSTEMS Applied Numerical Methods with MATLAB fo
» CASE STUDY MODEL OF A HEATED ROD CASE STUDY continued
» Given the system of equations
» Given the equations Applied Numerical Methods with MATLAB fo
» An electrical engineer supervises the production of three
» Develop an M-file function based on Fig. 9.5 to im-
» GAUSS ELIMINATION AS LU FACTORIZATION
» CHOLESKY FACTORIZATION Applied Numerical Methods with MATLAB fo
» MATLAB LEFT DIVISION Applied Numerical Methods with MATLAB fo
» Use Cholesky factorization to determine [U] so that THE MATRIX INVERSE
» Norms and Condition Number in MATLAB
» CASE STUDY INDOOR AIR POLLUTION CASE STUDY continued
» Determine the matrix inverse for the system described
» Determine A Applied Numerical Methods with MATLAB fo
» Determine the Frobenius and row-sum norms for the
» Use MATLAB to determine the spectral condition num-
» Besides the Hilbert matrix, there are other matrices
» Use MATLAB to determine the spectral condition
» Repeat Prob. 11.10, but for the case of a six-
» The Lower Colorado River consists of a series of four a
» A chemical constituent flows between three reactors a
» LINEAR SYSTEMS: GAUSS-SEIDEL Applied Numerical Methods with MATLAB fo
» NONLINEAR SYSTEMS Applied Numerical Methods with MATLAB fo
» CASE STUDY CHEMICAL REACTIONS
» Use the Gauss-Seidel method to solve the following
» Repeat Prob. 12.3 but use Jacobi iteration.
» The following system of equations is designed to de-
» Solve the following system using three iterations with a
» MATHEMATICAL BACKGROUND Applied Numerical Methods with MATLAB fo
» PHYSICAL BACKGROUND Applied Numerical Methods with MATLAB fo
» THE POWER METHOD Applied Numerical Methods with MATLAB fo
» CASE STUDY EIGENVALUES AND EARTHQUAKES CASE STUDY continued
» Repeat Example but for three masses with the m’s =
» Use the power method to determine the highest eigen-
» Use the power method to determine the lowest eigen-
» Derive the set of differential equations for a three
» Consider the mass-spring system in Fig. P. The fre-
» STATISTICS REVIEW Applied Numerical Methods with MATLAB fo
» RANDOM NUMBERS AND SIMULATION
» LINEAR LEAST-SQUARES REGRESSION Applied Numerical Methods with MATLAB fo
» LINEARIZATION OF NONLINEAR RELATIONSHIPS
» COMPUTER APPLICATIONS Applied Numerical Methods with MATLAB fo
» CASE STUDY continued CASE STUDY continued CASE STUDY continued
» Using the same approach as was employed to derive
» Beyond the examples in Fig. 14.13, there are other
» The concentration of E. coli bacteria in a swimming
» Rather than using the base-e exponential model
» Determine an equation to predict metabolism rate as a
» On average, the surface area A of human beings is
» 2.12 2.15 2.20 2.22 2.23 2.26 2.30 Applied Numerical Methods with MATLAB fo
» An investigator has reported the data tabulated below
» Develop an M-file function to compute descriptive
» Modify the Applied Numerical Methods with MATLAB fo
» Develop an M-file function to fit a power model.
» Below are data taken from a batch reactor of bacterial
» A transportation engineering study was conducted to
» In water-resources engineering, the sizing of reser-
» Perform the same computation as in Example 14.3,
» POLYNOMIAL REGRESSION Applied Numerical Methods with MATLAB fo
» MULTIPLE LINEAR REGRESSION Applied Numerical Methods with MATLAB fo
» GENERAL LINEAR LEAST SQUARES
» QR FACTORIZATION AND THE BACKSLASH OPERATOR
» NONLINEAR REGRESSION Applied Numerical Methods with MATLAB fo
» CASE STUDY FITTING EXPERIMENTAL DATA CASE STUDY continued
» Fit a parabola to the data from Table 14.1. Determine
» Fit a cubic polynomial to the following data:
» Use multiple linear regression to derive a predictive
» As compared with the models from Probs. 15.5 and
» Use multiple linear regression to fit
» The following data were collected for the steady flow
» In Prob. 14.8 we used transformations to linearize
» Enzymatic reactions are used extensively to charac-
» The following data represent the bacterial growth in a
» Dynamic viscosity of water μ10
» Use the following set of pressure-volume data to find
» Environmental scientists and engineers dealing with
» It is known that the data tabulated below can be mod-
» hr Applied Numerical Methods with MATLAB fo
» CURVE FITTING WITH SINUSOIDAL FUNCTIONS
» CONTINUOUS FOURIER SERIES Applied Numerical Methods with MATLAB fo
» FOURIER INTEGRAL AND TRANSFORM
» DISCRETE FOURIER TRANSFORM DFT
» THE POWER SPECTRUM Applied Numerical Methods with MATLAB fo
» CASE STUDY SUNSPOTS Applied Numerical Methods with MATLAB fo
» The pH in a reactor varies sinusoidally over the course
» The solar radiation for Tucson, Arizona, has been tab-
» Use MATLAB to generate 64 points from the function
» INTRODUCTION TO INTERPOLATION Applied Numerical Methods with MATLAB fo
» NEWTON INTERPOLATING POLYNOMIAL Applied Numerical Methods with MATLAB fo
» LAGRANGE INTERPOLATING POLYNOMIAL Applied Numerical Methods with MATLAB fo
» INVERSE INTERPOLATION Applied Numerical Methods with MATLAB fo
» EXTRAPOLATION AND OSCILLATIONS Applied Numerical Methods with MATLAB fo
» Given the data Applied Numerical Methods with MATLAB fo
» Use the portion of the given steam table for super-
» The following data for the density of nitrogen gas ver-
» Ohm’s law states that the voltage drop V across an
» The acceleration due to gravity at an altitude y above
» INTRODUCTION TO SPLINES Applied Numerical Methods with MATLAB fo
» LINEAR SPLINES Applied Numerical Methods with MATLAB fo
» CUBIC SPLINES Applied Numerical Methods with MATLAB fo
» PIECEWISE INTERPOLATION IN MATLAB
» MULTIDIMENSIONAL INTERPOLATION Applied Numerical Methods with MATLAB fo
» CASE STUDY HEAT TRANSFER CASE STUDY continued
» A reactor is thermally stratified as in the following
» The following is the built-in
» Develop a plot of a cubic spline fit of the following
» The following data define the sea-level concentra- a
» NEWTON-COTES FORMULAS Applied Numerical Methods with MATLAB fo
» THE TRAPEZOIDAL RULE Applied Numerical Methods with MATLAB fo
» SIMPSON’S RULES Applied Numerical Methods with MATLAB fo
» HIGHER-ORDER NEWTON-COTES FORMULAS Applied Numerical Methods with MATLAB fo
» INTEGRATION WITH UNEQUAL SEGMENTS
» OPEN METHODS MULTIPLE INTEGRALS
» CASE STUDY COMPUTING WORK WITH NUMERICAL INTEGRATION CASE STUDY continued CASE STUDY continued
» Derive Eq. 19.4 by integrating Eq. 19.3. Evaluate the following integral:
» INTRODUCTION Applied Numerical Methods with MATLAB fo
» ROMBERG INTEGRATION Applied Numerical Methods with MATLAB fo
» GAUSS QUADRATURE Applied Numerical Methods with MATLAB fo
» MATLAB Functions: If the error is larger than the tolerance,
» CASE STUDY ROOT-MEAN-SQUARE CURRENT CASE STUDY continued
» Evaluate the following integral a analytically,
» Evaluate the following integral with a Romberg inte-
» Evaluate the double integral Compute work as described in Sec. 19.9, but use the
» Suppose that the current through a resistor is de-
» km 1100 1500 2450 3400 3630 4500 km 5380 6060 6280 6380 Applied Numerical Methods with MATLAB fo
» HIGH-ACCURACY DIFFERENTIATION FORMULAS Applied Numerical Methods with MATLAB fo
» RICHARDSON EXTRAPOLATION Applied Numerical Methods with MATLAB fo
» DERIVATIVES OF UNEQUALLY SPACED DATA
» DERIVATIVES AND INTEGRALS FOR DATA WITH ERRORS
» PARTIAL DERIVATIVES Applied Numerical Methods with MATLAB fo
» NUMERICAL DIFFERENTIATION WITH MATLAB
» CASE STUDY VISUALIZING FIELDS CASE STUDY continued
» Develop an M-file to obtain first-derivative estimates
» Develop an M-file function that computes first and
» A jet fighter’s position on an aircraft carrier’s runway
» Use the following data to find the velocity and accel-
» A plane is being tracked by radar, and data are taken
» Use regression to estimate the acceleration at each
» The normal distribution is defined as
» The following data were generated from the normal Use the
» The objective of this problem is to compare second-
» The pressure gradient for laminar flow through a con-
» The following data for the specific heat of benzene
» The specific heat at constant pressure c
» OVERVIEW IMPROVEMENTS OF EULER’S METHOD
» RUNGE-KUTTA METHODS Applied Numerical Methods with MATLAB fo
» SYSTEMS OF EQUATIONS Applied Numerical Methods with MATLAB fo
» CASE STUDY PREDATOR-PREY MODELS AND CHAOS
» Develop an M-file to solve a single ODE with Heun’s
» Develop an M-file to solve a single ODE with the
» Develop an M-file to solve a system of ODEs with
» Isle Royale National Park is a 210-square-mile archi-
» The motion of a damped spring-mass system
» Perform the same simulations as in Section 22.6 for ADAPTIVE RUNGE-KUTTA METHODS
» MULTISTEP METHODS Applied Numerical Methods with MATLAB fo
» STIFFNESS Applied Numerical Methods with MATLAB fo
» MATLAB APPLICATION: BUNGEE JUMPER WITH CORD
» CASE STUDY PLINY’S INTERMITTENT FOUNTAIN CASE STUDY continued
» Given Applied Numerical Methods with MATLAB fo
» THE SHOOTING METHOD Applied Numerical Methods with MATLAB fo
» FINITE-DIFFERENCE METHODS Applied Numerical Methods with MATLAB fo
» A cable is hanging from two supports at A and B
» In Prob. 24.16, the basic differential equation of the
Show more