MATLAB - EDB/ 第1 章 行列の入力と加工 MATLAB は行列の演算を基本とするソフトです.そこでまずは行列を作ることから始めましょう. その後で行列の要素をみたり,行列を加工する練習をしましょう. 1.1 ベクトルと行列要素の作り方 ここでは

  • Published on
    15-May-2018

  • View
    222

  • Download
    4

Embed Size (px)

Transcript

  • i

    I MATLAB iii

    1 11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    2 162.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    3 263.1 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.2 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    II MATLAB 37

    4 334.1 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384.5 . . . . . . . . . . . . . . . . . . . . . . . . . . 424.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

  • ii

    4.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

    5 GUI 505.1 . . . . . . . . . . . . . . . . . . . . . . . . . . 505.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575.3 GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625.4 GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695.5 MATLAB . . . . . . . . . . . . . . . . . . . 725.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    III MATLAB 74

    6 756.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 756.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766.3 Phase Portrait DemoPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786.4 DufFix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816.5 DemoBF . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

    7 867.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 867.2 Poincare . . . . . . . . . . . . . . . . . . . . . . . . . . . 877.3 Rossler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

    8 Switched Dynamical Systems with Moving Border 968.1 Formation of the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

    8.2 The Poincare map and periodic solution . . . . . . . . . . . . . . . . . . . . . . . 99

    8.3 Phase Portrait DemoPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038.4 DufFix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038.5 DemoBF . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

    A 104

    B 105B.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105B.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106B.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

  • I

    MATLAB

  • 1

    1

    MATLAB

    1.1

    1.1.1 [ ; ]

    1.1

    1. A=0:102. t=0 : pi/2: 2*pi3. B=[1 2 3; 4 5 6]4. A=[B; 7 8 9]5. H=Hello world!

    x = xmin : x : xmax

    [xmin, xmin + x, xmin + 2x, , xmax] x 1

    t=0 : pi/30 : 2*pi; plot(t, sin(t))

  • 1 2

    [] 1.1 3. 2 3 2 3

    B =

    1 2 3

    4 5 6

    B 4. B 3 [7 8 9] 3 3 A

    A =

    1 2 3

    4 5 6

    7 8 9

    A=1:100; 1.1 5.

    1.1.2 [ ]

    [] 1 (array constractor) [ ] (concatenation) 1.1

    1. A = [5] , B =

    2 1 1

    1 5 1

    1 1 2

    , C =

    1 + 2j

    2

    3 5j

    , D =

    1 5j

    2 + 4j

    3 3j

    4 + 2j

    5 j

    2. A =

    2 4 6 8 10

    10 8 6 4 2

    , B =

    1 3 5 7 9

    9 8 7 6 5

    3. A =[Solving our problems with MATLAB.

    ], B = [I like A]

    1. A = [5] A = 5 1 1 A = [5] B = 5 2 A==B

  • 1 3

    1 A = B 0 3. j

    1 i j i

    2 sqrt(2)

    4.

    A = [2 : 2 : 10 ; 10 : 2 : 2]

    A = [(2 : 2 : 10) ; (10 : 2 : 2)]

    [A B] [A ; B]5. A B I like Solving our problems with MATLAB.

    1.2 1.2.1

    MATLAB (array) *1 1.1 2 MATLAB

    NaN Not a Number 0/0

    (matrix)*2 (scalar)*3 m n m n m n

    *1 C Pascal Fortran MATLAB

    *2 matrices *3 i j

  • 1 4

    1.1 MATLAB

    a = 5.5, b = 2 pi+ 3j

    1 1

    pi

    i j

    NaN

    1 n a = [1 2 3 4 5]

    m 1 a =

    1

    2

    3

    m n A =

    1 2 3

    5 3 1

    0 0 A = [ ]

    zeros(0,4) 0 4

    zeros(3,0) 3 0

    eye(3)

    ones(3,4) 1

    zeros(3,4) 0

    rand(3,4)

    magic(4) 4 4

    pascal(4) Pascal

  • 1 5

    A =

    a11 a12 a1na21 a22 a2n...

    ......

    am1 am2 amn

    = [apq] (1.1)

    apq (element) (p, q) *42

    Avec = vec (A) =

    a11...

    am1

    a12...

    am2...

    a1n...

    amn

    =

    1

    2..................

    r1

    r

    (1.2)

    A 1 1 (array) A 1

    apq = r

    (p, q) r

    r = (q 1)m+ p (1.3)

    MATLAB apq r ( )

    apq A (p, q)r A (r)

    2 1 (1.3)

    *4 aij i j MATLAB p q

  • 1 6

    1.2.2

    1 M

    M=magic(5)

    M =

    17 24 1 8 15

    23 5 7 14 16

    4 6 13 20 22

    10 12 19 21 3

    11 18 25 2 9

    M = [mpq] , p, q = 1, 2, , 5

    mpq 2 3 M( 2, 3 )7 M( 12 ) (1.3) 12 = (3 1)5 + 2 M( 12 : 15 ) M( [12 13 14 15] )

    p 2 M( 2, : ) M( end, : )

    2 4 M( 2 : 4, : )

  • 1 7

    M( 4 : 2, : )Empty matrix: 0-by-5 0 5

    q 3 M( : , 3 ) M( : , end )

    3 4 M( : , 3 : 4 )2 3 4 M( 2 , 3 : 4 )

    2 4 3 4 N N=M( 2 : 4 , 3 : 4 )

    1.3

    1 1 (1.1) (1.2) M( : ) size( ans ) ans= 25 12 25 1

    2 M=magic(5) M( 2, : )=[ ]

  • 1 8

    2 3 M=magic(5) M( 2 : 3, : )=[ ]

    3 M=magic(5) M( :, 3 )=[ ]

    2 3 M=magic(5) M( :, 2 : 3 )=[ ]

    2 2 20 M=magic(5) M( 2 : 2 : 20 )=[ ]

    X X=1 : 10 X( 10 : -1 : 1 )

    M=magic(5) M( [1,5],: )=M( [5,1],: )

    M=magic(5) M( [2, 5,1],: )

    M=magic(5) N=[ 1 2 3 4 5 ] M(2, : )=N

    M=magic(5) N=[ 1 2 3 4 5 ] M(2, : )=N

  • 1 9

    1.2 MATLAB

    MATLAB

    diag

    rot90 90

    tril

    triu

    fliplr

    flipud

    N M=magic(5) N=[ 1 2; 3 4] M(2:3, 3:4 )=N

    N M=magic(5) N=[ 1 2; 3 4] M([1, 3], [3, 4] )=N

    [ ] 1.2

    1.4 `

    A1, A2, , A`

    MATLAB 3

    3 3 4 A=ones(3)

  • 1 10

    A( : , : , 2)=2*ones(3) A( : , : , 3)=3*ones(3) A( : , : , 4)=4*ones(3) size( A ) ans= 3 3 42 A 4 MATLAB (dimension) 3

    2 A( : , : , 2)

    1 1 2 1 [A( : , 1 , 1), A( : , 1 , 2)]

    3 1.3

    whos whos(A) Name Size Bytes Class A 3 3 4 228 double array Grand total is 36 elements using 228 bytes

    1.5

    clear wild card * clear AA

  • 1 11

    1.3

    size size(A)

    ans= 2 3 4 2 3 4

    ndims ndims(A)

    ans= 3

    whos whos

    Name Size Bytes Class

    1.6

    1.6.1

    1. MATLAB

    2. [ ]

    3.4. Hello

    1. 2 A(2, 3) 2 3 1 A(12) (1.3)

    2. 3. 4. size

  • 1 12

    array

    char numeric

    double

    sparse

    unit8

    cell struct

    1.1

    1.6.2

    MATLAB Data Types There are six fundamental data types(classes) in MATLAB, each one a multidimensional array.

    The six classes are double, char, sparse, init8, cell, and struct. The two-dimensional versions of these

    arrays are called matrices and are where MATLAB gets its name.

    You will probably spend most of your time working with only two of these data types: the double

    precision matrix (double) and the character array (char) or string. This is because all computations

    are done in double-precision numbers or strings.

    The other data types are for specialized situations like image processing (unit8), sparse matrices

    (sparse), and large scale programming (cell and struct).

    You cant create variables with the types numeric or array. These virtual types serve only

    to group together types that share some common attributes.

    The unit8 data type is for memory efficient storage only. You can apply basic operations such

    as subscripting and reshaping to these types of arrays but you cant perform any math with them.

    You must convert such arrays to double via the double function before doing any math operations.

    You can overload methods for the build-in data types in exactly the same way you overload a

    method for an object. For example to define sort for a unit8 array, create a method(sort.m) and

    place it into @unit8 within a directory on your path.

    The table that follows describes the data types in more detail.

    (struct) C C

  • 1 13

    1.4 MATLAB

    Class Example Description

    double [1 2; 3 4] Double precision numeric array (this is

    the most common MATLAB variable type).

    char Helloe Character array (each character is 16 bits

    long). Also referred to as string.

    sparse speye(5) Sparse double precision matrix (2-D only).

    The sparse matrix stores matrices with

    only few non-zero elements in a fraction

    of the space required for an equivalent full

    matrix. Sparse matrices invoke special

    methods especially tailored to solve sparse

    problems.

    cell {17 hello eye(3)} Cell array. Elements of cell array contain

    other arrays. Cell arrays collect related

    data and information of a dissimilar size

    together.

    struct a.date=12; Structure array. Structure arrays have

    a.mat=magic(5); field names. The fields contain other

    a.color=red; arrays. Like cell arrays, structures collect

    related data and information together.

    unit8 unit8(magic(3)) Unsigned 8 bit integer array. The unit8

    array stores integers in the range from 0

    to 255 in 1/8 the memory required for a

    double precision array. No mathematical

    operations are defined for unit8 arrays.

    UserObject inline(sin(x)) User-defined type.

  • 1 14

    [ ] [ ] (nest) [ ] { }

    C={[1, 2], [1 2 3;4 5 6], [7 8; 9 0; 2 1]} C 3 C{2}

  • 1 15

    1.7 1. [[[]]] [],[],[] [[],[]] [[]],[] {{{}}} {},{},{} {{},{}} {{}},{}

    2. eye(4) 4 4

    0 1 0 0

    0 0 1 0

    0 0 0 1

    1 0 0 0

    ,

    0 0 1 0

    0 0 0 1

    1 0 0 0

    0 1 0 0

    ,

    0 0 0 1

    1 0 0 0

    0 1 0 0

    0 0 1 0

    ,

    1 0 0 0

    0 0 1 0

    0 1 0 0

    0 0 0 1

    shiftrotate(A)

    1

    3. 4 4

    0 1 0 0

    0 0 1 0

    0 0 0 1

    0 0 0 0

    ,

    0 1 0 0

    0 0 1 0

    0 0 0 0

    0 0 0 0

    ,

    0 1 0 0

    0 0 0 0

    0 0 0 1

    0 0 0 0

    ,

    0 0 0 0

    0 0 0 0

    0 0 0 0

    0 0 0 0

    4. MATLAB Help menu Examples and Demos Demo Window Matrixmanipulation Demo

    Windows 95

    1 3 Demo

    5. MATLAB Help Window matlab\elmat compan, hadamard, hilb, vander magic, pascal

    Hadamard 2n

    hadamard(2), hadamard(4), hadamard(8)

  • 16

    2

    MATLAB

    2.1

    1. 2

    2.

    3.

    3

    MATLAB

    MATLAB

    MATLAB

  • 2 17

    2.1 a = [ar] , b =[br] A = [apq] , B = [bpq]

    2.2 2.1

    1. t=0:10 t*5 t.2 t /2 t

    2. A=[1 2 3; 4 5 6; 7 8 9] A*2 A.2 A/2 A

    3. A=[1 2 3; 4 5 6; 7 8 9] B=2*ones(3) A.*B A*B A./B 2*A-3*B

    4. A=[1 1;1 -1] kron(A, A)

    2.1 1.. x=-5 : 0.1 : 5

    f (x) = x3 x2 5x 2

    y = x.3 x.2 5 x 2

  • 2 18

    2.1

    MATLAB

    a = [ar] a or a 1a =

    [1ar

    ]a/ or \a

    A = [apq] A or A 1A =

    [1apq

    ]A/ or \A

    [(apq)n] A.n

    a+ b = [ar + br] a+ b

    a b = [ar br] a b

    A+B = [apq + bpq] A+B

    AB = [apq bpq] AB

    AB = [apqbpq] A. B

    AB = [apq/bpq] A./B

    AB =[

    k

    apkbkq

    ]A B

    Kronecker AB = [apqB] kron(A, B)

    AX = B X = A1B A\B

    XA = B X = BA1 B/A

    An An

    At A.

    At A

  • 2 19

    plot(x, y) grid4. Kronecker

    1 1

    1 1

    1 1

    1 1

    =

    1

    1 1

    1 1

    1

    1 1

    1 1

    1

    1 1

    1 1

    1

    1 1

    1 1

    =

    1 1 1 1

    1 1 1 11 1 1 11 1 1 1

    2.1

    1.[1 2 3 4 5

    ]/2

    2.

    1 2 3

    6 5 4

    7 8 9

    1 6

    2 5

    3 4

    3.

    1 5 6

    9 2 4

    7 8 3

    x

    y

    z

    =

    3

    2

    1

    4.[1 2 3 4

    ]

    1

    2

    3

    4

    5.

    1

    2

    3

    4

    [1 2 3 4

    ]

  • 2 20

    2.2 MATLAB

    MATLAB

    abs

    angle

    real

    imag

    conj

    sin

    cos

    tan

    atan

    atan2 4

    exp

    log

    log10

    rem

    sign

    sqrt

    2.3 2.3.1

    MATLAB 2.2

  • 2 21

    A =

    a bc d

    cos

    cos (A) =

    cos (a) cos (b)

    cos (c) cos (d)

    cos

    *1 t=0 : pi/30 : 2*pi plot( t, sin(t))

    2.3.2

    2.3 M=magic(5) sum(M) trace(M) sum(M) sum(rot90(M))

    rank

    1 2 3

    4 5 6

    7 8 9

    = 2

    *1 (array) MATLAB (list) arrayable cos

  • 2 22

    2.3 MATLAB

    MATLAB

    det

    sum

    trace

    norm

    rank

    null Kernel

    eig

    poly

    expm

    logm

    sqrtm

    1 2 3

    4 5 6

    7 8 9

    x

    y

    z

    =

    a

    b

    c

    2.4 1.

    1. 2x+ 13y 3z = 7, x+ y = 1, x+ 7z = 222. x 2y 12z = 12, 2x+ 2y + 2z = 4, 2x+ 3y + 4z = 33. x+ y + z = 5, x y z = 4, 2x+ 6y + 6z = 124. x+ 3y + z = 4, x y + z = 1, 2x+ 4y = 05. x+ 2y + z 4w + 1 = 0, x+ 2y z + 2w 1 = 0,

    2x+ 4y + z 5w + 1 = 0, x+ 2y + 3z 10w + 2 = 0

  • 2 23

    2. 78 120 264

    3.

    1.

    1 2 3

    4 5 6

    7 8

    9 0

    1 2

    2.

    1 2

    0 3

    1 4

    1 3

    0 1

    1 3

    3.

    1 1 1

    1 0 2

    1 1 1

    0 1 1

    1 1 2

    2 0 2

    4.

    7 1

    1 0

    2 3

    5

    4

    5.

    7 1

    1 0

    2 3

    5

    4

    6.

    1 1 1

    1 0 2

    1 1 1

    0 1 1

    1 1 2

    2 0 2

    7. A =

    1 2 1

    1 4 1

    2 4 0

    , P =

    1 2 1

    1 1 1

    1 1 0

    find PAP1

  • 2 24

    R

    R R

    RR

    R

    R

    R

    R

    R

    R

    R

    b' a'

    a b

    2.1

    (a)

    a'

    a

    (b)

    a'aR

    C

    G

    1L

    1C RL

    2L

    2C

    2.2

    4. A=[1: 5]

    B =

    1 2 3 4 5

    1 2 3 4 5

    1 2 3 4 5

    1 2 3 4 5

    1 2 3 4 5

    , C =

    1 2 3 4 5

    2 4 6 8 10

    3 6 9 12 15

    4 8 12 16 20

    5 10 15 20 25

    5. 2.1 aa, bb, ab, ab 6. 2.2(a), (b) ab 1

    7. rank null

    8. 2.3 abcd

  • 2 25

    a b c d

    1 2 3 4

    2.3 4

  • 26

    3

    Visualization

    3.1 2 2

    3.1.1 plot

    3.1

    1. t=0 : pi/30 : 2*pi ; plot(t, sin(t))

    2. t=0 : pi/30 : 2*pi ; plot(cos(t), sin(t))

    3. t=0 : pi/30 : 2*pi ; plot(t, cos(t), t, sin(t))

    4. t=linspace(0 , 2*pi, 30); plot(t, cos(t),r , t, sin(t), b*)

    plot plot(, ), plot(, , r )r red r 3.1 linspace(xmin, xmax, ) [xmin, xmax]

  • 3 27

    3.1

    y .

    m o

    c x x

    r + +

    g *

    b -

    w :

    k -.

    hold on plot(t, sin(3*t)) hold off 3.1

    1. y = exp(0.3t) sin(3t), 0 < t < 102. y = 2 sin(t) + cos(4t), 0 < t < 2

    3. y = 2 sin(t) + cos(4t) and y = 2 cos(t) + sin(3t), 0 < t < 24. x = sin(t) and y = cos(t) + sin(3t), 0 < t < 2

    axis square axis normal title Exercise 1 xlabel time ylabel voltage

  • 3 28

    3.2 2

    plot(x,y)

    ploar(t,r)

    semilogx(x,y)

    loglog(x,y)

    stem(x,y)

    grid on/off

    clf

    axis([xmin xmax ymin ymax]) 3 plot(t, y1, t, y2, t, y3) grid ongrid off clf

    3.1.2 polar

  • 3 29

    3.2

    1. t=0 : pi/50 : 2*pi ; polar(t, exp(-0.1*t))

    2. t=0 : pi/50 : 2*pi ; polar(cos(t), sin(t))

    3. t=0 : pi/50 : 2*pi ; polar(cos(t), 2*sin(t)+cos(4*t))

    polar polar()

    3.1.3 semilogx

    3.3

    2

    G (s) =1

    s2 + 2s+ 1

    y = 20 log |G| = 10 log{(

    1 x2)2 + (2x)2}

    x=logspace(-1 , 1, 100); zeta=.1 y=-10*log10((1.0-x.2).2+(2.0*zeta*x).2); semilogx(x, y, r) grid on

    logspace( 10 10 ) [101, 101] 100 semilogx zeta

  • 3 30

    Root

    Figure

    Axes Uicontrol Uimenu

    Surface Text LightImage Line Patch

    3.1

    hndl=semilogx(x, y, r) set(hndl, Color, blue) set(hndl, LineWidth, 4) 3.2

    3.3

    = tan1 2x1 x2

    tan1 atan2 atan2( y x ) y=-180*atan2(2.0*zeta*x, 1.0-x.2)/pi;

    3.1.4

    MATLABVersion 5 (objet oriented programming: OOP) (handling) 3.1

    1. 3

  • 3 31

    figure figure figure3 Figure No. 3

    2. figure(1)

    3. axis([-1,1,-2,2])Figure No. 1 figure

    4. 3.1 MATLAB MATLAB

    5. Uicontrol(user interface control) Uimenu(user interface menu) Demo help

    3.1.5

    Bode subplot(m,n,p) m n p x=logspace(-1 , 1, 100); y=-10*log10((1.0-x.2).2+(0.2*x).2); z=-180*atan2(0.2*x, 1.0-x.2)/pi; subplot(2,1,1); semilogx(x, y, r);grid on subplot(2,1,2); semilogx(x, z, b);grid ony -200 axis([0.1,10,-180,0]) (tick)

  • 3 32

    set(gca, ytick, [-180:30:0]) gca graphics current axes

    gcf Root Current Figure gca Figures Current Axes gco Figures Current Object

    get(gca) get(gcf)set help

    3.2 3 3

    3D 2D 2D

    3.2.1 plot3

    3.4

    (torus)

    x = (r1 + r2 cos) cos , y = (r1 + r2 cos) sin , z = r2 sin

    t=0:pi/20:60*pi; r=2+0.5*cos(t/30); x=r.*cos(t) y=r.*sin(t) z=0.5*sin(t/30); plot3(x,y,z,r):axis([-2,2,-2,2,-1,1]):grid onplot3 3D 3 plot( x , y , z , r )

  • 3 33

    r red

    3.2.2

    3.5

    x, y

    z = sinx cos y

    t=-pi:pi/20:pi; x=t; y=t; z=sin(x)*cos(y); mesh(x,y,z) surf(x,y,z) mesh(z) surf(z) mesh( i, j, zij )

  • 3 34

    3.2.3

    3.6

    colormap cool; k=4;n=2k-1; theta=pi*[-n:2:n]/n; phi=(pi/2)*[-n:2:n]/n; x=cos(phi)*cos(theta); y=cos(phi)*sin(theta); z=sin(phi)*ones(size(theta)); c=hadamard(2k); surf(x,y,z,c); axis square colorbar

    mesh surf (xij , yij , zij) (cij) 3.3 shading interp colormap hsv light lighting gouraud *1 help shading help lighting

    3D

    *1 gouraud

  • 3 35

Recommended

View more >