Getting started with matlab-numerical calculation
All of MATLAB's operations are based on matrices, and most of the data in MATLAB is almost always matrices. In the following introduction, mainly from these aspects of MATLAB basic numerical calculation instructions:
1. Creation of matrices
2. Operation of The Matrix
3. Operation of the polynomial
4. Linear interpolation 1. Creation of matrices
There are two main ways to create matrices in MATLAB:
1. Create by direct input matrix data
2. Create a function by calling the matrix
1. Create by direct input matrix data:
This is the most common way to create a matlab matrix, the data directly entered must be included in the [], the data separated by a space or a comma, the data must be separated by semicolons, each element of the matrix can be an expression, a real number, a complex number, such as:
(1) Enter the line matrix of the 1x5 and assign to a:
Use spaces as separators between different column data for each row:
>> a=[1 2 3 4 5]
a =
1 2 3 4 5
Use "," as a delimiter between different column data for each row:
>> a=[1,2,3,4,5]
a =
1 2 3 4 5
(2) Create the row matrix of the 2x3 and assign to a:
>> a=[2,3,4;1,5,7]
a =
2 3 4
1 5 7
>> b=[1,6+7i,5;2,7.5,8]
b =
1.0000 6.0000 + 7.0000i 5.0000
2.0000 7.5000 8.0000
2. Create by calling the Matrix function:
The commonly used matrix creation functions are as follows:
Rand (M,N) —————— create a random matrix of M row n columns
Eye (m,n) ——————-create unit matrix for m row n columns
Ones (M,n) ————— – Creates a full 1 matrix of M row n columns
Zeros (m,n) ————— – Creates a full 0 matrix of M row n columns
In addition, there are sparse matrices, diagonal matrices, Rubik's matrix functions, and so on, not one example.
>> rand (3,4)
ans =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
>> Eye (3,3)
ans =
1 0 0
0 1 0
0 0 1
>> ones (2,3)
ans =
1 1 1
1 1 1
>> zeros (3,2)
ans =
0 0
0 0
0 0
3. Modification of the elements of the matrix
The elements of the matrix can be modified by index, such as creating a 3x3 random matrix A, now to modify the third row in the matrix of the 3rd column of the element, change it to 8, it can be modified by a (3,3) = 8来 (note that the MATLAB column subscript is starting from 0, This is different from other high-level languages), as follows:
>> A=rand (3,3)
a =
0.9572 0.1419 0.7922
0.4854 0.4218 0.9595
0.8003 0.9157 0.6557
>> A (3,3) =8
a =
0.9572 0.1419 0.7922
0.4854 0.4218 0.9595
0.8003 0.9157 8.0000
2. Operation of the Matrix
There are many types of matrix operations, and the common ones are:
The matrix of the + 、-、 X, power, transpose, left and right upside-down, inverse matrix, the diagonal matrix, matrix root, the eigenvalues and diagonal matrix of the matrix, as well as array operations of matrices and so on. Here is a brief introduction:
1. Matrix's + 、-、 x, exponentiation operation
(1) +,-arithmetic
Matrix of the +,-operation, requires that the matrix A and B to participate in the operation must have the same number of rows and columns, in the operation, A and b two matrix corresponding to the elements of the respective position of the addition of the final result, if it is a separate scalar C and the matrix is added, the result is the number of the matrix at each position of , as detailed below.
Two matrices A and b add:
>> A=rand (2,2)
A =
0.0357 0.9340
0.8491 0.6787
>> b=ones (2,2)
B =
1 1
1 1
>> a+b
ans =
1.0357 1.9340
1.8491 1.6787
Matrix A and a scalar 5 add:
>> a
a =
0.0357 0.9340
0.8491 0.6787
>> a+5
ans =
5.0357 5.9340
5.8491 5.6787
(2) Matrix X operation
When matrix A and B are multiplied, the number of columns of a matrix must be equal to the number of rows in the B matrix to multiply. Its mathematical process and meaning do not repeat.
>> a
a =
0.0357 0.9340
0.8491 0.6787
>> b
b =
1 1
1 1
>> a*b
ans =
0.9697 0.9697
1.5279 1.5279
(3) Matrix exponentiation operation
The exponentiation of a matrix can be expressed in a^b, if a is a representation of a matrix, B represents a scalar, then a matrix is multiplied by a B matrix, and if both A and B represent matrices, the operation is meaningless.
>> a
a =
0.0357 0.9340
0.8491 0.6787
>> b=2
b =
2
> > a^b
ans =
0.7944 0.6673
0.6067 1.2538
2. Array operations for matrices
Array of matrices, with the band "." Operators to perform operations such as ". *", "./", ". ^" And so on. Array operations of matrices require that arrays of matrices A and B must have the same dimension size, otherwise they cannot be calculated.
The result of the operation of the corresponding position of the number of A and b corresponding to the *,/, ^ and other operations, the result returns an array of the same size as a and B.
A=rand (2,3)
A =
0.7547 0.6797 0.1626
0.2760 0.6551 0.1190
>> b=rand (2,3)
B =
0.4984 0.3404 0.2238
0.9597 0.5853 0.7513
>> a.*b
ans =
0.3761 0.2314 0.0364
0.2649 0.3834 0.0894
>> a./b
ans =
1.5143 1.9969 0.7266
0.2876 1.1193 0.1584
>> c=[1 2 0;1 1 1]
c =
1 2 0
1 1 1
>> a.^c
ans =
0.7547 0.4620 1.0000
0.2760 0.6551 0.1190
3. Other operations of the matrix
The matrix operation in MATLAB, in addition to the above common operations, there are many operations, these operations are mostly through a specific function to achieve.
INV () —————— the inverse matrix of the matrix
Eig () —————-the characteristic matrix of the matrix
Det () —————— The determinant value of the matrix
sqrt () ————— – Finding the root of a matrix
Diag () ————— – Finding the diagonal matrix
' ——————— seek the transpose of the matrix
Rot90 () —————-flips the matrix 90 degrees
FLIPLR () ————— – Flip left and right
Flipud () —————-upside down
Tril () —————— – extracting the main lower triangle
Triu () —————— – Extracting the Lord Triangle
Reshape () ————— Realizing the variable dimension of matrix
>> A=magic (3) A = 8 1 6 3 5 7 4 9 2 >> inv (a) ans = 0.1472- 0.1444 0.0639-0.0611 0.0222 0.1056-0.0194 0.1889-0.1028 >> det (a) ans = -360 >> D IAG (a) ans = 8 5 2 >> sqrt (a) ans = 2.8284 1.0000 2.4495 1.7321 2.2361 2.645 8 2.0000 3.0000 1.4142 >> Eig (a) ans = 15.0000 4.8990-4.8990 >> a ' ans = 8
3 4 1 5 9 6 7 2 >> A A = 8 1 6 3 5 7 4 9 2 >> Rot90 (a) ans = 6 7 2 1 5 9 8 3 4 >> FLIPLR (a) ans = 6 1 8 7 5 3 2 9 4 >> flipud (a) ans = 4 9 2 3 5 7 8
1 6 >> Tril (a) ans = 8 0 0 3 5 0 4 9 2 >> Triu (a) ans = 8 1 6 0 5 7 0 0 2 >> reshape (a,9,1) ans = 8 3 4 1 5 9 6 7
2
3. Operation of the polynomial
In Matlab, the polynomial is represented by a vector consisting of its coefficients, and the polynomial higher order coefficients to the lower order coefficients are represented from left to right. For example, 4x^3+5x^2+6x+5 can be expressed as P=[4 5 6 5]. In order to be able to express the representation of this vectorization polynomial in a familiar polynomial form, such as 4x^3+5x^2+6x+5, the corresponding vector can be expressed as a familiar mathematical expression using POLY2STR (p, ' X '). where p represents the vector of the polynomial, ' x ' indicates the unknowns we are going to adopt. When a polynomial expression is to be rooted, the roots (p) can be used to find the root of the polynomial and return the root result. You can revert to the original function using poly (roots) from the root obtained from the roots () function. MATLAB also provides the derivative of the Polyder (p) function to find the polynomial.
>> p=[4 5 6 5]
p =
4 5 6 5
>> poly2str (P, ' x ')
ans =
4 x^3 + 5 x^2 + 6 x + 5
>> roots (P)
ans =
-1.0000
-0.1250 + 1.1110i
-0.1250-1.1110i
>> poly (Roots (p) )
ans =
1.0000 1.2500 1.5000 1.2500
>> s=polyder (p)
s = 10 6
>> poly2str (P, ' x ')
ans =
4 x^3 + 5 x^2 + 6 x + 5
>> poly2str (S, ' x ')
ans =
12 x^2 + x + 6
4. Linear interpolation
In practical applications, it is often necessary to estimate the correlation between these discrete points according to some discrete data points, in order to use this law to predict the subsequent data, this operation is called fitting, MATLAB provides Polyfit (X,y,count) to do, X, Y represents the X-and y-coordinate data to fit, and the count represents a polynomial that uses the highest number of times to fit the data. Polyval (p,x) provides a function for the calculation of the fitted function. Fitting operation is very convenient, but sometimes at two intervals between the points, if you want to get two points in accordance with other segments of the polynomial operation of the data, it is necessary to perform interpolation, in the intermittent two points to simulate the corresponding function relationship, thereby inferring the data between the discontinuity. MATLAB includes INTERP1, INTERP2, spline three kinds of interpolation operations, the first two represent secondary interpolation, the last one represents 3 times spline interpolation.
Use known data to fit:
>> x0=0:0.1:1;
>> y0=[-.447 1.978 3.11 5.25 5.02 4.66 4.01 4.58 3.45 5.35 9.22];
>> P=polyfit (x0,y0,3);
>> x=0:0.01:1;
>> Y=polyval (p,x);
>> plot (x, Y, '-B ', x0,y0, ' ro ')