Introduction to MATLAB--Process Control __matlab

Source: Internet
Author: User
Tags rand
The introduction of MATLAB Tutorial-Process Control 1-2, repeat command The simplest repeating command is the For Loop (For-loop), whose basic form is: for variable = matrix; The value of the variable is then set to each row of the matrix to perform the expression between the for and end. Therefore, in the absence of an exception, the number of expressions performed is equal to the number of rows in the matrix. For example, the following command produces a 6-length harmonic sequence (harmonic sequence): x = zeros (1,6); % x is a 16 0 matrix   For i = 1:6, x (i) = 1/i; End in the example above, Matrix X is originally a 16 0 matrix, in which the value of variable i is 1 to 6 in For, so the value of the first element of Matrix X is set to 1/i in turn. We can use fractions to display this series: The format rat uses fractions to represent the value disp (x) 1 1/2 1/3 1/4 1/5 1/6 For loops may be multilayered, and the following example produces a 16 Hilbert matrix H, where the elements in column I and J are: H = Zeros (6); For i = 1:6, for j = 1:6, h (i,j) = 1/(i+j-1); End End Disp (h) 1 1/2 1/3 1/4 1/5 1/6 + 1/2 1/3 1/4 1/5 1/6 1/7 1/3 1/4 1/5 1/6 1/7 1/8 1/4 1/5 1/6 1/7 1/8 1/9 1/5 1/7 1/8 1/9 1/10 1/6 1/7 1/8 1/9   tips: Pre-configured matrices   in the above example, we use 1/10 to pre-configured (1/11) a matrix of the appropriate size. If you do not configure the matrix, the program can still execute, but at this time the MATLAB needs to dynamically increase (or decrease) the size of the matrix, thus reducing the execution efficiency of the program. So when using a matrix, if you know its size beforehand, it is best to use commands such as zeros or ones to pre-configured the desired memory (that is, the matrix) size. In the following example, the For loop lists the sum of squares of each row of the previously generated Hilbert matrix: For i = h, disp (Norm (i) ^2); % prints the square and end of each row 1299/871 282/551 650/2343   524/2933 559/4431 831/8801 in the above example, each time I's value is a row of matrix H, so the written commands are particularly concise. So that a commonThe repeating command is the While circle, its basic form is: while conditional, expression, end that is, as long as the condition is set, the expression is executed repeatedly. For example, a previous example of a harmonic sequence, we can rewrite the While circle as follows: x = zeros (1,6); % x is a 16 of the 0 matrix i = 1; While I <= 6, x (i) = 1/i; i = i+1; End format Short 1-3, the simplest logical command for a logical command is if, ..., end, and its basic form is: if conditional; expression; End If Rand (1,1) > 0.5, disp (' Given random number is greater than 0.5. '); End Given random number is greater than 0.5. 1-4. Assemble multiple commands in an M file to execute a large number of MATLAB commands, these commands can be stored in a file with a secondary file name M, and the MATLAB hint number under the main file name can be typed. This type of file containing the MATLAB command is in m as a file name, so it is commonly known as M files (m-files). For example, an m file named TEST.M contains a series of MATLAB commands, so just type test directly to execute the commands it contains: pwd% displays the current directory ans =   d:/matlab5/bin CD C:/data/mlbook% Enter TEST.M's directory type test.m% of TEST.M content% This was my-i-Test m-file. % Roger Jang, March 3, 1997 fprintf (' Start of test.m!/n '); For i = 1:3, fprintf (' i =%d---> i^3 =%d/n ', I, i^3); End fprintf (' End of test.m!/n '); Test% Execute test.m   Start of test.m! i = 1---> i^3 = 1 i = 2---> i^3 = 8 i = 3---> i^3 = end of test.m!   Tips: First callout line (H1 help lines)  test.m the first two lines are annotations, you can make your program easier to understand and manage. In particular, the first annotation line is typically used to briefly describe the function of this m file so that lookfor can find the M file in a keyword-specific way. For example, the first comment line in TEST.M contains the word test, so if you type LookFor  test,matlab, you can list all M files that contain test in the first callout line, so TEST.M is also listed. Strictly speaking, M files can be subdivided into command sets (Scripts) and functions (functions). The aforementioned test.m is a set of commands, and the utility is exactly the same as the command input, so that the variables in the command set can be used directly, and those set in the command set are also visible in the workspace. Functions need to use input arguments (inputs arguments) and output arguments to convey information, which is like a function in C, or a FORTRAN secondary program (subroutines). For example, to compute the factorial of a positive integer (factorial), we can write a Matlab function as follows and archive it in fact.m:function output = fact (n)% fact Calculate factorial of a GI Ven positive integer. output = 1; For i = 1:n, output = output*i; End where fact is the function name, n is the input argument, output is an argument, and I is the temporary variable used by this function. To use this function, type the function name directly and the appropriate input value: y = fact (5)   y = 120 (of course, before executing fact, you must first enter the directory where FACT.M resides. In the execution of fact (5), Matlab will jump into a lower-level temporary work space (Temperary workspace), the value of the variable n is set to 5, and then the internal operation of the functions, all the internal operations generated by the variables (including input argument n, temporary variable i, and output arguments are present in this temporary workspace. After the operation, Matlab sets the value of the last output argument to the upper variable y, and clears the temporary workspace and all the variables it contains. In other words, when calling a function, you can only control the input of the function by the input argument, and the output of the function by the output argument, but all the temporary variables will disappear as the function ends, and you cannot get their values.   Tips: HaveGuan Jie multiplication function   The factorial function used in front (and back) is simply used to illustrate the function concept of MATLAB. If you actually want to compute the factorial of a positive integer n (that is, n!), you can write directly to prod (1:n), or call the Gamma function directly: Gamma (n-1). The function of Matlab can also be recursive  (recursive), that is to say, a function can call itself. For example, n! = N (n-1)!, so the preceding factorial function can be changed to a recursive : function output = fact (n)% fact Calculate factorial of a given positive integer recursive Ly.     If n = 1,% terminating condition output = 1; Return End output = N*fact (n-1); When writing a recursive  function, be sure to include the end condition (terminating condition), otherwise this function will call itself again and again, never stop until the computer's memory is exhausted. For the above example, n==1 satisfies the end condition, at which point we set output to 1 instead of calling the function itself. Sender: Alphazhao (Zi Feather & Three laughs), letter area: Modelling mark   title: Matlab Introduction (3) Letter station: Wuhan Baiyun Huang Station (Fri Dec 10 14:41:49 1999), Station letter   letters Man: Mars (Chaos, fractal, Cycle III), letter area: Math standard   title: Matlab Introductory Course--Process Control Letter station: a net affectionate (Sun Nov 29 17:35:36 1998), a letter 1-2, repeat command the simplest repeat command is For Circle ( For-loop), its basic form is: for variable = matrix; The value of the variable is then set to each row of the matrix to perform the expression between the for and end. Therefore, in the absence of an exception, the number of expressions performed is equal to the number of rows in the matrix. For example, the following command produces a 6-length harmonic sequence (harmonic sequence): x = zeros (1,6); % x is a 16 0 matrix   For i = 1:6, x (i) = 1/i; End in the example above, Matrix X is initially a 16 0 matrix, in the ForCircle, the value of variable i is 1 to 6 in turn, so the value of the first element of Matrix X is set to 1/i. We can use fractions to display this series: The format rat uses fractions to represent the value disp (x) 1 1/2 1/3 1/4 1/5 1/6 For loops may be multilayered, and the following example produces a 16 Hilbert matrix H, where the elements in column I and J are: H = Zeros (6); For i = 1:6, for j = 1:6, h (i,j) = 1/(i+j-1); End End Disp (h) 1 1/2 1/3 1/4 1/5 1/6 + 1/2 1/3 1/4 1/5 1/6 1/7 1/3 1/4 1/5 1/6 1/7 1/8 1/4 1/5 1/6 1/7 1/8 1/9 1/5 1/7 1/8 1/9 1/10 1/6 1/7 1/8 1/9   tips: Pre-configured matrices   in the above example, we use 1/10 to pre-configured (1/11) a matrix of the appropriate size. If you do not configure the matrix, the program can still execute, but at this time the MATLAB needs to dynamically increase (or decrease) the size of the matrix, thus reducing the execution efficiency of the program. So when using a matrix, if you know its size beforehand, it is best to use commands such as zeros or ones to pre-configured the desired memory (that is, the matrix) size. In the following example, the For loop lists the sum of squares of each row of the previously generated Hilbert matrix: For i = h, disp (Norm (i) ^2); % prints the square and end of each row 1299/871 282/551 650/2343   524/2933 559/4431 831/8801 in the above example, each time I's value is a row of matrix H, so the written commands are particularly concise. A commonly used repeating command is a While loop whose basic form is: while conditional; an expression; end that is, the expression is executed repeatedly as long as the condition is set. For example, a previous example of a harmonic sequence, we can rewrite the While circle as follows: x = zeros (1,6); % x is a 16 of the 0 matrix i = 1; While I <= 6, x (i) = 1/i; i = i+1; End format 1-3, the simplest logical command for logical commands is if, ..., end, which is the basic form: if conditional; expressions; End If Rand (1,1)> 0.5, disp (' Given random number is greater than 0.5. '); End Given random number is greater than 0.5. 1-4. Assemble multiple commands in an M file to execute a large number of MATLAB commands, these commands can be stored in a file with a secondary file name M, and the MATLAB hint number under the main file name can be typed. This type of file containing the MATLAB command is in m as a file name, so it is commonly known as M files (m-files). For example, an m file named TEST.M contains a series of MATLAB commands, so just type test directly to execute the commands it contains: pwd% displays the current directory ans =   d:/matlab5/bin CD C:/data/mlbook% Enter TEST.M's directory type test.m% of TEST.M content% This was my-i-Test m-file. % Roger Jang, March 3, 1997 fprintf (' Start of test.m!/n '); For i = 1:3, fprintf (' i =%d---> i^3 =%d/n ', I, i^3); End fprintf (' End of test.m!/n '); Test% Execute test.m   Start of test.m! i = 1---> i^3 = 1 i = 2---> i^3 = 8 i = 3---> i^3 = end of test.m!   Tips: The first two lines of &NBSP;TEST.M (H1 help lines) are annotations that make the program easier to understand and manage. In particular, the first annotation line is typically used to briefly describe the function of this m file so that lookfor can find the M file in a keyword-specific way. For example, the first comment line in TEST.M contains the word test, so if you type LookFor  test,matlab, you can list all M files that contain test in the first callout line, so TEST.M is also listed. Strictly speaking, M files can be subdivided into command sets (Scripts) and functions (functions). The aforementioned test.m is the command set, and the utility is exactly the same as the command input, so if you can use the command set directly from theThe variables in the workspace, and the variables set in the command set, are also visible in the work space. Functions need to use input arguments (inputs arguments) and output arguments to convey information, which is like a function in C, or a FORTRAN secondary program (subroutines). For example, to compute the factorial of a positive integer (factorial), we can write a Matlab function as follows and archive it in fact.m:function output = fact (n)% fact Calculate factorial of a GI Ven positive integer. output = 1; For i = 1:n, output = output*i; End where fact is the function name, n is the input argument, output is an argument, and I is the temporary variable used by this function. To use this function, type the function name directly and the appropriate input value: y = fact (5)   y = 120 (of course, before executing fact, you must first enter the directory where FACT.M resides. In the execution of fact (5), Matlab will jump into a lower-level temporary work space (Temperary workspace), the value of the variable n is set to 5, and then the internal operation of the functions, all the internal operations generated by the variables (including input argument n, temporary variable i, and output arguments are present in this temporary workspace. After the operation, Matlab sets the value of the last output argument to the upper variable y, and clears the temporary workspace and all the variables it contains. In other words, when calling a function, you can only control the input of the function by the input argument, and the output of the function by the output argument, but all the temporary variables will disappear as the function ends, and you cannot get their values.   Tips: The factorial function   front (and back) of the factorial function is simply used to illustrate the function of MATLAB concept. If you actually want to compute the factorial of a positive integer n (that is, n!), you can write directly to prod (1:n), or call the Gamma function directly: Gamma (n-1). The function of Matlab can also be recursive  (recursive), that is to say, a function can call itself. For example, n! = N (n-1)!, so the previous factorial function can be changed to a recursive : function output = fact (n)% fact Calculate factorial of a GIVen positive integer recursively.     If n = 1,% terminating condition output = 1; Return End output = N*fact (n-1); When writing a recursive  function, be sure to include the end condition (terminating condition), otherwise this function will call itself again and again, never stop until the computer's memory is exhausted. For the above example, n==1 satisfies the end condition, at which point we set output to 1 instead of calling the function itself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.