Octave メモ

2015年9月4日

はじめに

プログラミング言語 GNU Octave のメモ。

ベクトル

定義

>> a = [
   1
   2
   3
]
a =

   1
   2
   3

成分

>> a = [1, 2, 3]'
>> a(2)
ans =  2
>> a(2:3)
ans =

   2
   3

代入

>> a = [1, 2, 3]'
>> b = a
b =

   1
   2
   3

スカラーとの和・差

>> a = [1, 2, 3]';
>> a + 1
ans =

   2
   3
   4

>> a - 1
ans =

   0
   1
   2

スカラーとの積・除算

>> a = [1, 2, 3]';
>> a*2
ans =

   2
   4
   6

>> a/2
ans =

   0.50000
   1.00000
   1.50000

ベクトルどうしの和・差

>> a = [1, 2, 3]';
>> b = [4, 5, 6]';
>> a + b
ans =

   5
   7
   9

>> b - a
ans =

   3
   3
   3

ベクトルの成分どうしの積・除算

>> a = [1, 2, 3]';
>> b = [4, 5, 6]';
>> a .* b
ans =

    4
   10
   18

>> b ./ a
ans =

   4.0000
   2.5000
   2.0000

ノルム

>> a = [1, 2, 3]';
>> norm(a)
ans =  3.7417

内積

>> a = [1, 2, 3]';
>> b = [4, 5, 6]';
>> dot(a, b)
ans =  32

外積

>> a = [1, 2, 3]';
>> b = [4, 5, 6]';
>> cross(a, b)
ans =

  -3
   6
  -3

総和

>> a = [1, 2, 3]';
>> sum(a)
ans =  6

最大・最小

>> a = [1, 2, 3]';
>> max(a)
ans =  3
>> min(a)
ans =  1

行列

定義

>> A = [
   1, 2, 3
   4, 5, 6
   7, 8, 9
]
A =

   1   2   3
   4   5   6
   7   8   9

成分

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A(2, 3)
ans =  6
>> A(:, 2)
ans =

   2
   5
   8

>> A(1:2, 2:3)
ans =

   2   3
   5   6

>> A(:, [1, 3])
ans =

   1   3
   4   6
   7   9

代入

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = A
B =

   1   2   3
   4   5   6
   7   8   9

スカラーとの和・差

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A + 1
ans =

    2    3    4
    5    6    7
    8    9   10

>> A - 1
ans =

   0   1   2
   3   4   5
   6   7   8

スカラーとの積/除算

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A*2
ans =

    2    4    6
    8   10   12
   14   16   18

>> A/2
ans =

   0.50000   1.00000   1.50000
   2.00000   2.50000   3.00000
   3.50000   4.00000   4.50000

行列どうしの和・差

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = [10, 11, 12; 13, 14, 15; 16, 17, 18];
>> A + B
ans =

   11   13   15
   17   19   21
   23   25   27

>> B - A
ans =

   9   9   9
   9   9   9
   9   9   9

行列の成分どうしの積・除算

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = [10, 11, 12; 13, 14, 15; 16, 17, 18];
>> A .* B
ans =

    10    22    36
    52    70    90
   112   136   162

>> B ./ A
ans =

   10.0000    5.5000    4.0000
    3.2500    2.8000    2.5000
    2.2857    2.1250    2.0000

行列の積

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> B = [10, 11, 12; 13, 14, 15; 16, 17, 18];
>> A*B
ans =

    84    90    96
   201   216   231
   318   342   366

行列とベクトルの積

>> a = [1, 2, 3]';
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A*a
ans =

   14
   32
   50

行列の累乗

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A^2
ans =

    30    36    42
    66    81    96
   102   126   150

転置

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> A'
ans =

   1   4   7
   2   5   8
   3   6   9

トレース

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> trace(A)
ans =  15

対角成分

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> diag(A)
ans =

   1
   5
   9

>> sum(diag(A))
ans =  15

行列式

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 10];
>> det(A)
ans = -3.0000

逆行列

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 10];
>> inv(A)
ans =

  -0.66667  -1.33333   1.00000
  -0.66667   3.66667  -2.00000
   1.00000  -2.00000   1.00000

>> A*inv(A)
ans =

   1.00000   0.00000  -0.00000
   0.00000   1.00000   0.00000
   0.00000   0.00000   1.00000

連立方程式の解

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 10];
>> b = [1, 2, 3]';
>> x = A \ b
x =

  -0.33333
   0.66667
  -0.00000

>> A*x
ans =

   1.00000
   2.00000
   3.00000

単位行列

>> eye(3)
ans =

Diagonal Matrix

   1   0   0
   0   1   0
   0   0   1

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
>> eye(3)*A
ans =

   1   2   3
   4   5   6
   7   8   9

ゼロ行列

>> zeros(3)
ans =

   0   0   0
   0   0   0
   0   0   0

>> zeros(2, 3)
ans =

   0   0   0
   0   0   0

固有値・固有ベクトル

>> A = [1, 2, 3; 4, 5, 6; 7, 8, 10];
>> [P, lambda] = eig(A)
P =

  -0.223513  -0.865846   0.278296
  -0.503946   0.085651  -0.831847
  -0.834314   0.492925   0.480190

lambda =

Diagonal Matrix

   16.70749          0          0
          0   -0.90574          0
          0          0    0.19825

>> inv(P)*A*P
ans =

   16.70749   -0.00000   -0.00000
    0.00000   -0.90574   -0.00000
    0.00000   -0.00000    0.19825

非線形方程式

非線形方程式を解く

>> function r = f(x)
r = x^2 - 2;
end
>> fsolve(@f, 1)
ans =  1.4142

最小化

>> fminsearch(@(x) abs(x^2 - 2), 1)
ans =     1.4142

表示

結果の表示桁数を増やす

>> sqrt(2)
ans =  1.4142
>> format long
>> sqrt(2)
ans =  1.41421356237310
>> format short
>> sqrt(2)
ans =  1.4142

表示

>> a = 1
>> printf("a = %d\n", a)
a = 1