1. Basic Knowledge of MATLAB 1-1. Basic Operations and Functions In MATLAB, to perform basic mathematical operations, simply type the expression directly after the prompt (>>), and press Enter. For example: >> (5*2+1.3-0.8)*10/25 ans = 4.2000 MATLAB will store the result directly in a variable called ans, representing the answer after the computation and displaying its value on the screen. Tip: “>>” is the prompt symbol in MATLAB, but in the PC Chinese Windows system, due to different encoding methods, this prompt may often disappear, but this does not affect MATLAB’s computation results. We can also assign the result of the above expression to another variable x: x = (5*2+1.3-0.8)*10^2/25 x = 42 At this point, MATLAB will directly display the value of x. From the above example, we can see that MATLAB recognizes all commonly used mathematical operators such as addition (+), subtraction (-), multiplication (*), and division (/), as well as exponentiation (^). Tip: MATLAB stores all variables in double format, so there is no need for variable declaration. MATLAB also automatically manages memory usage and recovery, unlike C language, where the user must specify each one. These features make MATLAB easy to learn and use, allowing users to focus on writing programs without being distracted by software issues. If you do not want MATLAB to display the computation results each time, simply add a semicolon (;) at the end of the expression, as in the following example: y = sin(10)*exp(-0.3*4^2); To display the value of the variable y, just type y: >> y y = -0.0045 In the above example, sin is the sine function, exp is the exponential function, both of which are commonly used mathematical functions in MATLAB. The following table lists the commonly used basic mathematical functions and trigonometric functions in MATLAB: Summary: Commonly used basic mathematical functions in MATLAB abs(x): Absolute value of a scalar or length of a vector angle(z): Phase angle of complex number z sqrt(x): Square root real(z): Real part of complex number z imag(z): Imaginary part of complex number z conj(z): Conjugate of complex number z round(x): Round to the nearest integer fix(x): Truncate to the nearest integer regardless of sign floor(x): Floor function, truncating positive decimals to the nearest integer ceil(x): Ceiling function, rounding up positive decimals to the nearest integer rat(x): Convert real number x to a fraction representation rats(x): Convert real number x to a polynomial fraction expansion sign(x): Sign function (Signum function). When x < 0, sign(x) = -1; When x = 0, sign(x) = 0; When x > 0, sign(x) = 1. > Summary: Commonly used trigonometric functions in MATLAB sin(x): Sine function cos(x): Cosine function tan(x): Tangent function asin(x): Inverse sine function acos(x): Inverse cosine function atan(x): Inverse tangent function atan2(x,y): Four-quadrant inverse tangent function sinh(x): Hyperbolic sine function cosh(x): Hyperbolic cosine function tanh(x): Hyperbolic tangent function asinh(x): Inverse hyperbolic sine function acosh(x): Inverse hyperbolic cosine function atanh(x): Inverse hyperbolic tangent function Variables can also be used to store vectors or matrices and perform various operations, as in the following example of row vector operations: x = [1 3 5 2]; y = 2*x + 1 y = 3 7 11 5 Tip: Rules for naming variables 1. The first letter must be an English letter 2. No spaces are allowed between letters 3. A maximum of 19 letters is allowed; MATLAB will ignore any excess letters We can freely change, add, or delete elements of a vector: y(3) = 2 % Change the third element y = 3 7 2 5 y(6) = 10 % Add the sixth element y = 3 7 2 5 0 10 y(4) = [] % Delete the fourth element, y = 3 7 2 0 10 In the above example, MATLAB will ignore all text after the percentage symbol (%), so the text after the percentage can be considered as comments. MATLAB can also extract an element or a part of a vector for computation: x(2)*3 + y(4) % Extract the second element of x and the fourth element of y for computation ans = 9 y(2:4)-1 % Extract the second to fourth elements of y for computation ans = 6 1 -1 In the above example, 2:4 represents a vector consisting of 2, 3, and 4. If you have questions about the usage of MATLAB functions, you can always use help to seek online support (on-line help): help linspace Summary: MATLAB’s query command help: Used to query the usage of known commands. For example, if you know inv is used to calculate the inverse matrix, typing help inv will provide information about the inv command usage. (Type help help to see the usage of help, please try it out!) lookfor: Used to search for unknown commands. For example, to find the command for calculating the inverse matrix, you can type lookfor inverse, and MATLAB will list all commands related to the keyword inverse. After finding the required command, you can use help to further find out its usage.