Introduction to Matlab for engineers solutions manual pdf

matlab and simulink for engineers tyagi pdf and matlab for chemical engineers pdf
NancyWest Profile Pic
NancyWest,Germany,Professional
Published Date:12-07-2017
Your Website URL(Optional)
Comment
Introductionto Matlabfor Engineers Are Mjaavatten Bernt Lie August, 2005iiContents Preface xiii I Analysis of technical data with Matlab 1 1 The Matlab workplace 3 2 Arrays: Matlab’s basic data structure 5 2.1 Overview of learning goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.2 Arrays and data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.3 Assignment: Naming data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.4 Naming data structures: some technicalities . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.5 Built-in constants in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.6 Basic creation of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.6.1 Dimension of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.6.2 Element-wise assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.6.3 Assigning row arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.6.4 Assigning column arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.6.5 Assigning two-dimensional arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.6.6 Line continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.6.7 One-dimensional addressing of array elements . . . . . . . . . . . . . . . . . . . . 13 2.7 Exporting and importing data from …les . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.7.1 Saving and loading data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.7.2 File import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.8 Functions and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.9 Numeric format and accuracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.10 Functions for creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.10.1 Creating arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.10.2 Size of arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.10.3 Rearranging arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.11 Subarrays and superarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.11.1 Picking subarrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.11.2 Building superarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.12 Housekeeping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.12.1 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.12.2 Matlab commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.12.3 OS commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.13 Basic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.13.1 Overview of operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.13.2 Trigonometric functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.13.3 Exponential functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.13.4 Complex functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.13.5 Rounding and remainder functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.13.6 Number theoretic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.14 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 iiiiv CONTENTS 3 Basic plotting in Matlab 43 3.1 Overview of learning goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.2 Basic two dimensional plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.2.1 The plot function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.2.2 Editing plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.2.3 Multiple plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.2.4 Editing axes properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.2.5 Command line plot editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.2.6 Fancy typesetting in plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.2.7 Closing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.3 Improving plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.4 Array plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.5 Presentation of experimental data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.5.1 Case study: distillation of water and ethanol . . . . . . . . . . . . . . . . . . . . . 52 3.5.2 Plotting data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.6 Further study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4 Simple data analysis 57 4.1 Overview of learning goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2.2 Polynomial interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4.2.3 Other interpolation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.2.4 Comparison of interpolation models . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.3.1 Polynomials in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.3.2 Operations on polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 4.3.3 Analysis of polynomial models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4.4 More on interpolation and prediction models . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.4.1 Case study: Saturated steam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.4.2 Multivariable interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 5 Introduction to automation of tasks 77 5.1 Overview of learning goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.2 Strings and basic string operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 5.3 Motivating examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 5.4 The basic repetition statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.5 Scripts and the Matlab editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.6 Examples: Repetition and Sums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 5.7 Example: Basic repetition over array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 6 Problems 87 II Exploiting the power of Matlab 91 7 Revisiting Part I 93 8 More program ‡ow control 95 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.2 Logical variables in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.3 Relations: functions and operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 8.4 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 8.5 Making logical choices: the if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 8.5.1 Motivating example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 8.5.2 Structure of if statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 8.5.3 Example: use of if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 8.6 Logical choices: the switch statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 8.7 Repetition loop: the while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100CONTENTS v 8.7.1 Motivating example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 8.7.2 Structure of while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 8.7.3 Example: use of while statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 8.7.4 Breaking out from for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 9 Writing your own functions 103 9.1 Example: Distance to horizon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 9.1.1 Basic case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 9.1.2 Returning more than one result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 9.1.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 9.2 Workspaces and variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 9.3 Loops vs. matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 10 Advanced use of the Matlab editor 109 10.1 Errors and the debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 10.2 De-linting the code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 10.3 Structuring the …le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 11 Data structures in Matlab 115 11.1 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 11.2 Cell arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 12 Handle graphics 117 13 Problems 121 III Becoming a Matlab Master 123 14 The once and future Matlab Master 125 14.1 Revisiting Part II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 14.2 Overview of new material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 15 Functions and function handles 127 15.1 Function handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 15.2 Function functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 15.3 Anonymous functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 15.4 Function functions: motivating example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 15.4.1 Implementation of Newton method . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 15.4.2 Generalizing the Newton method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 15.4.3 Some …nal thoughts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 15.5 Function handles as function arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 15.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 16 Simulation of dynamic systems 141 16.1 Solution of ODEs using Euler integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 16.2 Generalizing our Euler solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 16.3 ODE solvers in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 16.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 IV Closing 149 17 Conclusions 151vi CONTENTS V Appendices 153 A Arrays vs. vectors and matrices 155 A.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 A.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 B Manipulation of Arrays and Array Functions 159 B.1 Indexing arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 B.2 Building superarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 B.2.1 Toeplitz and Hankel arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 B.2.2 Kronecker product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 B.2.3 Block diagonal arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 C Hexadecimal numbers 165 D Three Dimensional Plots and Plot Housekeeping 167 D.1 Three dimensional plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 D.2 Housekeeping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 E Automation and String Operations 173 E.1 Motivating example: generating sequence of plots . . . . . . . . . . . . . . . . . . . . . . . 173 E.2 Strings and string operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 E.3 Example: automatic generation of …gures . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 E.4 Example: hex 2 ‡oating point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 F Numerical methods with Matlab 181 F.1 Numerical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 F.2 Arrays vs. Vectors and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 F.3 Matrix operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 F.3.1 Basic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 1 F.3.2 Matrix factorizations/decompositions . . . . . . . . . . . . . . . . . . . . . . . . . 183 2 F.3.3 Vector space commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 G Extra parameters and anonymous functions 185 H DAE solvers in Matlab 187 VI References 191 Bibliography 193 Index 195 1 These methods are used extensively in linear algebra. 2 These methods are used extensively in advanced linear algebra.List of Figures 1.1 The opening window in Matlab 6.5. It looks almost the same in Matlab 7.0. . . . . . . . . 4 1.2 The Matlab window after entering a command. . . . . . . . . . . . . . . . . . . . . . . . . 4 2.1 The result after loading a .mat …le. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.2 The content of the ASCII …le. Note that the …rst two rows come from the A array, while the three next rows come from the B array. . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3 Situation after loading …le myworkspacemA (a .mat …le), which only contains array A. . . 16 2.4 Situation after loading …le myworkspaceaB (an .ascii …le), which only contains array B. 17 2.5 Excel spreadsheet with data that we want to import into Matlab. . . . . . . . . . . . . . . 18 2.6 The result of the Matlab Import Wizard. . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.7 The function y = sinx, where x is the input argument, and y is the output argument. This is an example of a relationship between x and y, which is also a function. . . . . . . 19 2 2 2.8 Implicit plot of x +y = 1. This is an example of a relationship between x and y which is not a function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.9 ThehelpbrowserwindowwhichopenswhenusingthedoccommandinMatlab’scommand window, or by choosing Help/MATLAB Help in the Matlab window. . . . . . . . . . . . . . 42 3.1 Function y = sinx plotted using command plot(y). . . . . . . . . . . . . . . . . . . . . . 44 3.2 Figure Toolbar (upper row) and Plot Edit Toolbar (lower row). Select which toolbar to see from the View menu of the …gure window. . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.3 Function y = sinx, plotted using command plot(x,y). . . . . . . . . . . . . . . . . . . . 45 3.4 Function y = sinx and function cosx plotted using command plot(x,y,x,cos(x)). . . . 46 3.5 Example of edited line styles and line colors. To insert legend, click on the Insert Legend icon (…g. 3.2). To change the default legend text, double-click on the text and edit it. . . 46 3.6 The graph of exp(x) has been added to the graphs of sinx and cosx.. . . . . . . . . . . 47 3.7 Function y = tanx, plotted using command plot(x,tan(x)). . . . . . . . . . . . . . . . . 50 3.8 Thefunctiony = tanx,plottedusingcommandplot(x,y),butwherethey valuesgreater than 3 in absolute value have been replaced by NaN (Not a Number). . . . . . . . . . . . . 51 3.9 The use of the subplot command to produce an array of plots. . . . . . . . . . . . . . . . 52 3.10 The use of the subplot command to produce an array of plots, where there is a mixture of array sizes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.11 Sketch of a simple distillation unit, with feed stream (F), bottom liquid stream (L) with molefractionxoflightcomponent, andtopvaporstream(V)withmolefractiony oflight component. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.12 Equilibrium data for water and ethanol, as found from experiments: (x;y) are mole frac- tions of liquid and vapor at equilibrium. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.13 The equilibrium correlation for content of ethanol in the liquid phase (x) and in the vapor phase (y). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.14 Experimentally found equilibrium correlation for content of ethanol in the liquid phase (x) and in the vapor phase (y). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.1 Experimentaldata((y;x),)andmodel((y (x);x),solid)forwater-ethanolequilibrium. m Notice ‡awed model representation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.2 Experimental data ((y;x),) and model ((y (x);x), solid) for water-ethanol equilibrium. 59 m 4.3 Experimental data ((y;x), ), interpolation model ((y (x);x), solid), and low order m model (y (x);x, dotted) for water-ethanol equilibrium.. . . . . . . . . . . . . . . . . . . 60 m6 viiviii LIST OF FIGURES 4.4 Experimentaldata((y;x),)and splinemodel((y (x);x), solid)forwater-ethanolequi- m librium. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.5 Experimentaldata((y;x),)and pchipmodel((y (x);x), solid)forwater-ethanolequi- m librium. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 4.6 Comparison of various interpolation models. . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.7 6th order least squares polynomial model y (x) of water-ethanol equilibrium, with the m R derivative dy (x))=dx and the integral y (x)dx of y (x). . . . . . . . . . . . . . . . . 68 m m m 4.8 Relationships between thermodynamic variables of vapor phase saturated steam. . . . . . 68  4.9 Pressure (p, atm) as a function of temparature (T, C) for saturated steam. . . . . . . . 69  4.10 Experimentalpressuredata(p, atm,)asafunctionoftemparature(T, C)forsaturated steam, and 4th order least squares model …t (solid line). . . . . . . . . . . . . . . . . . . . 69  4.11 Experimental temparature (T, C,) as a function of pressure (p, atm) for saturated steam, and polynomial least squares models of degrees 3–5. . . . . . . . . . . . . . . . . . 71 4.12 Relationships between thermodynamic variables of superheated steam at p = 0:5atm. . . 72 4.13 Interpolation model for (T;p). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.14 Contour plot of interpolation model for (T;p). . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1 Matlab Editor window, containing a sequence of Matlab commands. Note: if the …le has been saved, the “Save and run”icon changes to the “Run”icon. . . . . . . . . . . . . . . 81 5.2 Matlab editor showing script …le with comments and spaces.. . . . . . . . . . . . . . . . . 82 5.3 Script SumInvSquare for computing S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 n 5.4 Script Fibonacci for computing f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 k 5.5 Matlab Editor with script where the repetition array has more than one row. . . . . . . 84 5.6 The result of running script RepetitionDemo3. . . . . . . . . . . . . . . . . . . . . . . . . 85 6.1 Experimental batch reactor data for reaction with stoichiometry A R. Taken from (Levenspiel 1972), p. 117. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 6.2 Extent of reaction (t) for reactor data based on trapezoidal integration. . . . . . . . . . 88 2 8.1 Scriptfor…ndingrootofax +bx+c = 0,wherea;b;carespeci…edintheMatlabCommand Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 P n 1 6 8.2 Scriptfor…ndingS = =S + toaprespeci…ed accuracyofjS S j 10 .101 n n1 2 n n1 k=1 n 8.3 Script with for loop, where an if statement is used to beak out of the loop. . . . . . . . 102 9.1 Distance x to visible horizon from height h. D and R are the diameter and radius, h respectively, of the earth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 10.1 Some debugger tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 10.2 Example of Matlab code with “lint”: statement Sn_new = 1; is super‡uous and should have been removed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 10.3 Report from checking the code of …g. 10.2 with M-Lint. . . . . . . . . . . . . . . . . . . . 112 10.4 Example of text cells in the Matlab editor.. . . . . . . . . . . . . . . . . . . . . . . . . . . 112 10.5 Tools for taking advantage of cells in Matlab …les. The Show cell titles tool can be used for browsing through the cells: clicking this tool, gives a list of the cells. . . . . . . . 112 12.1 Comparison of cosx, and two Taylor expansions of cosx. . . . . . . . . . . . . . . . . . . . 117 2 15.1 De…nition of function g : x sin x in Computer Algebra System (CAS) MuPAD. Note in particular that the mapping g is independent of what we choose to name the argument. 130 sinx 15.2 Plot of sincx = , x2 6;6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 x 15.3 Plot of function f (x) = sinx0:5 (black), and linear approximation at x = 1 : f (x) sin10:5+(x1)cos1 (gray). We want to …nd x :f (x) = 0. . . . . . . . . . . . . . . . 134 15.4 Function f (x) = cosx0:5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 16.1 Sketch of damped mass-spring system. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141  1 2 2 D.1 Graph of function sinxcosyexp x +y generated using a CAS system (Com- 10 puter Algebra System, here: MuPAD from within the word processor Scienti…c WorkPlace).168  1 2 2 D.2 Graph of function f (x;y) = sinxcosxexp x +y generated in Matlab. . . . . . 168 10LIST OF FIGURES ix D.3 In order to rotate 3D plots: click the rotation icon on the toolbar, click-and-hold in the plot, and “rotate”using the mouse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169  1 2 2 D.4 Graph of function f (x;y) = sinxcosxexp x +y generated in Matlab, with 10 added contour lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170  1 2 2 D.5 Contour lines of function f (x;y) = sinxcosxexp x +y . . . . . . . . . . . . . . 170 10  1 2 2 D.6 Graph of function f (x;y) = sinxcosxexp x +y , mapped on the x-z plane. . . 171 10 E.1 Script …le RepetitionDemoModelFit.m. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 E.2 The model …t of 15-th order polynomial model. Notice that although the model goes through most experimental data points, the model is very poor for interpolation at “high” pressures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178x LIST OF FIGURESList of Tables 2.1 Some built-in Matlab constants.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 Example array which we want to name A. . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.3 Available commands to format data structures in Matlab. . . . . . . . . . . . . . . . . . . 21 2.4 Some commands for creating and manipulating arrays in Matlab. . . . . . . . . . . . . . . 22 2.5 Some commands for basic housekeeping in Matlab. . . . . . . . . . . . . . . . . . . . . . . 31 2.6 Basic binary array operations in Matlab. It is assumed that arrayA and B have the same shape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.7 Selected trigonometric functions in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.8 Selected exponential functions in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.9 Selected complex functions in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.10 Selected rounding and remainder functions in Matlab. . . . . . . . . . . . . . . . . . . . . 39 2.11 Selected number theoretic functions in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . 40 3.1 Basic plotting commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.2 Mo…dying properties of graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.3 Modifying properties of axes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 A 3.4 Illustration of using T Xand LT Xtypesetting in plots. . . . . . . . . . . . . . . . . . . . 49 E E 3.5 Basic plotting commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.1 Table Caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.2 Data set for comparison of interpolation models. . . . . . . . . . . . . . . . . . . . . . . . 61 4.3 Functions on polynomials. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 8.1 Matlab relational functions and operators. In the examples, it is assumed that x = 3, y = 4. 95 8.2 Matlab logical functions and operators. In the examples, it is assumed that a = 1;0;0, b = 1;1;0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 15.1 Somebuilt-inMatlabfunctionsthatrequireafunctionreference(functionhandle)asinput argument. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 16.1 Numerical values for damped mass-spring system. . . . . . . . . . . . . . . . . . . . . . . 142 16.2 Table Caption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 16.3 Parameters and nominal operating conditions for CSTR. . . . . . . . . . . . . . . . . . . . 147 E.1 Some basic functions for operating on strings. . . . . . . . . . . . . . . . . . . . . . . . . . 174 E.2 More advanced functions for operating on strings. . . . . . . . . . . . . . . . . . . . . . . . 176 E.3 Some functions for number conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 F.1 Some numerical functions in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 F.2 Basic binary operations among scalars s and matrices M. . . . . . . . . . . . . . . . . . . 182 F.3 Basic Matrix operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 F.4 Basic matrix factorizations in Matlab. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 xixii LIST OF TABLESPreface These lecture notes are developed for the introductory course in Matlab for Master of Science students at Telemark University College. The lecture notes consists of three parts, and the presentation of each part is scheduled to take 8 hours and to be presented within a single week. Thus, a total of three weeks spread over the opening of the fall semester are used to present the content of the course, with a total of ca. 24 hours of arranged presentations (lectures + exercises). The presentations are integrated with exercises in the courses Numerical Methods, and Modeling of Dynamic Systems, but Matlab is also used in other courses to reinforce the learning. The choice of course content is the result of a couple of meetings between Are Mjaavatten, Bernt Lie, and Randi Holta in the early summer of 2003. Our background experiences for composing the course are:  Continuous use of Matlab for some 10 years, 3  The arrangement of a NIF course in the use of Matlab (BL), where the course content was highly inspiredbytalkswithstudentrepresentativesMarteS.LerdalandBjørnErikThorsteinseninJune 1999,  Experience with using Matlab in courses in Numerical Methods (AM) and Modeling of Dynamic Systems (BL). Comparedtothelecturenotesfor2004,wehavemovedsomeofthematerialwhichisnotofimmediate use to appendices. We have also marked material which is more advanced with an asterisk (). We encourage readers to give feedback on the course content and the presentation style. Finally, it should be mentioned that there exist a useful freeware version of Matlab, named Octave. A brief introduction to the use of Octave and how it deviates from using MathWorks’Matlab version, see www.techteach.no/octave/index.htm. For more information on Octave, see www.octave.org. Are Mjaavatten, Bernt Lie Porsgrunn, August 27 2005 3 NIF = Norske Sivilingeniørers Forening, which has been transformed into Tekna. xiiixiv LIST OF TABLESPart I Analysis of technical data with Matlab 1Chapter 1 The Matlab workplace The Matlab graphical user interface is programmed in Java, and thus looks practically the same on all supported computer platforms. We assume that the reader knows how to start up a program on her/his respective computer. When Matlab is started, it looks more or less as in …g. 1.1 (shows Matlab v. 6.5). The main window in …g. 1.1 is the Command Window. This is where commands are written, and we will show excerpts of such command list in these notes. As an introductory example, assume that we type the command: sin(0.7) Here, the symbol is the Matlab command prompt, and the user does not type this symbol. We simply typed sin(0.7). Matlab responds with: ans = 0.6442 Here, 0.6442 is Matlab’s evaluation of sin(0.7), and the symbol signi…es that Matlab’s Command Windowisreadytoacceptanothercommand. IntheMatlabwindow,wenowhavethefollowingsituation, …g. 1.2. Note that by double-clicking on the item sin(0.7) in the Command History window, this command is copied into the Command Window, and is immediately (re-) executed. Also, by double-clicking on the item ans in the Workspace, the so-called Array Editor is opened, containing the data of variable ans. The Array Editor is a spreadsheet-like window which makes it possible to interactively modify the content of the chosen variable. 34 CHAPTER 1. THE MATLAB WORKPLACE Workspace: table of user defined Click here to change current directory variables and data structures Command window: type commands here Command history: previous commands Figure 1.1: The opening window in Matlab 6.5. It looks almost the same in Matlab 7.0. Variable is named "ans" by default, and Command and response in the is listed in Workspace Command Window The latest command is listed in Command History Figure 1.2: The Matlab window after entering a command.Chapter 2 Arrays: Matlab’s basic data structure 2.1 Overview of learning goals After having completed this chapter, you should have a clear understanding of what an array is, what distinguishes an array from vectors and matrices, and how you can operate on arrays. In particular, you should master:  How to de…ne variables by assigning a value to a name, and how to save, load, and import data,  The creation of arrays, how to pick out subarrays and how to build superarrays,  How to operate on variables using functions and operators,  How to adjust the presentation format of numbers,  Basic housekeeping of variables, commands, and …les,  How to use Matlab’s help system. 2.2 Arrays and data structures By array, we will mean a table of numbers similar to a spreadsheet, where we require that every element in the table contains a number. Thus, there can be no vacant elements, and there can be no text strings or other objects in any of the elements. In principle, an array can hold a single element. In that case, we will use the term scalar to denote the array. If the array consists of a single row of numbers, we will denote it a row array. If the array consists of a single column of numbers, we will denote it a column array. A scalar is a special case of both a row array and of a column array. If the array contains several rows (or columns) of numbers where each row (column) does not have to be a scalar, then we will simply call it a (two dimensional) array. Obviously, both row and column arrays are arrays. Even more complicated arrays could be imagined. We can consider arrays consisting of several layers of two dimensional arrays, much like a spreadsheet can contain several sheets of spreadsheets. In Matlab parlance, we say that the array consists of several pages of (two dimensional) arrays. Together these pages could be considered a volume. We can also consider arrays with several volumes. Together, several volumes could constitute a library. Then we can imagine arrays consisting of several libraries. And so on. Finally, we can also imagine arrays which contains zero elements; these we will denote empty arrays. Originally,Matlabonlysupportedtwodimensionalarrays(andcharacterstrings). Fromversion5ofof Matlab,moregeneralarraysaresupported,aswellasotherdata structures — theseotherdatastructures can be considered various types of tables. Often, row and column arrays are denoted row and column vectors in Matlab literature. Likewise, two dimensional arrays are often denoted matrices (Matlab was originally short for MATrix LABoratory). In these notes, we prefer to use the word array. The reason is that vectors are mathematical objects with very speci…c properties; vectors are not necessarily arrays. Likewise, matrices are mathematical operators with speci…c properties (matrices are arrays, however). Thus, we will use the term array when we do not imply such speci…c properties. 56 CHAPTER 2. ARRAYS: MATLAB’S BASIC DATA STRUCTURE Exercise 2.1 Write down examples of various types of arrays. Exercise 2.2 Write down examples of tables which are not arrays. 2.3 Assignment: Naming data structures During computations, it is very important to be able to give names to data structures, e.g. to arrays. In practice, some data structures have constant value during these computations, while others vary/are changed. In this section, we will look at how we can give names (or: assign names) to data structures in Matlab. Some computer languages distinguish between how constants and variables are named — in Matlab, there is no such distinction We have already seen that Matlab automatically gives a name to a result from a computation, see e.g. …g. 1.2 where the result of a computation was given the name ans. In practice, we need to be able to decide our own names for the data structures. Various computer languages di¤er in how data structures are assigned a name. Symbolically, we coulddescribetheassignmentasdata structure - name,meaning“putthecontentofthedata structuredata structureintoacomputermemorylocationwhichbearsthenamename”. Inmost computer languages, this assignment is written the reverse way as name - data structure, and other symbols may be used for the assignment. In e.g. the computer algebra system Maple, the assignment is written name := data structure. InMatlab,theassignmentsymbolisidenticaltothesymbolwhichisnormallyusedforwritingequality in mathematics: name = data structure: a = 0.7 a = 0.7000 b = sin(a) b = 0.6442 1 Here , a and b are the names we want to use for the data structures, while 0.7 and sin(a) (where a is a replacement for 0.7) are the data structures (they are arrays, or more precisely: scalars). In the assignment it is important both that the name we want to use is a valid name (see below), and that the data structure has a value (e.g. 0.7, and a as a replacement for 0.7). If we switch the sequence of the above commands, an error occurs: clear all b = sin(a) ??? Undefined function or variable ’a’. a = 0.7 a = 0.7000 At…rst, name adoesnothaveavalue, hence sin(a)doesnothaveavaluewhenwetrytoassign sin(a) to b. We’ll come back to the e¤ect of command clear all later. The chosen assignment symbol in Matlab (and many other languages) may be confusing at …rst. Consider a common type of assignment: i = i+1; 1 To get a compact response in the Command Window, the command format compact has …rst been issued.