MATLAB: [6] Numerical Calculus and Equation Solving

Table of contents

6.1 Numerical differentiation and numerical integration

6.2 Solving Linear Equations

6.3 Nonlinear Equation Solving and Function Extremum Calculation

6.4 Numerical solution of ordinary differential equations


6.1 Numerical differentiation and numerical integration

numerical differentiation

  • In calculus, the x_0derivative of any function f(x) at a point is defined by a limit, so the difference quotient can be used to approximate the derivative at x_0a point.

  •  Implementation of Numerical Differentiation

dx = diff(x) calculates the forward difference of vector x, dx(i)=x(i+1)-x(i)

dx = diff(x, n) calculates the n-order forward difference of the vector x

dx = diff(A, n, dim) Calculate the n-order difference of the matrix, when dim=1 (default state), calculate the difference by column, dim=2, calculate the difference by row

 Numerical integration

  • Often use the Newton-Leibniz formula to find the integral of the function, or use the numerical solution to solve the approximate value of the definite integral. The basic idea is to decompose the interval into n sub-intervals and sum them

  •  Realization of Numerical Integration

[l, n] = quad(filename, a, b, tol, trace ) based on the adaptive Simpson method

[l, n] = quadl(filename, a, b, tol, trace ) based on adaptive Simpson method

Among them, filename is the name of the integrand; a and b are the upper and lower limits of the definite integral (the integral limit must be finite ), tol is used to control the integral situation, it is taken by default tol=10^{-6}, trace controls whether to display the integral process, if it is not 0 If it is set to 0, it will not be displayed (default), and the return parameter I is the value of the definite integral, and n is the number of calls of the integrand function.

l = integral(filename, a, b ) based on the global adaptive integration method

Among them, filename is the name of the integrand; a and b are the upper limit and lower limit of the definite integral (the integral limit can be infinite ), and the return parameter I is the value of the definite integral

[l, err] = quadgk(filename, a, b) based on the adaptive Gauss-Kronrod method

Among them, err returns the approximate error range, and the meaning and usage of other parameters are the same as the quad function. The upper and lower limits of the integral can be real infinite (-Inf or Inf), or complex numbers. If the upper and lower limits of the integral are complex numbers, the quadgk function is in the complex plane Find points.

l = trapz(x, y) based on trapezoidal integration

Among them, the vector x, y defines the functional relationship y=f(x)

  • Numerical Solution of Multiple Definite Integrals

\int ^d_c\int ^b_af(x,y)dxdy

l = integral2(filename, a, b, c, d)

l = quad2d(filename, a, b, c, d)

l = dblquad(filename, a, b, c, d, tol) 

 \int^f_e\int ^d_c\int ^b_af(x,y,z)dxdydz

l = integral3(filename, a, b, c, d, e, f)

l = triplequad(filename, a, b, c, d, e, f, tol)

6.2 Solving Linear Equations

direct method

  • Direct solution using left division operator

Ax=b, so x=A\b

If the right-hand term is a matrix of n*m, then x=A\b can simultaneously obtain the numerical solution x of m linear equations with the same sparse matrix A, and x is a matrix of n*m. That is, x(:,j)= A\b(:,j),j=1,2,...,m

A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
x=A\b
  •  Solving Systems of Linear Equations Using Matrix Factorization

Mainly divided into the following three decomposition methods

  1. LU decomposition (take this as an example)
  2. QR decomposition
  3. Cholesky decomposition

The basic idea is as follows:

decomposition function

[L, U]=lu(A) Generate an upper triangular matrix U and a transformed lower triangular matrix L, so that it satisfies A=LU. Note that the matrix A here must be a square matrix, and finally x=U\( L\b)

[L,U,P]=lu(A) Generate an upper triangular matrix U and a lower triangular matrix and a permutation matrix P, so that it satisfies PA=LU, and finally x=U\(L\P*b)

A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
[L,U]=lu(A);
x=U\(L\b)

Iterative method

  • Jacobian iterative method

 If x converges, it is the value solved.

function [y,n]=jacobi(A,b,x0,ep) 
D=diag(diag(A));
L=-tril(A,-1);
U=-triu(A,1);
B=D\(L+U);
f=D\b;
y=B*x0+f;
n=1;
while norm(y-x0)>=ep
    x0=y;
    y=B*x0+f;
    n=n+1;
end

 x0 is the initial value set, n is the number of iterations, the same below

  • Gauss-Seidel iteration method (higher precision)
function [y,n]=gauseidel(A,b,x0,ep)
D=diag(diag(A));
L=-tril(A,-1); 
U=-triu(A,1);
B=(D-L)\U;
f=(D-L)\b;
y=B*x0+f;
n=1;
while norm(y-x0)>=ep
    x0=y;
    y=B*x0+f;
    n=n+1;
end

6.3 Nonlinear Equation Solving and Function Extremum Calculation

Nonlinear Equation Solving

x=fzero(filename,x0) where filename is the function expression on the left side of the equation to be solved, and x0 is the initial value

x=fsolve(filename,x0,option) option is an optimization parameter, which can be set to optimset('Display','off'), which can solve nonlinear equations

f=@(x) [sin(x(1))+x(2)+x(3)^2*exp(x(1)),x(1)+x(2)+x(3),x(1)*x(2)*x(3)];
f([1,1,1])
x=fsolve(f,[1,1,1],optimset('Display','off'))
f(x)

function extremum calculation

  • unconstrained optimization problem

[xmin, fmin] = fminbnd(filename, x1, x2, option) 

[xmin, fmin] = fminsearch(filename, x0, option) 

[xmin, fmin] = fminunc(filename, x0, option)  

Among them, x1 and x2 are the left and right boundaries of the research interval, and x0 is the initial value representing the extreme point

  • constrained optimization problem
  1. Linear Inequality Constraints
  2. linear equality constraints
  3. Nonlinear Inequality Constraints
  4. nonlinear equality constraints
  5. upper and lower bounds of x

 [xmin, fmin] = fmincon(filename, x0, A, b, Aep, bep, Lbnd, Ubnd, NonF, option)

Among them, xmin, fmin, fiename, x0, and option have the same meaning as the minimum value function. The rest of the parameters are constraints, including linear inequality constraints, linear equality constraints, lower and upper bounds of x, and functions that define nonlinear constraints. If If a constraint does not exist, it is represented by an empty matrix.

6.4 Numerical solution of ordinary differential equations

general concept

  • Find a function y that satisfies y'=f(t, y)

solve function

[t, y] = solver(filename, tspan, y0, option)

  • Among them, t and y respectively give the time vector and the corresponding numerical solution. solver is a function for finding the numerical solution of the ordinary differential return process. filename is the name of the function defining f(t, y), and the function must return a column vector. The form of tspan It is [t0, tf], which means the solution interval, y0 is the initial state column vector, and option is an optional parameter, which is used to set the solution properties.
  • The solver function has a unified naming method, odennxx.nn indicates the order of the left and right methods, and ode45 is commonly used
f=@(t,y) (y^2-t-2)/4/(t+1);
[t,y]=ode23(f,[0,10],2);
y1=sqrt(t+1)+1;
plot(t,y,'b:',t,y1,'r');

Rigid problem

  • Some components of the solution change quickly, some change very slowly, and the difference is huge, which is the so-called rigid problem (Stiff)

Solve the function ode15s, use the same method as above

Guess you like

Origin blog.csdn.net/Alex497259/article/details/104595592