Matlab Algorithm Collection

1.Gaussian Elimination: function (x,det,flag)=Gauss(A,b), where A is the coefficient matrix of the equations; b is the right-hand side; det is the value of the determinant of the coefficient matrix; x is the solution of the equations.

2.Gaussian-Jordan Elimination: function (x,flag)=Gau_Jor(A,b), where flag=’ok’ indicates successful computation.

3.The syntax for plotting function graphs is:

(1).plot——Utilize points to draw the function. For example: plot(x1,y1,x2,y2)

(2).fplot——Requires a function, an interval, and gridf. For example: fplot(‘[x^3+5*x+4]’,[1,2]);grid

(3).ezplot——Just write the function. For example: ezplot(‘x^2+7*sin(x)-30’)

4.Command to find the roots of equations: roots[p]; p=[an,an-1,….a1,a0], which are the coefficients before the unknown variables.

5.Command to find the zero of a function: x1=fzero(‘ff’,x0) or f=inline(‘ff’); x1=fzero(‘f’,x0); x0 indicates the approximate position of a certain point of the function.

6.Method for solving systems of equations: For example: a=’s1′; b=’s2′; c=’s3′; [a,b,c]=solve(a,b,c) or [x,y,z]=solve[‘s1′,’s2′,’s3′,’x’,’y’,’z’].

7.Method for solving ordinary differential equations: f=dsolve(‘D2y-Dy-2′,’D(y)=0′,’y(0)=1’), which indicates that with initial conditions, it is a particular solution; without it, it seeks the general solution. D2y represents the second derivative of y.

LU decomposition method for matrices: function (L,U,flag)=LU_Decom(A), where A is the matrix to be decomposed; L is the unit lower triangular matrix; U is the upper triangular matrix.

8.Square Root Method for matrices: function (L,flag)=chol_factor(A), where L is the lower triangular matrix.

9.Improved Square Root Method (Cyclic Method): function (L,D,flag)=LDL_Decom(A), where L is the lower triangular matrix; D is the diagonal matrix.

10.Command to find the rank of a matrix: c=rank(a), where c is the maximum number of linearly independent rows or columns of a.

11.Writing a matrix: For example: A=[1,2,3;4,5,6] or you can write another set on a new line.

12.Real part of a complex matrix: real(A); Imaginary part: imag(A); Modulus: abs(A); Argument: angle(A)*180/pi.

13.n-order all-ones matrix: ones(n); n-order all-zeros matrix: zeros(n).

14.Command to make a matrix conjugate transpose: For example: A’; to change it to its conjugate matrix format: conj(A); the command for mere transpose of the matrix is: (1). If A is a real matrix, use “‘” to obtain it; if A is a complex matrix, use conj(A’) or conj(A)’ to obtain it.

15.Method to find the inverse of a matrix: For example: b=inv(a), where b is the inverse of a.

16.Method to find any function f(a) of matrix A: For example: sin(a)=funm(a,@sin) or funm(a,’sin’).

17.Creating symbolic variables and expression variables: sym(f).

18.Method for differentiating functions:

Examples: (1). f=’x^3+2*x+5′; dfdx=diff(f) or f1=sym(f); dfdx2=diff(f1).

(2). diff(s) indicates the first derivative of s with respect to the default variable.

(3). diff(s,’v’) indicates the first derivative of s with respect to v

(4). diff(s,n) indicates the n-th derivative of s

(5). diff(s,’v’,n) indicates the n-th derivative of s with respect to v.

19.Method to extract elements from a matrix: A(3,2) represents the element in the 3rd row and 2nd column of the matrix; A(15) represents the 15th element. (Counting from top to bottom, from left to right)

20.Method to delete matrix elements: For example: A(:,3)=[] indicates deleting the 3rd column elements of the matrix; A(15)=[] indicates deleting the 15th element.

21.Method to indicate the size of a matrix: Input size(A), which will give you the number of rows and columns of the matrix.

22.Method to represent numbers (points): a=1:2:15, representing from 1 to 15, with a distance of 2 between each number (point).

23.Given the polynomial coefficient vector, derive the polynomial: For example p1=[1 8 0 0 -10], p2=[2 -1 3], using poly2str(p1,’x’), poly2str(p2,’x’).

