Numerical Analysis
2.1 Differentiation
The diff function is used to compute the differentiation of a function, with the following four related syntaxes:
diff(f) returns the first derivative of f with respect to the predefined independent variable.
diff(f, ‘t’) returns the first derivative of f with respect to the independent variable t.
diff(f, n) returns the n-th derivative of f with respect to the predefined independent variable.
diff(f, ‘t’, n) returns the n-th derivative of f with respect to the independent variable t.
The numerical differentiation function is also diff, so this function determines whether to perform numerical or symbolic differentiation based on the input arguments. If the argument is a vector, it performs numerical differentiation; if the argument is a symbolic expression, it performs symbolic differentiation.
First, define the following three equations, then calculate their derivatives:
>> S1 = ‘6*x^3-4*x^2+b*x-5’;
>> S2 = ‘sin(a)’;
>> S3 = ‘(1 – t^3)/(1 + t^4)’;
>> diff(S1)
ans = 18*x^2 – 8*x + b
>> diff(S1, 2)
ans = 36*x – 8
>> diff(S1, ‘b’)
ans = x
>> diff(S2)
ans =
cos(a)
>> diff(S3)
ans = -3*t^2/(1 + t^4) – 4*(1 – t^3)/(1 + t^4)^2*t^3
>> simplify(diff(S3))
ans = t^2*(-3 + t^4 – 4*t)/(1 + t^4)^2
2.2 Integration
The int function is used to compute the integral of a function. This function seeks a symbolic expression F such that diff(F) = f. If the analytical form of the integral does not exist or MATLAB cannot find it, int returns the original input symbolic expression. The related syntax includes the following four:
int(f) returns the integral of f with respect to the predefined independent variable.
int(f, ‘t’) returns the integral of f with respect to the independent variable t.
int(f, a, b) returns the integral of f with respect to the predefined independent variable over the interval [a, b], where a and b are numerical values.
int(f, ‘t’, a, b) returns the integral of f with respect to the independent variable t over the interval [a, b], where a and b are numerical values.
int(f, ‘m’, ‘n’) returns the integral of f with respect to the predefined variable over the interval [m, n], where m and n are symbolic expressions.
Let’s demonstrate a few examples:
>> S1 = ‘6*x^3-4*x^2+b*x-5’;
>> S2 = ‘sin(a)’;
>> S3 = ‘sqrt(x)’;
>> int(S1)
ans = 3/2*x^4 – 4/3*x^3 + 1/2*b*x^2 – 5*x
>> int(S2)
ans = -cos(a)
>> int(S3)
ans = 2/3*x^(3/2)
>> int(S3, ‘a’, ‘b’)
ans = 2/3*b^(3/2) – 2/3*a^(3/2)
>> int(S3, 0.5, 0.6)
ans = 2/25*15^(1/2) – 1/6*2^(1/2)
>> numeric(int(S3, 0.5, 0.6)) % The numeric function can calculate the value of the integral
ans = 0.0741
2.3 Solving Ordinary Differential Equations
The syntax for solving ordinary differential equations in MATLAB is dsolve(‘equation’, ‘condition’), where equation represents the ordinary differential equation, i.e., y’ = g(x, y), and must use Dy to represent the first derivative y’ and D2y to represent the second derivative y”. The condition represents the initial conditions.
Assume there are the following three first-order ordinary differential equations and their initial conditions:
y’ = 3x^2, y(2) = 0.5
y’ = 2*x*cos(y)^2, y(0) = 0.25
y’ = 3y + exp(2x), y(0) = 3
The corresponding symbolic expressions for the above ordinary differential equations are:
>> soln_1 = dsolve(‘Dy = 3*x^2’, ‘y(2) = 0.5’)
ans = x^3 – 7.500000000000000
>> ezplot(soln_1, [2, 4]) % See what this function looks like
>> soln_2 = dsolve(‘Dy = 2*x*cos(y)^2’, ‘y(0) = pi/4’)
ans = atan(x^2 + 1)
>> soln_3 = dsolve(‘Dy = 3*y + exp(2*x)’, ‘y(0) = 3’)
ans = -exp(2*x) + 4*exp(3*x)
2.4 Finding Real Roots of Nonlinear Equations
To find the roots of any equation, there are three steps:
First, define the equation. Note that the equation must be arranged in the form f(x) = 0. For example, if the equation is sin(x) = 3, it should be expressed as f(x) = sin(x) – 3. You can define the equation in an m-file.
Substitute appropriate ranges for x, and plot y(x) to understand what the equation looks like.
From the graph, determine where y(x) intersects the x-axis near (x0), and use the syntax fzero(‘function’, x0) to find the root near x0, where function is the name of the function defined earlier. If the function graph shows more than one root, substitute another x0 near the root to find the next root.
Here are a few examples to illustrate how to find their roots:
Example 1, the equation is
sin(x) = 0
We know the roots of the above equation, and the root-finding method is as follows:
>> r = fzero(‘sin’, 3) % Since sin(x) is a built-in function, its name is sin, so it does not need to be defined. Choose to find the root near x = 3
r = 3.1416
>> r = fzero(‘sin’, 6) % Choose to find the root near x = 6
r = 6.2832
Example 2, the equation is the built-in function humps in MATLAB. We do not need to know the form of this equation, but we can plot it and find the location of the roots. The root-finding method is as follows:
>> x = linspace(-2, 3);
>> y = humps(x);
>> plot(x, y), grid % From the graph, we can see there are two roots near 0 and 1
>> r = fzero(‘humps’, 1.2)
r = 1.2995
Example 3, the equation is y = x.^3 – 2*x – 5
This equation is actually a polynomial. We can explain that in addition to using the roots function to find its roots, we can also use the method introduced in this section to find the roots, noting that the two methods have different solutions and results. The root-finding method is as follows:
% m-function, f_1.m
function y = f_1(x) % Define f_1.m function
y = x.^3 – 2*x – 5;
>> x = linspace(-2, 3);
>> y = f_1(x);
>> plot(x, y), grid % From the graph, we can see there are two roots near 2 and -1
>> r = fzero(‘f_1’, 2); % Determine the root near 2
r = 2.0946
>> p = [1 0 -2 -5]
>> r = roots(p) % Verify by finding the roots of the polynomial
r =
2.0946
-1.0473 + 1.1359i
-1.0473 – 1.1359i
2.5 Solving Linear Algebraic Equations (Systems)
We are accustomed to expressing the above system of equations in matrix form as follows:
AX = B
where A represents the coefficients of the equations on the left side, X represents the unknowns to be solved, and B represents the known terms on the right side of the equation.
To solve the above system of equations, we can use the left division operation \ to perform the calculation, that is, X = A\B.
If we rewrite the original equation as XA = B
where A represents the coefficients of the equations on the left side, X represents the unknowns to be solved, and B represents the known terms on the right side of the equation.
Note that in the above equation, X and B have been rewritten as column vectors, and A is actually the transpose of A in the previous equation. The above equation X can be solved by right division /, that is, X = B/A.
If we solve AX = B using the inverse matrix operation, we have X = inv(A)*B, or rewritten as XA = B, then X = B*inv(A).
We will directly illustrate the usage of these three operations with the following example:
>> A = [3 2 -1; -1 3 2; 1 -1 -1]; % Enter the coefficients on the left side of the equation
>> B = [10 5 -1]’; % Enter the known terms on the right side of the equation, B needs to be transposed
>> X = A\B % First solve using left division
X = % Note X is a row vector
-2
5
6
>> C = A*X % Verify if the solution is correct
C = % C = B
10
5
-1
>> A = A’; % Transpose A first
>> B = [10 5 -1];
>> X = B/A % The result of solving using right division is the same
X = % Note X is a column vector
10
5
-1
>> X = B*inv(A); % You can also solve using the inverse matrix operation
Note: This article is reproduced from Zhao Yue’s WeChat public platform.
To facilitate academic discussions among researchers, Yan Zhi Cheng Li has also created its own QQ group. Group 1: Full; Group 2: Full; Group 3: 585629919. Everyone is welcome to join for intense academic discussions!
This article is copyrighted by Yan Zhi Cheng Li. Please contact us via QQ for reprints. Unauthorized reproduction is prohibited. Thank you!
Long press the image below to identify the QR code in the image or search for WeChat ID rationalscience to easily follow us. Thank you!
