Charpter 1 Basic Matlab Chapter 1 Basic Matlab 1.1 Introduction MATLAB is a software package for computation in interface:界面 engineering,science,and applied mathematics. inexact:不精确 It offers a powerful programming language,excellent graphics, arithmetic:计算 and a wide range of expert knowledge.MATLAB is published oriented:面向 by and a trademark of The MathWorks,Inc. ymbolic:形象化的 The focus in MATLAB is on computation,not mathematics: manipulation:操作 Symbolic expressions and manipulations are not possible (except rounding errors: through the optional Symbolic Toolbox,a clever interface to 舍入误差 Maple).All results are not only numerical but also inexact, popular lore:通俗故事 thanks to the rounding errors inherent in computer arithmetic. vice versa:反之亦然 The limitation to numerical computation can be seen as a number crunching: drawback,but it is a source of strength too:MATLAB is much 捣弄数字 preferred to Maple,Mathematica,and the like when it comes to underflow:下溢 numeric. overflow:上溢 On the other hand,compared to other numerically oriented languages like C++and FORTRAN,MATLAB is much easier to use and comes with a huge standard library.The unfavorable comparison here is a gap in execution speed.This gap is not always as dramatic as popular lore has it,and it can often be narrowed or closed with good MATLAB programming. Moreover,one can link other codes into MATLAB,or vice versa, and MATLAB now optionally supports parallel computing.Still, MATLAB is usually not the tool of choice for maximum- performance computing. 1.2 History Engineering and scientific applications involve a lot of "number crunching".For many years,the main language for this was FORTRAN-first "high level"programming language,and especially designed for numerical computing. "Number crunching"on a computer can be tricky For example,problems that occur are: 1.Loss of precision and inaccurate results: X=0.1 Y=1.0-10*X Y "should"equal 0,but probably does not! 2.Underflow and overflow:X=1.0e20,X*X too big! The programme below all result in programming errors! The U.S.government recognized these problems,and the inefficiency of many engineers all writing the same algorithms
Charpter 1 Basic Matlab Chapter 1 Basic Matlab 1.1 Introduction MATLAB is a software package for computation in engineering, science, and applied mathematics. It offers a powerful programming language, excellent graphics, and a wide range of expert knowledge. MATLAB is published by and a trademark of The MathWorks, Inc. The focus in MATLAB is on computation, not mathematics: Symbolic expressions and manipulations are not possible (except through the optional Symbolic Toolbox, a clever interface to Maple). All results are not only numerical but also inexact, thanks to the rounding errors inherent in computer arithmetic. The limitation to numerical computation can be seen as a drawback, but it is a source of strength too: MATLAB is much preferred to Maple, Mathematica, and the like when it comes to numeric. On the other hand, compared to other numerically oriented languages like C++ and FORTRAN, MATLAB is much easier to use and comes with a huge standard library. The unfavorable comparison here is a gap in execution speed. This gap is not always as dramatic as popular lore has it, and it can often be narrowed or closed with good MATLAB programming. Moreover, one can link other codes into MATLAB, or vice versa, and MATLAB now optionally supports parallel computing. Still, MATLAB is usually not the tool of choice for maximumperformance computing. 1.2 History Engineering and scientific applications involve a lot of "number crunching". For many years, the main language for this was FORTRAN - first "high level" programming language, and especially designed for numerical computing. "Number crunching" on a computer can be tricky. For example, problems that occur are: 1. Loss of precision and inaccurate results: X = 0.1 Y = 1.0 - 10*X Y "should" equal 0, but probably does not! 2. Underflow and overflow: X = 1.0e20, X*X → too big! The programme below all result in programming errors! The U.S. government recognized these problems, and the inefficiency of many engineers all writing the same algorithms. interface:界面 inexact:不精确 arithmetic:计算 oriented:面向 symbolic:形象化的 manipulation:操作 rounding errors: 舍入误差 popular lore:通俗故事 vice versa:反之亦然 number crunching: 捣弄数字 underflow:下溢 overflow:上溢
Charpter 1 Basic Matlab again and again.So,they commissioned numerical analysts to subroutine: write good quality algorithms for common tasks.They make the eigenvalue: results freely available as"libraries"of subroutines than anyone eigenvector:特征向量 can use in their programs.The libraries are available at:interactive: www.netlib.org linear algebra:线性代 In the middle of 1970s,Cleve Moler,mathematician,C.S. Professor,write LINPACK and EISPACK for student to do the factor a matrix problem of linear algebra. 解 compile:编译 debug:调试 Figure 1.1 Cleve Moler Figure 1.2 Jack Little LINPACK:linear algebra subroutines for vector-matrix operations,solving linear systems,factoring a matrix,inverting a matrix.Later replaced by LAPACK. EISPACK:compute eigenvalues and eigenvectors of matrices. Example:solve A*x=b using LINPACK Cleve Moler thought this is still too much work:write FORTRAN,compile,debug,compile,run.He wanted to give students easy access to LINPACK. So,he wrote MATLAB ("Matrix Laboratory").The "laboratory"is interactive,easy input and output,and can be operated on a whole vector or matrix at once. Example:solve b=A*x in Matlab. x=A\b MATLAB quickly became quite popular and used for both teaching and research.It was also free. In 1983,an engineer,Jack Little,saw Matlab during a lecture by Cleve Moler at Stanford University.He saw the commercial potential and(with permission)rewrote Matlab in C.In the same time,they added "M-files"(stored programs)many new features and libraries In1984,they founded The Mathworks to market it. 1.3 Basic Programming
Charpter 1 Basic Matlab again and again. So, they commissioned numerical analysts to write good quality algorithms for common tasks. They make the results freely available as "libraries" of subroutines than anyone can use in their programs. The libraries are available at: www.netlib.org In the middle of 1970s, Cleve Moler, mathematician, C.S. Professor, write LINPACK and EISPACK for student to do the problem of linear algebra. LINPACK: linear algebra subroutines for vector-matrix operations, solving linear systems, factoring a matrix, inverting a matrix. Later replaced by LAPACK. EISPACK: compute eigenvalues and eigenvectors of matrices. Example: solve A*x = b using LINPACK Cleve Moler thought this is still too much work: write FORTRAN, compile, debug, compile, run. He wanted to give students easy access to LINPACK. So, he wrote MATLAB ("Matrix Laboratory"). The “laboratory” is interactive, easy input and output, and can be operated on a whole vector or matrix at once. Example: solve b = A*x in Matlab. x = A \ b MATLAB quickly became quite popular and used for both teaching and research. It was also free. In 1983, an engineer, Jack Little, saw Matlab during a lecture by Cleve Moler at Stanford University. He saw the commercial potential and (with permission) rewrote Matlab in C. In the same time, they added "M-files" (stored programs) many new features and libraries . In1984,they founded The Mathworks to market it. 1.3 Basic Programming Figure 1.1 Cleve Moler Figure 1.2 Jack Little subroutine:子程序 eigenvalue:特征值 eigenvector:特征向量 interactive:交互式 linear algebra:线性代 数 factor a matrix:矩阵分 解 compile:编译 debug:调试
Charpter 1 Basic Matlab Let us start with something simple,like defining a row vector bracket: with components the numbers 1,2,3,4,5 and assigning it a enclose: variable name,say,x. space:空格 》x=[12345] demonstrate:证明 X= comment:说明 12345 specific:具体 Note that we used the equal sign for assigning the variable automatically: name x to the vector,brackets to enclose its entries and spaces to assign: separate them (just like you would using the linear algebra equal sign: notation).We could have used commas (,instead of spaces to transpose: separate the entries,or even a combination of the two.The use of single quote: either spaces or commas is essential! To create a column vector (MATLAB distinguishes between row and column vectors,as it should)we can either use semicolons (to separate the entries,or first define a row vector and take its transpose to obtain a column vector.Let us demonstrate this by defining a column vector y with entries 6,7, 8,9,10 using both techniques. 》y=[6;78:9;10] y= 6 1 8 9 10 》y=[6,7,8,9,10] y= 678910 》y ans= 6 2 8 9 10 Let us make a few comments.First,note that to take the transpose of a vector (or a matrix for that matter)we use the single quote ()Also note that MATLAB repeats (after it processes)what we typed in.Sometimes,however,we might not wish to "see"the output of a specific command.We can suppress the output by using a semicolon (;at the end of the command line. Finally,keep in mind that MATLAB automatically assigns the variable name ans to anything that has not been assigned a name
Charpter 1 Basic Matlab Let us start with something simple, like defining a row vector with components the numbers 1, 2, 3, 4, 5 and assigning it a variable name, say, x. » x = [1 2 3 4 5] x = 1 2 3 4 5 Note that we used the equal sign for assigning the variable name x to the vector, brackets to enclose its entries and spaces to separate them (just like you would using the linear algebra notation). We could have used commas (,) instead of spaces to separate the entries, or even a combination of the two. The use of either spaces or commas is essential! To create a column vector (MATLAB distinguishes between row and column vectors, as it should) we can either use semicolons (;) to separate the entries, or first define a row vector and take its transpose to obtain a column vector. Let us demonstrate this by defining a column vector y with entries 6, 7, 8, 9, 10 using both techniques. » y = [6;7;8;9;10] y = 6 7 8 9 10 » y = [6,7,8,9,10] y = 6 7 8 9 10 » y’ ans = 6 7 8 9 10 Let us make a few comments. First, note that to take the transpose of a vector (or a matrix for that matter) we use the single quote (‘’). Also note that MATLAB repeats (after it processes) what we typed in. Sometimes, however, we might not wish to “see” the output of a specific command. We can suppress the output by using a semicolon (;) at the end of the command line. Finally, keep in mind that MATLAB automatically assigns the variable name ans to anything that has not been assigned a name. bracket:括号 enclose:围住 space:空格 demonstrate:证明 comment:说明 specific:具体 automatically:自动的 assign:制定 equal sign:等号 transpose:转置 single quote:单引号
Charpter 1 Basic Matlab In the example above,this means that a new variable has been recycle created with the column vector entries as its value.The variable retrieve ans,however,gets recycled and every time we type in a keep track of: command without assigning a variable,ans gets that value. grand total:总计 It is good practice to keep track of what variables are defined invoking: and occupy our workspace.Due to the fact that this can be cumbersome,MATLAB can do it for us.The command whos gives all sorts of information on what variables are active. 》whos Name Size Elements Bytes Density Complex ans 5×1 5 40 Full No X 1×5 5 40 Full No y 1×5 40 Full No Grand total is 15 elements using 120 bytes A similar command,called who,only provides the names of the variables that are active. 》who Your variables are: ansxy If we no longer need a particular variable we can "erase"it from memory using the command clear variable name. Let us clear the variable ans and check that we indeed did so. 》clear ans 》who Your variables are: xy The command clear used by itself,"erases"all the variables from the memory.Be careful,as this is not reversible and you do not have a second chance to change your mind. You may exit the program using the quit command.When doing so,all variables are lost. However,invoking the command save filename before exiting, causes all variables to be written to a binary file called filename.mat.When we start MATLAB again,we may retrieve the information in this file with the command "load filename". We can also create an ascii (text)file containing the entire MATLAB session if we use the command diary One last command to mention before we start learning some more interesting things about MATLAB is the help command This provides help for any existing MATLAB command. Let us try this command on the command who
Charpter 1 Basic Matlab In the example above, this means that a new variable has been created with the column vector entries as its value. The variable ans, however, gets recycled and every time we type in a command without assigning a variable, ans gets that value. It is good practice to keep track of what variables are defined and occupy our workspace. Due to the fact that this can be cumbersome, MATLAB can do it for us. The command whos gives all sorts of information on what variables are active. » whos Name Size Elements Bytes Density Complex ans 5×1 5 40 Full No x 1×5 5 40 Full No y 1×5 5 40 Full No Grand total is 15 elements using 120 bytes A similar command, called who, only provides the names of the variables that are active. » who Your variables are: ans x y If we no longer need a particular variable we can “erase” it from memory using the command clear variable_name. Let us clear the variable ans and check that we indeed did so. » clear ans » who Your variables are: x y The command clear used by itself, “erases” all the variables from the memory. Be careful, as this is not reversible and you do not have a second chance to change your mind. You may exit the program using the quit command. When doing so, all variables are lost. However, invoking the command save filename before exiting, causes all variables to be written to a binary file called filename.mat. When we start MATLAB again, we may retrieve the information in this file with the command “load filename”. We can also create an ascii (text) file containing the entire MATLAB session if we use the command diary One last command to mention before we start learning some more interesting things about MATLAB is the help command. This provides help for any existing MATLAB command. Let us try this command on the command who. recycle 再利用 retrieve 得到 keep track of:跟踪 grand total:总计 invoking:发出
Charpter 1 Basic Matlab 》help who binary:二进制 WHO List current variables. entry:元素 WHO lists the variables in the current workspace increment::增量 WHOS lists more information about each variable. figure out:算出 WHO GLOBAL and WHOS GLOBAL list the variables in the default:默认 global workspace. Try using the command help on itself! On a PC,help is also available from the Window Menus. Sometimes it is easier to look up a command from the list provided there,instead of using the command line help. 1.4 Vectors and matrices We have already seen how to define a vector and assign a variable name to it.Often it is useful to define vectors (and matrices)that contain equally spaced entries.This can be done by specifying the first entry,an increment,and the last entry. MATLAB will automatically figure out how many entries you need and their values.For example,to create a vector whose entries are 0,1,2,3,.,7,8,you can type 》u=[0:8] u= 012345678 Here we specified the first entry 0 and the last entry 8, separated by a colon (:)MATLAB automatically filled-in the (omitted)entries using the (default)increment 1.You could also specify an increment as is done in the next example. To obtain a vector whose entries are 0,2,4,6,and 8,you can type in the following line: 》V=[02:8] V= 02468 Here we specified the first entry 0,the increment value 2,and the last entry 8.The two colons (:)"tell"MATLAB to fill in the (omitted)entries using the specified increment value. MATLAB will allow you to look at specific parts of the vector. If you want,for example,to only look at the first 3 entries in the vector v,you can use the same notation you used to create the vector: 》v(1:3) ans= 024 Note that we used parentheses,instead of brackets,to refer to the entries of the vector.Since we omitted the increment value, MATLAB automatically assumes that the increment is 1.The
Charpter 1 Basic Matlab » help who WHO List current variables. WHO lists the variables in the current workspace. WHOS lists more information about each variable. WHO GLOBAL and WHOS GLOBAL list the variables in the global workspace. Try using the command help on itself! On a PC, help is also available from the Window Menus. Sometimes it is easier to look up a command from the list provided there, instead of using the command line help. 1.4 Vectors and matrices We have already seen how to define a vector and assign a variable name to it. Often it is useful to define vectors (and matrices) that contain equally spaced entries. This can be done by specifying the first entry, an increment, and the last entry. MATLAB will automatically figure out how many entries you need and their values. For example, to create a vector whose entries are 0, 1, 2, 3, ., 7, 8, you can type » u = [0:8] u = 0 1 2 3 4 5 6 7 8 Here we specified the first entry 0 and the last entry 8, separated by a colon (:). MATLAB automatically filled-in the (omitted) entries using the (default) increment 1. You could also specify an increment as is done in the next example. To obtain a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line: » v = [0:2:8] v = 0 2 4 6 8 Here we specified the first entry 0, the increment value 2, and the last entry 8. The two colons (:) “tell” MATLAB to fill in the (omitted) entries using the specified increment value. MATLAB will allow you to look at specific parts of the vector. If you want, for example, to only look at the first 3 entries in the vector v, you can use the same notation you used to create the vector: » v(1:3) ans = 0 2 4 Note that we used parentheses, instead of brackets, to refer to the entries of the vector. Since we omitted the increment value, MATLAB automatically assumes that the increment is 1. The binary:二进制 entry:元素 increment:增量 figure out:算出 default:默认