24.Method to find the product of polynomials: For example f=conv(p1,p2), where p1 and p2 are coefficient vectors.

25.Method for polynomial division: For example [Q,r]=deconv(p1,p2), where p1 and p2 are coefficient vectors. The quotient of p1 and p2 is stored in Q, and the remainder in r.

26.Method to find the derivative of a polynomial:

(1). To find the derivative polynomial of polynomial p: p=polyder(p);

(2). To find the derivative polynomial of the product of polynomial p and polynomial Q: p=polyder(p,Q);

(3). To find the derivative polynomial of the division of polynomial p and polynomial Q, the numerator of the derivative is stored in p, and the denominator in q: [p,q]=polyder(p,Q).

27.(1). Finding the roots of a polynomial: x=roots(p); (2). If all roots of the polynomial are known, constructing the polynomial: p=poly(x); (3). Finding the eigenvalues of a square matrix: eig(A).

28.Method to find the limit of a function: limit(f,x,a) indicates the limit of f(x) as x approaches a; (2). If limit(f,a) defaults to the variable x approaching a; (3). limit(f,x,a,’right’) indicates the limit of variable x approaching a from the right.

29.Finding the solution of variables in equations: Example: solve(‘a*x^2+b*x+c’,b), which gives the value of b.

30.Curve fitting: (1). Based on sample points A and B, generate a quadratic polynomial P, written as: [p,s]=polyfit(A,B,2); (2). Calculate the function value of the fitted polynomial at a given point: Y=polyval(p,x). (Calculating the fitted function value at the corresponding point x)

31.Symbolic integration: (1). int(s) indicates the indefinite integral of s with respect to the default variable; (2). int(s,v) indicates the indefinite integral of s with respect to v; (3). int(s,v,a,b) simultaneously specifies the upper limit a and lower limit b for the definite integral of s over the interval [a,b].

32.Solving algebraic equations: (1). solve(s) solves s with the default variable, e.g. f=sym(‘x^2+3*x+2’); solve(f); (2). solve(s,v) solves s with variable v, e.g. f=sym(‘x^2+b*x+c’); solve(f,’b’).

33.Method for multiple plots in a single window: subplot(m,n,p) this function divides the window into m*n sub-windows, designating the p-th area as the active area, with sub-windows arranged from left to right, top to bottom, e.g., t=0:1:7; y=sin(t); y1=sin(t+1); y2=sin(t+0.5); y3=sin(t+0.25); subplot(2,2,1); plot(t,y); subplot(2,2,2); plot(t,y1); subplot(2,2,3); plot(t,y2); subplot(2,2,4); plot(t,y3).

34.Calling format for matrix decomposition: (1). Triangular decomposition: [L,U]=lu(A) and [L,U,P]=lu(A); (2). Cholesky decomposition: B=chol(A).

35.Iterative methods for solving current systems of equations:

(1). Jacobi: function [x,k,flag,err]=Jacobi(A,b,delta,max1), where A is the coefficient matrix, b is the right-hand side, delta is the precision requirement, default value is le-5, max1 is the maximum number of iterations, default value is 100, k is the number of iterations;

(2). Gauss-Seidel: function [x,k,flag]=Gau_Seid(A,b,delta,max1);

(3). Successive Over-Relaxation Iteration Method: function [x,k,flag]=SOR(A,b,ep,w,max1), where w is the over-relaxation factor, default is 1, otherwise give you the relaxation factor number.

36.Command to find the determinant of a square matrix: e.g., A=[1,2,3;4,5,6], det(A).

37.Given a matrix, find its characteristic polynomial: e.g., A=[1,2,3;4,5,6]; P=poly(A), obtain the polynomial coefficient vector, input poly2str(P,’x’) to get the polynomial.

38.Finding the eigenvalues of a square matrix: e.g., A=[1,2,3;4,5,6]; roots(A) gives the eigenvalues of the matrix.

39.Finding eigenvalues and eigenvectors of a square matrix: e.g., A=[1,2,3;4,5,6]; [x,r]=eig(A) or [x,r]=eig(sym(A)), the latter results will be more accurate.

40.Finding the orthogonal triangular decomposition of a matrix: the calling format is A=[1,2;3,4]; [q,r]=qr(A) also can format rat; [q,r]=qr(A); or [q,r,p]=qr(A), where p is the permutation matrix, satisfying A*p=q*r.

