Permanent variables commonly used in MATLAB.
Ans: The default variable name for the computed result.
I j: basic imaginary Units.
EPS: Floating point of the system (F10A9BG OHT):
INF: Infinitely large, example 1/0
Nan nan: Non-numeric (n Voyage a nmnb Xie)
Pi: Pi N (n=3. 1415926.. )。
Realmax: The maximum value that the system can represent.
Realmin: The minimum value that the system can represent,
Nargin: Number of input parameters for the function:
Nargout: Most of the output of a function
All operations of ①matlab are defined in the plural city. For the square root problem operation, only the solution in the first quadrant is returned.
The ⑦matlab is left oblique/and right/to denote "left and right divide" operations respectively. For scalar operations, there is no difference between the two: but for matrix operations, they will produce different results.
expressions and operations of polynomial
P (x) =x^3-3x-5 can be expressed as p=[1 0–3 5], x=5 value is Plotval (p,5)
You can also ask for vectors: a=[3 4 5],plotval (p,a)
function roots The root roots of the polynomial (p)
P=[1 0-3 5];
R=roots (P)
By the root recombination polynomial poly (root)
Q=poly (R)
Real (q) Sometimes generates virtual roots, when you can extract real roots with real
Conv (A, b) function polynomial multiplication (performs convolution of two arrays)
A=[1 2 3 4];
B=[1 4 9 16];
C=conv (A, B)
The addition and subtraction of polynomial, the lower order polynomial must be filled with the first 0, so that it has the same order as the higher order polynomial
polynomial division [Q, R]=deconv (c, b) denotes b/c q as quotient polynomial, R is the remainder
Derivative of the polynomial Polyder (f)
F=[2 4 5 6 2 1];
S=polyder (f)
Curve fitting of polynomial
X=[1 2 3 4 5];
y=[5.6 40 150 250 498.9];
N-Polynomial fitting poly of P=polyfit (x,y,n) data: polynomial of characteristic polynomial and root set of matrix
X2=1:0.1:5; N takes 1 o'clock, which is the least squares
Y2=polyval (P,X2); Calculates the value of a polynomial (Polyvalm matrix polynomial)
Plot (x, y, ' * ', x2,y2); grid on
Least squares
X=[1 2 3 4 5];
y=[5.6 40 150 250 498.9];
Plot (x, y, ' * '), Lsline
Polynomial interpolation (p158)
YI=INTERP1 (X,y,xi, ' method ') one-dimensional interpolation
(Xi is an independent coordinate vector of interpolated points, which can be an array or a single number.)
method is the choice of interpolation algorithm, including:
Linear (linear interpolation)
Cubic (cubic interpolation)
Spline (three-time spline interpolation)
Nearst (Recent pro interpolation)
For example: population projections
year=1900:10:1900;
NUMBER=[78 91 105 .... of population per 10 years];
x=1900:1:2000;
Y=INTERP1 (year,number,x, ' spline ');
Plot (Year,numeber, ' * ', x, y); grid on
A Vibory leaf transform interpolation is implemented using function interpft to calculate the Fourier transform of a vector with periodic function values
Then use more points for the inverse transformation of the Fourier transform, the function is used as follows: Y=INTERPFT (X,n) where x is a vector with periodic function values, and is an equidistant point, N is the number of return equal spacing points.
Solving the minimum value of a unary function
Y=fminbnd (' Humps ', 0.3,1) humps as a built-in function
Solving the minimum value of multivariate functions
The function fminserch is used to calculate the minimum value of a multivariate function. It can specify a starting vector, not a range. This function returns a vector corresponding to the local minimum function value for this multivariate function.
Texture mapping function
The texture graph function of warp function realizes the display of planar image on three-dimensional surface of space.
Projecting images with file name flowers.tif to cylindrical and star surfaces, respectively
I=imread (' flowers.tif ');
[X,y,z]=cylinder;
Subplot (1,2,1), Warp (x,y,z,i);
[X,y,z]=sphere (50);
Subplot (1,2,2), Warp (x,y,z,i);
Warp (x,y,z,i);
To find the 0 points of the function
To find the function humps 0 points fzero (' humps ', [up]) on the [up] interval;
You can also give an initial value of Fzero (' humps ', 0.9);
For a polynomial, the root roots (' 4*x^3+ ... ') can be obtained directly from the roots;
You can also use solve
C=sym (' C ', ' real ');
X=sym (' x ', ' real ');
S=solve (X^3-x+c)
function Definite integral
Q=quadl (' Humps ', 0, 1) The definite integral of the humps function on the 0 1 interval can also be used quad statement
The double integral first calculates the inner integral, then computes the value of the double integral by the intermediate result of the inner integral, similar to the step integral method in the integral.
Result=dblquad (' Integrnd ', Xin,xmax.,ymin,ymax) Integrnd is the name string for the integrand
Symbolic integral operation Int (f)
The most accurate is the symbolic integration method
Calculate S=∫12[∫01xydx]dy
Syms x y Middle is a space and cannot be a comma
S=int (int (' X^y ', ' X ', 0,1), ' Y ', and up) quotation marks can be omitted
VPA (s) displays the value of s
The double integral of inner integral limit to function
I=∫14[∫√y2 (x2+y2) dx]dy
notation I=VPA (int (' x^2+y^2 ', ' X ', sqrt (y), 2), ' Y ', 1,4)
Differential Operations (diff)
Differentiation is the slope of a function at a point, which is the microscopic nature of the function, so that the integral is insensitive to changes in the shape of the function in a small range, while the differential is sensitive. -A small change of function that easily produces a large change in the slope of the neighboring point. By the dry differential this inherent difficulty. So avoid numerical differentiation as much as possible. In particular, the data obtained from the experiment is differentiated. In this case, it is best to use the least squares curve to fit this data, and then differential the resulting polynomial, or a different method of point data three times spline fitting, and then look for spline differential, but sometimes the differential operation is unavoidable, in MATLAB. Use the function diff juice to calculate the derivative of a vector or matrix (which can also be interpreted as differential).
A=[1 2 3 3 3 7 8 9];
B=diff (a) one-time differential
Bb=diff (a,2) two-time differential
actually diff (a) =[a (2)-A (1), A (3)-A (2),......, A (n)-A (n-1)]
For the differential of the matrix, that is to find the differential of the vector of each column, from the differential value of the vector can judge the monotony of the vector, whether the spacing and whether there are duplicate elements.
Symbolic differential Operations (diff)
Syms x t A
F =cos (A*x)
DF =diff (f) by the rules of Findsym, implicitly specifying the differential of X
Dfa=diff (F, ' a ') specifies the differential of variable a
Dfa=diff (F, ' a ', 3) three differential
The diff function not only works on the scalar, but also on the matrix, the arithmetic rule is to differentiate by the elements of the matrix respectively.
Syms A X
A=[cos (a*x), sin (a*x),-sin (a*x), cos (a*x)];
Da=diff (A)
Differential Equation Dsolve
In MATLAB, the symbol expression contains the letter D to denote the differential operation, d2,d3 corresponding to the second, third derivative, d2y that D2Y/DT2 t default
Y=dsolve (' dy=f (y) ') single equation, single output
[U,v]=dsolve (' Du=f (u,v) ', ' Dv=g (u,v) ') 2 equations, 2 outputs
S=dsolve (' Dx=f (x, y, z) ', ' dy=g (x, y, z) ', ' dz=k (x, y, z) ')
S.x s.y s.z 3 equations, schema array
Dsolve (' dx=-a*x ') Result: C1*exp (-a*t) is not given the initial value, so the result contains parametric
X=dsolve (' dx=-a*x ', ' x (0) =1 ', ' s ') results exp (-a*s) Given the initial value, and the independent variable is set to S
Calculate the gradient of a multivariate function
Fx=gradient (f) F is a one-dimensional numerical gradient in which the vector returns F, and FX corresponds to the differential in the x direction.
[X,y]=meshgrid ( -2:.2:2,-2:.2:2);
Z=x.*exp (-x.^2-y.^2);
[Px,py]=gradient (z,.2,.2);
Contour (z), hold on draw contour line
Quiver (Px,py)
MATLAB String Operations
Creating an expression with the sym command
F=sym (' cos (x) +sin (x) ') or Syms x, F=cos (x) +sin (x)
diff (F) to find its derivative
(You can also use the command F=diff (' cos (x) +cos (y) ')
When a character expression contains more than one variable, only-the variable is an independent variable. If you do not tell matlab which variable is an independent variable, you can ask through the Findsym command
Querying independent variables with the Findsym command
F=sym (' Sin (a*x) +b ')
Findsym (f,1) gives the independent variable (a variable, if 2 is given 2 variables)
Findsym (f) give all the variables
Simplification and substitution of symbolic expressions
Collect function Collect (F,V) indicates that F is represented as a polynomial form of the symbol variable V, that is, about V merge similar terms, v default, the default variable findsym determined by
Syms x y
f=x^2*y+y*x-x^2-2*x+1
Collect (f) Get ( -1+y) *x^2+ (y-2) *x+1
Collect (F,y) get (x+x^2) *y+1 -x^2-2*x
Expand function expand (f) expands F, writes and forms
syms x
Expand ((x-1) ^3) Get x^3-3*x^2+3*x-1 The
Horner function Horner (f) writes F as a mosaic
Syms x
Horner (x^3-6*x^2) gets ( -6+x) *x^2
Factor function Factor (f) converts F to the product of the low-order rational polynomial,
syms x
f=x^3-6*x^2+11*x-6
Factor (f) Get (x-1) * (x-2) * (x-3)
Simplify (f) function synthesis simplification
Simple (f) function in the simplest form
Syms x
F=2*sin (x^2) +cos (3*x)
Simple (f) If you do not want to see intermediate processes, you can z=simple (f) Sometimes use the simplest command two times to obtain a minimalist
If you want to know which simplified command gets the final result, you can add a parameter how
[z,how]= Simple (f)
Substitution of symbolic expressions
Subs (F,new,old)
f= ' A*x^2+b*x+c '
Subs (f, ' t ', ' X ') gets a A * (t) ^2+b* (t) +c Subs is a symbolic function that returns a symbolic variable
subexpr functions Sometimes the symbolic expressions returned by MATLAB are difficult to understand, and with the SUBEXPR function, you can simplify the expression by using a symbolic representation of the repeated occurrences of the sub-formula in the expression.
C=sym (' C ', ' real ');
X=sym (' x ', ' real ');
S=solve (X^3-x+c)
A=SUBEXPR (s) Get sigma = -108*c+12* ( -12+81*c^2) ^ (1/2)
A =
[1/6*sigma^ (1/3) +2/sigma^ (1/3)]
[ -1/12*sigma^ (1/3) -1/sigma^ (1/3) +1/2*i*3^ (in) * (1/6*sigma^ (1/3) -2/sigma^ (1/3))]
[ -1/12*sigma^ (1/3) -1/sigma^ (1/3) -1/2*i*3^ (in) * (1/6*sigma^ (1/3) -2/sigma^ (1/3))]
Pretty functions can sometimes play the same role.
Pretty (f) show the customary writing form of a function
The solution of linear equation Group
Solving a linear equation group with a backslash/
A=HILB (3)
B=[1 2 3] '
A/b
Eigenvalues and eigenvectors of matrices
Using the Eig (v,d) function, [V,d]=eig (A); where D returns the eigenvalues, V returns the corresponding eigenvectors, and the default second parameter returns only the eigenvalues
Syms a B C real
A=[a b c; b c A; c a b];
[V,d]=eig (A);
In order to observe more clearly, using the previously learned substitution function, here is not the default sigma, instead of the use of M, explicit instead of the tedious expression of the sub-type
Vv=subexpr (v);
Vs=subs (vv, ' m ', ' Sigma ') runs the result of
vs =
[1, 1, 1]
[-(C+ (m)-a)/(C-b),-(C (M)-a)/(C-b), 1]
[-(A-(m)-B)/(C-b),-(A + (m)-B)/(C-b), 1]
and substituting m for expression in D
Dd=subexpr (d);
Ds=subs (DD, ' m ', ' Sigma ')
Run the result as DS =
[(M), 0, 0]
[0,-(m), 0]
[0, 0, C+a+b]
Note the following commands are also available for eigenvalue values
F=poly (a) Poly function is used to find the characteristic polynomial of a
D=solve (f) Solve (f) function to find the solution of a polynomial
The SVD () function evaluates the singular value decomposition of a matrix and decomposes the matrix into the product of two orthogonal matrices and diagonal matrices.
A=sym (HILB (2))
[U,s,v]=svd (a)
Algebraic equations and equations
The solution of algebraic equations can be solve (f) command, and if F does not contain =,matlab the expression will be zeroed. The unknown amount of the equation is determined by findsym or explicitly indicated by default
Syms a b c x
Solve (A*x^2+b*x+c) with x as the default variable
Solve (A*x^2+b*x+c,a) specifies that a is a variable
To find the solution of an equation with an equal sign (be sure to add a single quotation mark)
F=solve (' cos (x) =sin (x) ')
X=solve (' exp (x) =tan (x) ') calculates a variable-precision solution if the symbolic solution cannot be obtained.
Solving equations is similar to a single equation
Solve a ternary once equation
V=solve (' a*u^2+v^2 ', ' u-v=1 ', ' a^2-5*a+6 ')
The result is V =
A: [4x1 sym] u: [4x1 sym] V: [4x1 sym]
Some common symbolic operations
Limit operations Limit
Limit (f) To find the maximum of x to 0
Limit (f,x,a) or limit (f,a) to find the limits of X to a
Limit (F,a, ' left ') limits (F,a, ' right ') for X to A and a
Limit (F,inf) for X to infinity
Symbol summation Symsum (s)
Symsum (s) sum of variables determined by default Findsym
Symsum (s,v) sums the variable v specified in s
Symsum (s,a,b) symsum (s,v,a,b) sum of the finite terms from a to B
Syms k N
Symsum (k) sum from 0 to K
Symsum (k,0,n-1) sum from 0 to N-1
Symsum (1/k^2,1,inf) infinite sum
Taylor series Taylor (f)
Taylor (f) Represents the 5-step talor expansion of F, which can be increased by specifying the order of the expansion (default 5), or by specifying the expanded variable for the multivariate function, and specifying the point at which to expand
Syms x t
Taylor (exp (x))
Taylor (log (x), 6, 1) at 1 O ' 6 order Taylor unfold
Taylor (X^t,3,t) on the 3-order Taylor expansion of T
Integral transformation
Fourier transform and Inverse transform Fourier (f)
Fourier analysis can convert signals to sinusoidal curves of different frequencies. Discrete data can be analyzed, and continuous-time systems can be analyzed, especially in the field of signal and graphics processing. Discrete transformation (DFT) acts on the acquisition of finite data, the most effective is the fast Fourier transform (FFT)
F=fourier (f) independent variable x, returns the function on the parameter W
F=fourier (F,V) returns function f about the function of the symbol Object V
F=fourier (f,u,v) transforms the function f about u instead of the default W, and the return function f is a function of V
Syms T v W x
Fourier (1/t)
Fourier (exp (-t) *sym (' Heaviside (t) '), V)
Fourier (diff (Sym (' F (x) ')), x,w)
Fourier inverse transformation
F=ifourier (F) default independent variable W, returns the function of X for integral to W
F=ifourier (f,v) returns function F is a function of the symbol object V, not the default X
F=ifourier (f,u,v) is a function f of u that transforms, not the default X, and the return function f is a function of V
Laplace transform and Inverse transform Laplace (f)
Applied in continuous system (differential equation), can be used to solve the initial value problem of differential equation
Laplace (F) default independent variable T, which returns the function of S by default L
Laplace (f,t) returns the function L for T, not the default s
Laplace (F,W,Z) The independent variable w integral of function F, returns the function of Z
Inverse transformation
F=ilaplace (L) default independent variable S, return function F for t
F=ilaplace (l,y) returns the function f for Y, not the default T
F=ilaplace (L,y,x) The independent Y-integral of the function L, returns the functions f for X
Z-Transform and Inverse transform Ztrans (f) The z-transform of scalar sign F
F=ztrans (f) default independent variable n, returns the function on Z
F=ztrans (f,w) returns the function f about the symbol variable W, not the default Z
F=ztrans (f,k,w) The symbolic variable for K returns the function of the symbol variable w as a Z-transform
Inverse transform Iztrans (F)
F=iztrans (F) or (f,k) or (f,w,k)
Symbol drawing function
Symbolic function simple drawing function Ezplot (f)
F can contain a string or expression of a single symbolic variable x, the default paint interval ( -2PI,2PI), if f contains X and Y, the image being drawn is f (x, y) = 0 of the images, the lack of provinces and regions is -2pi<x<2pi,-2pi<y<2pi.
Ezplot (F,xmin,xmax) or Ezplot (F,[xmin,xmax]) draws an image on the Xmin<x<xmax interval
syms x t
Ezplot (' T*cos (t) ', ' t *sin (t) ', [0,4*pi])
Draw symbol image function Fplot (fun,lims,tol, ' Linespec ', n)
where Lims=[xmin,xmax] or [Xmin,xmax, Ymin,ymax] Tol for the specified relative error, the default 0.001 ' Linespec ' specifies that the drawing's Linetype n specifies a minimum of n+1 points for plotting
[X,y]=fplot (Fun,lims,...) returns only the points used for drawing, not drawing, You can call plot (x, y) to draw the graph yourself.
Syms x
Subplot (2,2,1), Fplot (' humps ', [0,1])
f= ' ABS (exp (x* (0:9)) *ones (10,1)) '
Subplot (2,2,2), Fplot (F,[0,2*pi])
Subplot (2,2,3), Fplot (' Sin (1./x) ', [0.01,0.1],1e-3]
Matlab drawing
Two-dimensional drawing
Plot plots two-dimensional images in (x, y) coordinates to support multiple X-y two-element structures
Plot3 draws three-dimensional graphics in (x, y, z) coordinates;
Loglog draws a two-dimensional graphic at (x, y) logarithmic coordinates;
SEMILOGX plots in two-dimensional coordinates where x is a logarithmic coordinate and y is a linear coordinate;
Semilogy plotting in x is linear and y is a logarithmic coordinate in two-dimensional coordinates
Plotyy drawing in coordinates with two Y-axes
Plot usage
plot (x, y, '--rs ', ' linewidth ', 2, ' markeredgecolor ', ' K ',...
' Markerfacecolor ', ' g ', ' Markersize ', ten)
Plotyy usage
Plotyy (x1,y1,x2,y2) x1 as standard, left axis for y-axis y1 vector, X2 as datum, Y axis for right axis, y2 vector
Plotyy (x1,y1,x2,y2,fun) a drawing function (plot, semilogx,semilogy,loglog,stem) specified with a string fun
Plotyy (X1,Y1,X2,Y2,FUN1, FUN2)
t=0:pi/20:2*pi;
Y=exp (sin (t));
Plotyy (T,y,t,y, ' plot ', ' stem ') stem is a two-dimensional rod diagram
[Ax,h1,h2]=plotyy (...) returns a handle to the left and right two y-axis (ax (1) Ax (2), and a handle to a graphical object generated in two axes, respectively, H1 h2
t=0:900;
a=1000;
a=0.005;
b=0.005;
Z2=cos (b*t);
Z1=a*exp (-a*t);
[Haxes,hline1, Hline2]=plotyy (t,z1,t,z2, ' semilogy ', ' plot ');
Axes (haxes (1))
Ylabel (' semilog plot ') logarithmic coordinates
Axes (haxes (2))
Ylabel (' linear plot ')
Set (Hline2, ' LineStyle ', '--')
other two-dimensional drawing instructions
Bar (x, y) two-dimensional bar chart
hist (y,n) histogram
Histfit (Y,n) with a quasi-and line of Histograms, N is the number of straight square
Stem (x, y) match bar chart
Comet (x, y) Comet-like trajectory map
Compass (x, y) compass map
ErrorBar (x,y,l,u) error limit diagram
Feather (x, y) Feather Chart & nbsp The
Fill (x, Y, ' R ') two-dimensional fill function is filled with red
Pie (x) pie
Polar (t,r) Polar map R is the amplitude vector, T is the angular vector
T=0:0.1:8*pi;
R=cos (3*T/2) +1/2;
Polar (T,r), xlabel (' polar Directive ')
Quiver (x, y) magnetic line diagram
Stairs (x, y ) ladder diagram
Loglog (x, y) logarithmic graph
SEMILOGX semilogy semi-logarithmic graph
Matlab Three-dimensional drawing
Plot3 (x, Y, z) three-dimensional line drawing
t=0:pi/50:15*pi;
plot3 (sin (t), cos (t), T, ' r* ') similar to plot
V=axis returns the range of axes
text (0,0,0, ' origin ') adds text at a coordinate point;
plot3 Add dimension you can draw multiple graphs at once to make the two-dimensional graphical eye an axis arrangement
Three-dimensional Network Diagram drawing
Mesh (x, y, z) grid
Mesh (x,y,z,c) four-dimensional, (x, Y, z) represents three dimensions, and C represents the color
Mesh (..., ' property Name ', property value,...) Sets the value of each property of the surface
[x,y,z]=sphere];
Mesh (x, y, z), hidden off surface is set to transparent
MESHC (x, y, z) plot grid and basic contour plots
Meshz (x, y, Z) draws a grid diagram containing 0 planes
Waterfall (x, y, z) is like mesh, but its grid lines appear only in one direction of the x-axis in the effect, with the waterfall-like waterline
A scalar instruction of two variables Meshgrid (x) or Meshgrid (x, y) (p179)
generates two two-dimensional vectors for two one-dimensional vectors for z=f (x, y) operations, calculates all z values, and z is a scalar instruction of x y;
[x , abbreviated of Y]=meshgrid (x) Meshgrid (x,x);
[X,y]=meshgrid (x, y)
[X,y,z]=meshgrid (z/y) for drawing of three-dimensional graphics
[X,y]=meshgrid ([ -2:0.1:2]);
Z=x.*exp (-x.^2-y.^2);
Plot3 (x, Y, z)
Surf (x,y,z,c) Shaded surface
Surf (x, y, z) implies c=z
Surf (z) implies that the value of x, Y is automatically generated surf the instruction according to the dimensions of Z;
Surfc draw a surface plot with a basic contour
SURFL a surface chart with a luminance
Shading one of the entire segments of the flat network cable graph or a patch of a surface plot is a color
Shading Interp The color of each point on a segment or patch is by linear interpolation of the top color of the line or piece;
A surface chart cannot be made as transparent as a grid, but when needed, the data can be set to nun at the hole
Drawing of contours
Plotting contour lines in two-dimensional space contour
Contour (x,y,z,n) draw n Contour lines (n can be omitted)
Contour (x,y,z,v) draws contours at the height specified by vector V (can be saved)
C=contour (x, y, z) calculates the height value of the contour line
C=CONTOURC (X,y,z,n) calculates x-y coordinate data for n contour lines
C=CONTOURC (X,Y,Z,V) calculates x-y coordinate data for contours specified by vector v
Clabel (c) marking the contour of C array
Clabel (c,v) Specifies the height of the contour assigned to Vector v
Clabel (c, ' manual ') use the mouse to add height identification to the contours in the point
Contour Contour3 (x, Y, Z) for three-dimensional space plotting
[X,y,z]=peaks (30);
CONTOUR3 (x,y,z,16, ' G ')
Pseudo-color graph pcolor of binary functions (x, Y, z)
is the directive surf two-dimensional equivalent instruction, represents the pseudo-color, can be combined with the contour monochrome contour line to draw the color contour map
[X,y,z]=peaks (30);
Pcolor (x, y, z); Pseudo Color
Shading Interp color interpolation to make the color average gradient
Hold On,contour (x,y,z,20, ' K ') ... Draw Contour Lines
Colorbar (' Horiz ') horizontal color ruler
C=contour (x,y,z,8);
Clabel (c) Labeling contours
vector field graph (velocity diagram) quiver
Used to describe the gradient size and direction of the function z=f (x, y) at point (x, y)
[X,y]=meshgrid (x, y) x, y is the coordinate matrix of the Z-array element
[U,v]=gradient (Z,dx,dy) u,v is the derivative of Z pair x y respectively, and dx dy is the calculation step in x y direction.
Quiver (x,y,u,v,s, ' linespec ', ' filled ') u,v is required to determine the size and direction of each vector in the vector field, S is the size of the specified arrow, the default is 1,linespec as a string, specifying the legal line and color, Filled the drawing identifier used to populate the definition
[X,y]=meshgrid ( -2:.2:2,-1:.15:1);
Z=x.*exp (-y.^2);
[Px,py]=gradient (z,.2,.15);
Contour (x, y, z);
Hold On,quiver (x,y,px,py), axis image
Polygon Fill Fill (x,y,c)
c defines a color string, which can be ' r ', ' B ', etc., or it can be represented by an RGB tri-color [r,g,b] value of 0-1
Four-dimensional representation of graphs