MATLAB Practical Combat | Finding the matrix index, the meaning of predefined variables i and j, and the trapezoidal integration method

 

01. Find matrix indexPractical application

[Example 1]Find the matrix index according to the power series expansion of the matrix index.

 Suppose until time. In order to determine whether F is small, the concept of matrix norm can be used. One definition of the norm of matrix A is

picture

. Use the norm(A,1) function in MATLAB to calculate. Therefore, when norm(F,1)=0, F is considered to be very small and the execution of the loop should be exited. The procedure is as follows:

X = input('Enter X:')
E= zeros(size(X));
E = eye( size(X));
n=1;
while norm(E,1)> 0
E=E+F;
end
E
E=E*X/n;
n=n+1;
expm(X)  //调用 MATLAB 矩阵指数函数求矩阵指数

The results of running the program are as follows:

 

 The running results show that the program running results are consistent with the results of the MATLAB matrix exponential function expm(X). This program involves matrix operations, which may not be familiar to beginners. If you can analyze the execution process of the program, it will be beneficial to understand the programming ideas. In addition, we know that the commutative law of matrix multiplication does not hold, but here we would like to ask you to analyze whether the statement F=F*X/n in the program can be written as F= X * F /n, and why?

02. The meaning of predefined variables i and j ---Variables and assignment

1. Variable naming

In MATLAB, a variable name is a sequence of characters starting with a letter, followed by letters, numbers, or underscores, up to a maximum of 63 characters. For example, x, x_1, and x2 are all legal variable names. In MATLAB, variable names are case-sensitive, so score, Score, and SCORE represent three different variables. In addition, you cannot use MATLAB keywords as variable names, such as if, end, and for.

Caution/

When defining variables, avoid creating variables with the same name as predefined variables and functions, such as i, j, power, int16, format, path, etc. Generally, variable names take precedence over function names. If the created variable uses the name of a function, it may cause unexpected behavior in the calculation process and calculation results. You can use the exist or which function to check whether the intended name is already in use. The exist function returns 0 if there is no variable, function, or M-file with the same name as the intended name, otherwise it returns a nonzero value. For example:

>> exist power
ans =
5
>> exist Power
ans =
0

The which function is used to locate functions and files. If the function or file exists, its full path is displayed. For example:

>> which power
built-in (C:Program Files MATLAB\R2022a toolbox matlab ops) //char power
> which powerl

未找到power1'

 

2. Assignment statement

MATLAB assignment statements have two formats:

variable = expression

expression

Among them, expressions are formulas that use operators to connect related operands. When the first statement is executed, MATLAB assigns the value of the expression on the right to the variable on the left; when the second statement is executed, the value of the expression is assigned to MATLAB's predefined variable ans. See the execution results of the following commands.

 Normally, the operation results are displayed in the command line window. If you add a semicolon at the end of the command, MATLAB only performs the assignment operation and does not display the result of the operation. If the result of the operation is a large matrix or the operation result is not needed, you can add a semicolon at the end of the command.

【Example 2】 When x=π/2, y=1+3i, find the expression

picture

value.

Enter the commands respectively in the MATLAB command line window:

>> x= pi/2;
>> y=1+3i;
>> z= exp(2) * cos(x+ y)/(x+ sqrt(log(abs(y-1)))) //计算表达式的值
z = 
-23.9018 -15.2713i

3. Predefined variables

In MATLAB, some system-defined special variables are provided, which are called predefined variables. Table 1 lists some commonly used predefined variables. Predefined variables have specific meanings. When using them, try to avoid reassigning values ​​to these variables. However, there is no error in assigning values ​​to them. It will just overwrite the original value. After clearing it with the clear command, the original value can be restored.

■ Table 1 Commonly used predefined variables

 MATLAB provides the isfinite function to determine whether the data object has a finite value, the isinf function to determine whether the data object has an infinite value, and the isnan function to determine whether the data object contains NaN values.

Caution/

MATLAB predefined variables have specific meanings, and you should try to avoid reassigning these variables when using them. Take i or j as an example. In MATLAB, i and j represent imaginary units. If i or j are reassigned, the original definition of the imaginary unit will be overwritten, which may lead to some very hidden errors. For example, due to habit, i or j is usually used as a loop variable in the program. At this time, if there is a complex number operation, an error will occur. Therefore, do not use i or j as the loop variable name unless it is confirmed that it will not be used with the loop variable during the running of the program. When dealing with complex numbers, you may need to use a complex number notation like 7+5i instead of 7+5*i. The former is a complex constant, and the latter is an expression, that is, i is regarded as an operand and participates in the operation of the expression. You can also use j to represent a complex number when using i as a loop variable.

03. Trapezoidal integration method

In MATLAB, the function trapz is provided to calculate definite integrals using the trapezoidal method for discrete data defined in tabular form. The function calling format is as follows:

(1) T=trapz(Y). This format is used to find uniformly spaced integrals. Usually, the input parameter Y is a vector, and the approximate integral of Y is calculated using unit spacing (that is, the spacing is 1). If Y is a matrix, the output parameter T is a row vector, and each element of T stores the integration result of each column of Y respectively. For example:

If the distance is not 1, for example, find

picture

, you can use the following command:

 

(2) T=trapz(X,Y). This format is used to find integrals over non-uniform spacing. Usually, the input parameters X and Y are two vectors of equal length. X and Y satisfy the functional relationship Y = f(X). According to the data point spacing specified by X, Y is integrated. If X is a vector with m elements and Y is an m×n matrix, then the output parameter T is a vector with n elements, and each element of T stores the integration result of each column of Y respectively.

[Example 3]A rocket is launched from the ground. Table 2 records the acceleration of the rocket from 0 to 80s. Find the speed of the rocket at 80 seconds.

■ Table 2 Rocket launch acceleration

 

Assuming the speed is v(t), then

picture

, thus converting the problem into an integral problem. The command is as follows:

picture

Guess you like

Origin blog.csdn.net/qq_41640218/article/details/134796187