Matlab Start Basics

matlab introductory chapter, some basic usage remember about ~

M language is an interpreted language

the WHO : View the current variable

whoes : View the current variables and their dimension, such as the number of occupied bytes.

the Clear : Clear all variables

clear + variable name : Clear the variable

save save the current variable data to the default file name of the mat (.mat file A data file)

** save [** filename] [variable name] [variable name] [- append] [- ascii]

type of data

Numeric : double-precision, single-precision integer

a = 1; default established double data type

b = uint8 (a); b is converted to uint8;

a = uint8 (a); a is converted to uint8.

String

And character strings are used in single quotes , obtain ascii code value, or with double function abs

​ 如double('a');

ascii characters into output

char function: char (63); ascii code 63 representative of the output character.

The string to a single quotation mark

str2num, num2str, eval ( 't = 1'): The statement is executed as string

Structure

Structures. Member Name = // expression could have been no need to declare the establishment of ...

isstruct: whether the structure. fieldnames, isfield function, rmfield function, getfield function

Unit

{} Using established: a = {1, 'str', [11 12 13 14]};

Multidimensional matrix

Matrix establish :

Direct Input: Peer elements or spaces separated by commas, semicolons different row elements used as the spacer

The establishment of 0,1 matrix: zeros (row, column); empathy established ones (row, column);

Colon expression vector established: e1: e2: e3 e1 which is an initial value, e2 in steps, e3 is the end value

Similar colon expression: linspace (sta, end, step_length);

Simple matrix :

Matrix Index: A (i, j) == A (i * (m-1) + j);

int / pair <int, int> find (A == 2): find elements in the matrix A is 2, returns the serial number or the coordinate, a plurality of answers to a plurality of return time.

​ ind2sub(size(A),ind); sub2ind(sizeof(A),x,y);

Rearrangement matrix: res = reshape (A, 9,1) <==> A (:);

Transposed matrix: A ';

Split (exception index matrix) matrix: M = A (1,:) <==> M = A: contents of a first row of all columns (1,1 end), and if M = A (1,1 : 3); line one to three

Remove the matrix elements: A (ind) = []; A (1, :) = [];

Matrix extension (replication): M = repmat (A, 2,1); (2 extend into the A matrix row one, as a whole wherein the element A)

Compression matrix (de-emphasis): A = [1 2 3 4 4]; S = unique (A);

Matrix processing base

The establishment of a special matrix

Matrix : I = eye (m, n ); General (m == n) (Well matrix (* ^ _ ^ *));

Random matrix : rand: randomly generating a uniform array of 0-1. randn: generating a random number between 0 and 1, normal distribution

​ e.g.: Sn = rand(1,10); Sn = randn(1,10);

Sn = a + (b - a) * rand (row, column); a ~ b uniformly random matrix

y = u + sqrt (s) * randn: generating random matrix mean u, s is the variance of the normal distribution

Mean mean function can be obtained sequence, std variance sequence can be obtained

Cube matrix : magic (5);

In Hilbert : Hilb (. 4) (the format RAT) may be output in the form of a rational number (H (i, j) = (i + j-1) / (i + j))

Toeplitz: toeplitz(1:6);

Matrix and vector operations

matlab as a unit matrix, the matrix may be directly implemented addition, multiplication and other operations matrix determinant, rank of the matrix, inverse matrix calculation and the like transpose

DET (A) : the determinant of the matrix A, if the determinant is not zero, then reversible.

INV (A) : the inverse matrix

Complex vector : a = [1 + 5i, 2,3 + 6i];

Vector inner product :. S = a * b ' (a, b are row vectors)

Linear Equations

​ A = [1,2,3;1,4,8;1,8,27];

​ b = [5,-2,6]’;

​ x = inv(A)*b; OR x = A/ b;

Simplification and similar matrix decomposition

Jordan Standard : jordan (A); [v J] = jordan (A); wherein V is a similarity transformation matrix, J is about canonical form

Eigenvalues : eig (A); [VJ ] = eig (A)

Matrix and vector norm

Norm : (A, 1) norm: 1 norm, norm (A, 2): 2 -norm, norm (A, inf): infinite norm, norm (A, 'fro' ): f norm

Matrix Analysis

Calculation function matrix derivative (number of elements in each derivative calculation function matrix) : the diff (A): find the first derivative. diff (A, 2) the second derivative.

