高等应用数学问题的MATLAB求解02_第1页
高等应用数学问题的MATLAB求解02_第2页
高等应用数学问题的MATLAB求解02_第3页
高等应用数学问题的MATLAB求解02_第4页
高等应用数学问题的MATLAB求解02_第5页
已阅读5页,还剩98页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、星期三, 2008-4- 23, 20:08:07,Slide 1 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Chapter 2Fundamentals of MATLABProgramming,Solving Applied Mathematical Problems with MATLAB,CRC/Taylor & Francis Press Chinese version by Tsinghua University Pr

2、ess,PPT by Wenbin Dong and Jun Peng, Northeastern University, PRC Proofread by Dingyu Xue & YangQuan Chen,星期三, 2008-4- 23, 20:08:07,Slide 2 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Chapter 2 Fundamentals of MATLAB Programming,Fundamenta

3、ls of MATLAB Programming Fundamental Mathematical Calculations Flow Control Structures of MATLAB Language Writing and Debugging MATLAB Functions Two-dimensional Graphics Three-dimensional Graphics Chapter summary,星期三, 2008-4- 23, 20:08:07,Slide 3 (of 103) Dingy Xue and YangQuan Chen, Solving Applied

4、 Mathematical Problems with MATLAB, CRC Press, 2008,MATLAB has the following advantages: Clarity and high efficiency Scientific computation, covers almost all the useful topics in math and engineering Graphics facilities Comprehensive toolboxes and block-sets, designed by experts of almost all disci

5、plines Powerful simulation facilities, unite the sub-systems of different domains together,星期三, 2008-4- 23, 20:08:07,Slide 4 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.1 Fundamentals of MATLAB Programming,Variables and constants in MATL

6、AB Data structures Basic structure of MATLAB Colon expressions and sub-matrices extraction,星期三, 2008-4- 23, 20:08:07,Slide 5 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.1.1 Variables and constants in MATLAB,Variables in MATLAB Starting w

7、ith a letter followed by other characters: Case-sensitive: Valid variable names: MYvar12, MY_Var12 and MyVar12_ Invalid variable names: 12MyVar, _MyVar12 Constants in MATLAB: eps, i, j, pi, NaN, Inf, i=sqrt(-1) lastwarn, lasterr,星期三, 2008-4- 23, 20:08:07,Slide 6 (of 103) Dingy Xue and YangQuan Chen,

8、 Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.1.2 Data structures,Double-precision data type Symbolic data type Other data types,星期三, 2008-4- 23, 20:08:07,Slide 7 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Double-p

9、recision data type,IEEE standard, 64 bits (8 bytes),11 bits for exponential and 53 bits for numerical and a sign bit. Use double() to convert other types to double. Data range: Other data types: int8(), int16(), int32(), uint16(), uint32(),星期三, 2008-4- 23, 20:08:07,Slide 8 (of 103) Dingy Xue and Yan

10、gQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Symbolic data type,Usually used in formula derivations and analytical solutions variable declaration display the symbolic variables in any precision The default value: 32 decimal digits.,星期三, 2008-4- 23, 20:08:07,Slide 9

