I. script files and M function 1.1 script files
A script file is a collection of command lines and consists of a series of MATLAB commands, built-in functions, and m files. The script file is created in the MATLAB compiler and saved as a. m file. It is executed in sequence. The variables generated during execution are stored in the current workspace.
Note: The script cannot return output variables. All created variables are stored in the workspace, but the script can provide graphical output, just like using the graphic output function plot.
Example:
Use the M file editor, type a command, and save it as magicrank. M.
Array = zeros (1, 32 ); For N = 3:32 Array (n) = rank (MAGIC (n )); End Array Bar (array) % bar chart output result |
Run the M file. The result is 1-1.
Figure 1-1 function running result Diagram
1.2 m Functions
M functions are also called subprograms. They must be called by MATLAB and have certain universality.
1.2.1 M Function Structure
In terms of structure, the biggest difference between M functions and script files is the addition of a "function declaration line ".
M Function Format: function [return variable table] = function name (input variable List)
Description
Function body statement
Call format: output variable = function name (input variable)
(1) The function definition name should be the same as the file storage name. When an M file contains multiple M functions, the first function is the main function, and the M file name must be the main function name;
2) The name of the function file in MATLAB must start with a letter. It can be any combination of letters, underscores, and numbers, but cannot exceed 31 characters.
3) M function comments are represented by rows starting with %. Help function_name displays the comments after the first row.
Ii. Data Analysis 2.1 polynomials and their functions
In Matlab, a one-dimensional vector is used to represent the multi-direction formula. The polynomial coefficients are stored in the vector in descending order. For example, the specific representation of Polynomial p (x) is as follows:
The following is a vector composed of coefficients:
2.1.1 polynomial Root
MATLAB provides functions for solving the root of a polynomial
Example: Root of Polynomial
In the command window, enter:
> P = [3,-,]; > R = roots (P) |
The output result is as follows:
R = 4.0283 + 2.8554i 4.0283-2.8554i -2.3616 + 2.8452i -2.3616-2.8452i |
2.1.2 polynomial operation
Polynomial functions
Calculation relationship |
Corresponding function commands |
Multiplication |
Conv (A, B) |
Division |
[Q, R] = deconv (A, B) Q is the quotient of Polynomial a divided by polynomial B, and the remainder is R |
Polynomial Derivation |
Polyder (P) |
Polynomial points |
Polyint (P, K) returns the point of the polynomial P, and the point constant is K. |
2.2 rational Polynomials
In many applications, such as Fourier, Laplace, and Z transformations, there is a ratio of two polynomials. The expansion functions of polynomials in Matlab are as follows:
[R, P, K] = residue (B,)
B. A is the polynomial coefficient vector of the numerator and denominator respectively. R, P, and K are the retention, pole, and direct items respectively.
For example, partial fraction is expanded and the result is
Code:
> A = [1 5 6]; > B = [1]; > [R, S, K] = residue (B,) |
Output result:
R = -1.0000 1.0000 S = -3.0000 -2.0000 K = [] |