41.Functions to calculate norms and spectral radius of matrices:

(1). When the input parameter A is a vector: norm(A,ex) where ex=1,2,inf,-inf represents the 1-norm, 2-norm, infinity norm, minimum norm (i.e., the minimum absolute value of each component); when the input parameter A is a matrix: ex takes 1,2,fro,inf representing the column norm of matrix A, spectral norm of matrix A, F-norm, row norm of matrix A;

(2). The spectral radius of square matrix A is the absolute value of the maximum eigenvalue of A, i.e., max(abs(eig(A))).

42.General method for interpolation:

(1). Lagrange: function[c,l]=lagran(x,y), where x is a vector composed of n nodes’ x-coordinates, y is a vector composed of y-coordinates, c is a vector of coefficients of the interpolation function;

(2). Newton’s interpolation method: function[c,d]=newpoly(x,y);

(3). Hermite interpolation method: function yi=Hermite(x,y,ydot,xi), where X is a vector of all interpolation nodes, y is a vector of function values at the interpolation nodes, ydot is a vector of derivative values at the interpolation nodes, xi is a scalar, independent variable x, yi is the estimated function value at xi;

(4). Hermite cubic interpolation method: function yi=Hermite_wise(x,y,ydot,xi); (5). Cubic spline interpolation: function S=csfit(x,y,dx0,dxn), where dx0 and dxn are the values of S’s derivative at x0 and xn respectively.

43.Data curve fitting methods:

(1). Linear least squares method: function c=lspoly(x,y,m), where x is a vector of x-coordinates of data points, y is a vector of y-coordinates, m is the degree of the polynomial, c is a vector of coefficients from high degree to low degree;

(2). Best fit method: function S=squar_approx(a,b,n), where a and b are the endpoints of the interval, n is the best approximation degree, default is 1, S is the best approximation coefficient (arranged by order, from low to high).

44.Calling format for interpolation methods: Yk=interp(x,y,xk,’method’), where method can be (1). nearest – nearest interpolation; (2). linear – linear interpolation; (3). pchip – piecewise cubic interpolation; (4) spline – cubic spline interpolation.

45.Solving ordinary differential equations:

(1). Euler’s formula: function E=Euler_1(f,x0,y0,xn,n)

(2). Backward Euler’s formula: function E2=Euler_2(f,x0,y0,xn,n)

(3). Trapezoidal formula: function E3=Euler_3(f,x0,y0,xn,n)

(4). Improved Euler’s formula: function E4=Euler_4(f,x0,y0,xn,n) (5). Runge-Kutta: function R=rk4(f,a,b,ya,n).

46.Mathematical model for linear programming problems: [x,fval]=linprog(f,A,b,Aeg,beg,lb,ub,x0), where x’s lower bound lb and upper bound ub, when there are no equalities, Aeg=[], beg=[]; functions should be written based on constraints.

47.Non-linear programming problems: (1). Unconstrained non-linear: [x,fval]=fminbnd(f,x1,x2); [x,fval]=fminunc(f,x0); [x,fval]=fmisearch(f,x0) (2). Constrained non-linear: [x,fval]=fmincon(f,x0,A,b,Aeq,beq,lb,ub). (When there are no equalities, Aeg=[], beg=[])

48.Quadratic programming problems: [x,fval]=quadprog(H,f,A,b,Aeq,beq,lb,ub,x0). (When there are no equalities, Aeg=[], beg=[])

49.Max-min method: [x,fval]=fminimax(f,x0,A,b,Aeq,beq,lb,ub); using M-file, f=myfun(x), [x,fval]=fminimax(‘myfun’,x0). (When there are no equalities, Aeg=[], beg=[]; if there are equalities, A=[], b=[])

50.Multi-objective programming: [x,fval]=fgoalattain(f,x0,goal,weight,A,b,Aeq,beq,lb,ub), which can also use f=myfun(x), f(1)=?, f(2)=?, [x,fval]=fgoalattain(‘myfun’,x0,goal,weight,A,b). (When there are no equalities, Aeg=[], beg=[]; if there are equalities, A=[], b=[])

Leave a Comment