11、(of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.1,Display the first 300 digits of . MATLAB command One may further increase the number of digits to display. For extremely large number of digits, process may be slow,星期三, 2008-4- 23, 2

12、0:08:07,Slide 10 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Other data types,Strings: String variables are used to store messages. Multi-dimensional arrays: a direct extension of matrices with multiple indices. Cell arrays: to put a set o

13、f data of different types under a single variable, expressed by . Classes and objects: used in Object-Oriented Programming.,星期三, 2008-4- 23, 20:08:07,Slide 11 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.1.3 Basic structures of MATLAB,Dir

14、ect assignment The basic structure of this type of statement is A semicolon can prevent the results from display. Reserved variable: ans store the latest statements without left-hand-variable,星期三, 2008-4- 23, 20:08:07,Slide 12 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Proble

15、ms with MATLAB, CRC Press, 2008,Example 2.2,Enter matrix into MATLAB Commands Other commands,星期三, 2008-4- 23, 20:08:07,Slide 13 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.3,Enter complex matrix into MATLAB MATLAB commands Things

16、 to avoid:,星期三, 2008-4- 23, 20:08:07,Slide 14 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Function call statement Function call examples One function may be called in different ways Built-in functions, *.m functions, Anonymous functions, i

17、nline functions Overload functions,Function call statements,星期三, 2008-4- 23, 20:08:07,Slide 15 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.1.4 Colon expressions and sub-matrices extraction,Colon expression is an effective way in defining

18、 row vectors. Start value , increment and final value . default increment: 1,星期三, 2008-4- 23, 20:08:07,Slide 16 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.4,For different increments, establish vectors for,星期三, 2008-4- 23, 20:08:

19、07,Slide 17 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Sub-matrix Extraction,Basic format numbers of the rows numbers of the columns :, all the columns or rows, depending on the position of it,星期三, 2008-4- 23, 20:08:07,Slide 18 (of 103) D

20、ingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.5,Different sub-matrices can be extracted from the given matrix MATLAB command,星期三, 2008-4- 23, 20:08:07,Slide 19 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with

21、MATLAB, CRC Press, 2008,2.2 Fundamental Mathematical Calculations,Algebraic operations of matrices Logic operations of matrices Relationship operations of matrices Simplifications and presentations of analytical results Basic number theory computations,星期三, 2008-4- 23, 20:08:07,Slide 20 (of 103) Din

22、gy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.2.1 Algebraic operations of matrices,Matrix transpose Matrix addition and subtraction Matrix multiplications Matrix divisions Matrix flip and rotations Matrix power Matrix dot operations,星期三, 2008-4- 23, 2

23、0:08:07,Slide 21 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Matrix transpose,Matrix representation: Matrix A, n rows and m columns, is referred to as an matrix Hermitian transpose Simple transpose,星期三, 2008-4- 23, 20:08:07,Slide 22 (of 10

24、3) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Mathematical representations Difficult to program under C, like A*B MATLAB implementation Note: any variable can be a scalar If not compatible, error messages given,Matrix addition and subtraction,星期三,

25、 2008-4- 23, 20:08:07,Slide 23 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Math expression MATLAB expression Note: the dimension compatibility auto-checked,Matrix multiplication,星期三, 2008-4- 23, 20:08:07,Slide 24 (of 103) Dingy Xue and Yan

26、gQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Matrix left division Solve the linear equations: MATLAB solution: Least squares solution If A is a non-singular square matrix. Then,Matrix division,星期三, 2008-4- 23, 20:08:07,Slide 25 (of 103) Dingy Xue and YangQuan Chen,

27、Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Matrix right division Solve the linear equations: MATLAB solution: Least squares solution If A is a nonsingular square matrix. Then, More precisely,星期三, 2008-4- 23, 20:08:07,Slide 26 (of 103) Dingy Xue and YangQuan Chen, Solving Appl

28、ied Mathematical Problems with MATLAB, CRC Press, 2008,left-right flip: up-down flip: Rotate : How to rotate ?,Matrix flip and rotation,星期三, 2008-4- 23, 20:08:07,Slide 27 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,A is a square matrix, de

29、termine the matrix A to the power x. Math description MATLAB command:,Matrix power,星期三, 2008-4- 23, 20:08:07,Slide 28 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Matrix Dot operation,Element-by-element operation For example: Another exampl

30、e,means,means,星期三, 2008-4- 23, 20:08:07,Slide 29 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.6,Cubic root of MATLAB commands The other two roots,星期三, 2008-4- 23, 20:08:07,Slide 30 (of 103) Dingy Xue and YangQuan Chen, Solving App

31、lied Mathematical Problems with MATLAB, CRC Press, 2008,2.2.2 Logic operations of matrices,Logical variables For new version of MATLAB Non-zero means logic 1 Logical Operations (element-by-element) “And” operation “Or” operation “Not” operation Exclusive Or,星期三, 2008-4- 23, 20:08:07,Slide 31 (of 103

32、) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.2.3 Relationship operations of matrices,Allowed comparisons: , =, , =, =,=, find(), all(), any() Examples:,星期三, 2008-4- 23, 20:08:07,Slide 32 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Math

33、ematical Problems with MATLAB, CRC Press, 2008,2.2.4 Simplifications and presentations of analytical results,Function simple() can be used to simplify mathematical formula: Other commonly used simplification functions numden(), collect(), expand(), factor(),星期三, 2008-4- 23, 20:08:07,Slide 33 (of 103

34、) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.7,Find the simplest form of the polynomial Process it with various functions Expand it,星期三, 2008-4- 23, 20:08:07,Slide 34 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Pro

35、blems with MATLAB, CRC Press, 2008,Two commands to use It is run on the dot operation basis Convert to LATEX expression,Variable substitution,星期三, 2008-4- 23, 20:08:07,Slide 35 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.8,Functi

36、on Use taylor() to evaluate its Taylor expression and convert the results in LATEX,星期三, 2008-4- 23, 20:08:07,Slide 36 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,By MATLAB cos left( b right) -sin left( b right) at+ left( -1/2,cos left( b r

37、ight) a2+cd right) t2+1/6,sin left( b right) a3t3+ left( 1/24,cos left( b right) a4-1/6,cd3-1/6,c3d right) t4-frac 1120,sin left( b right) a5t5 By LaTeX,星期三, 2008-4- 23, 20:08:07,Slide 37 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.2.5 B

38、asic number theory computations,星期三, 2008-4- 23, 20:08:07,Slide 38 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.9,Data set -0.2765, 0.5772,1.4597, 2.1091, 1.191,-1.6187 Observe the results from different rounding functions.,星期三, 2

39、008-4- 23, 20:08:07,Slide 39 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.10,Hilbert matrix can be specified with the statement A=hilb(3), perform the rational transformation. result,星期三, 2008-4- 23, 20:08:07,Slide 40 (of 103) Din

40、gy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.11,1856120 , 1483720, get the GCD (greatest common divider), LCM (least common multiplier) and prime factor decomposition to the least common multiplier,星期三, 2008-4- 23, 20:08:07,Slide 41 (of 103)

41、Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.12,Prime numbers in 1-1000 The prime numbers obtained,星期三, 2008-4- 23, 20:08:07,Slide 42 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,

42、2.3 Flow Control Structures of MATLAB Language,Loop control structures Conditional control structures Switch structure Trial structure,星期三, 2008-4- 23, 20:08:07,Slide 43 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.3.1 Loop control struct

43、ures,The for loop structures If v is a matrix, i pick up one column at a time,星期三, 2008-4- 23, 20:08:07,Slide 44 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,The while loop structures,星期三, 2008-4- 23, 20:08:07,Slide 45 (of 103) Dingy Xue an

44、d YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.13,Compute the sum of using loop structures. the simplest statement,星期三, 2008-4- 23, 20:08:07,Slide 46 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 200

45、8,Example 2.14,Find the minimum value of m such that Only while can be used For structure cannot act alone for the problem,星期三, 2008-4- 23, 20:08:07,Slide 47 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.15,Evaluate the sum of the

46、series MATLAB loop code, and vectorized code,星期三, 2008-4- 23, 20:08:07,Slide 48 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.3.2 Conditional control structures,星期三, 2008-4- 23, 20:08:07,Slide 49 (of 103) Dingy Xue and YangQuan Chen, Solvi

47、ng Applied Mathematical Problems with MATLAB, CRC Press, 2008,星期三, 2008-4- 23, 20:08:07,Slide 50 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.16,Using for and if statements to determine the minimum m It is more complicated than th

48、e while structure.,星期三, 2008-4- 23, 20:08:07,Slide 51 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.3.3 Switch structure,星期三, 2008-4- 23, 20:08:07,Slide 52 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MA

49、TLAB, CRC Press, 2008,星期三, 2008-4- 23, 20:08:07,Slide 53 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.3.4 Trial structure,This is a brand new structure Advantages: An error trap More efficient algorithm implementation,星期三, 2008-4- 23, 20:

50、08:07,Slide 54 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.4 Writing and Debugging MATLAB Functions,Basic structure of MATLAB functions Programming of functions with variable inputs/outputs Inline functions and anonymous functions,星期三, 2

51、008-4- 23, 20:08:07,Slide 55 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,2.4.1 Basic structure of MATLAB functions,nargin, nargout, varargin, varargout,星期三, 2008-4- 23, 20:08:07,Slide 56 (of 103) Dingy Xue and YangQuan Chen, Solving Applie

52、d Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.17,Why functions? Problem M-script can be written and saved as an M-file. If 10000 is changed to other values, the M-file should be modified A new file format (function) is needed,星期三, 2008-4- 23, 20:08:07,Slide 57 (of 103) Dingy Xue and

53、 YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.18,For the preceding requirement to change 10000, let us use MATLAB function MATLAB description Benefit: no need to change the program,星期三, 2008-4- 23, 20:08:07,Slide 58 (of 103) Dingy Xue and YangQuan Chen,

54、 Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.19,Design a MATLAB function for an Hilbert matrix, whose element is . If only one input argument n is given in the calling command, a square matrix should be generated, such that m = n. Certain help information to this fun

55、ction is required. Check the formats of input and returned arguments.,星期三, 2008-4- 23, 20:08:07,Slide 59 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,edit myhilb,星期三, 2008-4- 23, 20:08:07,Slide 60 (of 103) Dingy Xue and YangQuan Chen, Solvi

56、ng Applied Mathematical Problems with MATLAB, CRC Press, 2008,On-line help command Generate Hilbert matrices,星期三, 2008-4- 23, 20:08:07,Slide 61 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.20,Write a recursive function to evaluate

57、 the factorial n!. Recursive calls,星期三, 2008-4- 23, 20:08:07,Slide 62 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Determine 11! Other functions,星期三, 2008-4- 23, 20:08:07,Slide 63 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Mathematical Problems with MATLAB, CRC Press, 2008,Example 2.21,Compare recursive algorithm with loop structure in constructing the Fibonacci arrays. Fibonacci arrays: MATLAB codes,星期三, 2008-4- 23, 20:08:07,Slide 64 (of 103) Dingy Xue and YangQuan Chen, Solving Applied Ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论