Matrix function : funm (A, @ exp // @sin // @ cos);

Matlab program control structure

M文件:Script File、Function File.

Input data : input function. Data: disp function. Program pause: pause function (in seconds).

Branch and loop help for, help switch, help while , help if

Matlab dimensional high-level drawing operations

%%二维高层绘图
    %基本绘图
        x = 0:0.1:2*pi;
        y = sin(x);
         plot(x,y);
    %第二个参数为矩阵
        y1 = sin(x);
        y2 = cos(x);
        y3 = 0.002*exp(x);
        y4 = x;
        y5 = 0.002*tan(x);
        z = [y1;y2;y3;y4;y5];
       % plot(x,z);
     %两个参数都是矩阵
        x1 = 0:0.01:2*pi;
        x2 = -pi:0.01:pi;
        x = [x1;x2];    

```
    y1 = cos(x1);
    y2 = sin(x2);
    y  = [y1;y2];
    plot(x,y); % 按列绘图
    plot(x',y');
 % plot只有一个参数
    x = linspace(0,2*pi,200);
    y = sin(x);
    plot(y)
    y2 = cos(x);
    y3 = y + i*y2;
    plot(y3)
  
  % plot有多个参数
    x1 = linspace(0,2*pi,200);
    x2 = linspace(0,2*pi,100);
    y1 = cos(x1);
    y2 = sin(x2);
    plot(x1,y1,x2,y2)
    
  % plot含有的曲线选项
    x = linspace(0,2*pi,100);
    y = sin(x);
    plot(x,y,'r') %r g y m k b
    plot(x,y,'*') % * . p < > 
    plot(x,y,':') %'--' '-' '-.' ':'
    plot(x,y,'r*:')
```

 %% 图形标注
    x = linspace(0,2*pi,100);
    y = sin(x);
    plot(x,y);
    xlabel('x');
    ylabel('y');
    title('正弦');
    text(2,0.3,'y=sin(x)') 
    

```
    x1 = linspace(0,2*pi,200);
    x2 = linspace(0,2*pi,100);
    y1 = cos(x1);
    y2 = sin(x2);
    plot(x1,y1,x2,y2)
    xlabel('x');
    ylabel('y');
    title('正弦');
    text(2,0.3,'y=sin(x)^2');
    text(0.5,0.2,'y=cos(x)_2');
    legend('cos','sin');

%坐标轴控制
    xlim([0 10]) 
    % axis equal
```

%% 图像保持
    x =0:0.1:2*pi;
    y1 = sin(x);
    y2 = cos(x);
    hold on  % 图形保持
    plot(x,y1,'r');
    plot(x,y2,'g');
    hold off % 图形保持取消

%% 窗口分割
    x = 0:0.1:2*pi;
    y1 = sin(x);
    y2 = cos(x);
    y3 = tan(x);
    y4 = exp(x);
    subplot(221); % 将窗口分成2*2的小格,然后绘制第一个
    plot(x,y1);
    subplot(222);
    plot(x,y2);
    subplot(223);
    plot(x,y3);
    subplot(224);
    plot(x,y4);

Matlab symbolic operations

    %符号变量
        a = sym('a')
        syms b;
        b
    %符号常量
        c = sym('3');
    
    %符号表达式
        syms x
        f1 = 3*x+6
        f2 = 3*x+6
    %符号四则运算
        fadd1 = f1 + f2
        fsub1 = f1 - f2
        fmu1 = f1*f2
        fdiv = f1 / f2
    
    %符号表达式化简
        syms x y
        s = (x^2+y^2)^2+(x^2-y^2)^2;
        spy = simplify(s); 
    
    %符号表达式和数值的转换
        eval(c)
    %因式分解,展开和合并同类项
        syms a b x y
        f1 = a^3 - b^3;
        factor(f1) %因式分解
    
        f2=(3*x^2+8*y^2)*(-x^2+3*y);
        expand(f2) % 展开
    
        f3=3*x^2+4*x^2+5*x^2*y;
        collect(f3) % 合并同类型
    
    %符号矩阵
            a1 = [x x+y;y y^2] 
            transpose(a1) % 普通转置
            a1' % 共轭转置
    
        %符号函数值的求解
            syms x
            f1 = x^3-9;
            subs(f1,3)
    
        % 符号极限,符号微分,符号积分
            syms x a
            y =sin(x+a);
            limit(y,0)
            y2 = sqrt(1+exp(x));
        
        diff(y2)  % 数值中是求差分,符号计算是求解导数
        diff(y2,2) % 求二重导数
        diff(y2,3)
    
        y3 = (3-x^2)^3; % 不定积分
        int(y3)
    
    % 求定积分
        y4 = abs(1-x);
        int(y4,1,2)
    
    % 符号级数求和、泰勒级数
        syms n
        f = 1/ n^2;
        s1 = symsum(f,n,1,inf) % 无穷级数  
    
    %泰勒展开  
        syms x
        y = (1 + x + x^2) / (1 - x + x^2);
        taylor(y,x,1,'Order',3) % 在x=1处进行taylor展开并且 得到第三项的值
    
    %符号代数方程
        clear 
        syms x
        eval(solve(x+x*exp(x)-10))
    % 方程组
        clear
        syms x y
        [x y] = solve('x+y-98','x^(1/3)+y^(1/3)-2','x,y')
    
        [x y] = solve('1/x^3+1/y^3-28','1/x+1/y-4','x,y')
    
    %符号微分方程
        dsolve('Dy-(x^2+y^2)/x^2/2','y(1)=2','x')

Guess you like

Origin www.cnblogs.com/backkom-buaa/p/11482373.html