1 1 Octave GNU Octave Matlab John W. Eaton 1992 2.0.16 2.1.35 Octave Matlab gnuplot Matlab Octave MATLAB [1] Octave [1] 2.7 Octave Matlab Octave Octave 2.1.35 2.5 2.0.16 Octave 1.1 Octave octave Octave Octave octave:1> quit octave:1> exit Octave Ctrl-c Ctrl c
2 1 Octave Octave Window M m.m Octave Window 1.2 octave:1> a = 1 a = 1 octave:2> b = 1.23 b = 1.2300 octave:3> c = 3; ; % octave:4> x = pi x = 3.1416 octave:5> format long octave:6> x x = 3.14159265358979 octave:7> format short octave:8> x x = 3.14 octave:9> format octave:10> x x = 3.1416 octave:11> format long e octave:12> x x = 3.14159265358979e+00 octave:13> format short e octave:14> x x = 3.14e+00 octave:15> format % % % long % short % % long % short 1 i, j, I, J
1.3. 3 octave:16> y = 1+2i y = 1 + 2i octave:17> z = i z = 0 + 1i octave:18> z*z ans = -1 octave:19> printf("hello, Octave\n"); hello, Octave octave:20> x = pi; octave:21> printf("variable x = %f\n",x); variable x = 3.141593 octave:22> y = 1 + 2i; octave:23> printf("complex y = %f + %fi\n",real(y),imag(y)); complex y = 1.000000 + 2.000000i 1.3 1 2 3 A = 4 5 6 7 8 9 Octave Window octave:1> A = [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 ; Octave M file1.m
4 1 Octave A = input( Enter matrix A ) file1.m Octave pwd Octave Window octave:1> file1 Enter matrix A [1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 A [ ] A=[1 2 3;4 5 6;7 8 9]; 1. [ 1 2 3 4 5 A = 6 7 8 9 10 ] B = 1 2 3 4 5 1 1 C = 2 2 0 0 1.4 A, B X = A + B Y = A - B X = A*B A, B A t X = A*t A t A
1.4. 5 X = A A k k X = A^k 1.1 A, B A + B, A B 1 2 3 1 1 1 A = 4 5 6, B = 2 2 1 7 8 9 1 2 1 A = input( Enter matrix A ) B = input( Enter matrix B ) X = A + B Y = A - B Enter matrix A [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 Enter matrix B [1-1 1; 2 2-1; -1 2 1] B = 1-1 1 2 2-1 -1 2 1 X = 2 1 4 6 7 5 6 10 10 Y = 0 3 2 2 3 7 8 6 8
6 1 Octave 1. A, B AB 1 [ ] (1) A = 1 1 1 1, B = 2 3 AB =10 4 [ ] 1 1 [ ] 1 2 3 4 (2) A =, B = 2 2 5 6 7 8 3 3 AB = 30 4 70 4 4 4 1 1 1 1 6 (3) A = 0 1 1, B = 2 AB = 5 0 0 1 3 3 2. A, k A k [ ] [ ] 2 1 38 41 (1) A =, k =5 1 2 41 38 [ ] [ 2 1 0.12 0.16 (2) A =, k = 2 1 2 0.16 0.12 ] 1.5 A X = inv(a) Octave 1.1 help octave:1> help inv http://www.octave.org/doc/octave toc.html Ax = b
1.5. 7 1.1: norm rank det inv eig expm x = A\b 1.2 1 2 1 Av = λv, A = 2 1 1 4 2 0 octave:1> A = [1 2 1;2 1-1;4 2 0] A = 1 2 1 2 1-1 4 2 0 octave:2> [V,lambda] = eig(a) V = -0.57735 0.54944 0.40825 % V = [v1 v2... vn] 0.57735 0.13736-0.40825 0.57735 0.82416 0.81650 lambda = -2.00000 0.00000 0.00000 % lambda 0.00000 3.00000 0.00000 0.00000 0.00000 1.00000 octave:3> lambda = eig(a) % lambda = -2.00000 3.00000 1.00000
8 1 Octave 1. X XA = I 1 2 1 1 (1) A = 0 1 3 2 1 1 2 1 0 0 1 1 0.2 0.4 0.8 0.2 X = 0.5 0.5 0.5 0 0.1 0.3 0.1 0.4, A =10 0.1 0.3 0.1 0.6 1 0 1 1 1 1 A = 1 1 1 X = 1 1 0, A = 1 1 1 0 0 1 1 2. [ ] [ 1 0 (1) A = 2 (2) A = 0 1 1 0 2 (3) A = 0 1 2 2 1 0 2 16 2 3 13 (4) A = 5 11 10 8 9 7 6 12 3 4 14 15 1 1 1 1 1 (5) A = 1 2 3 4 1 3 6 10 4 1 4 10 20 1 0 1 0 1 1 ] 2 3. A e A [ ] [ 0 1 (1) A = e A = 1 1 1 1 0 (2) A = 0 1 1 0 0 1 e A = ] 0.65970 0.53351 0.53351 0.12619 0.36788 0.36788 0.18394 0 0.36788 0.36788 0 0 0.36788
1.6. 9 1.6 n X = I(n n) X = eye(n,n) 0(n m) zeros(n,m) 1 n m ones(n,m) D = 1 0 0 0 0 0 9 0 0 0 0 0 4 0 0 0 0 0 4 0 0 0 0 0 2 v=[1 9 4 4 2]; D=diag(v,0) 1.7 1.7.1 if, else, elseif if 1.3 a, b b 0 y = a/b (1.1) a = input( Enter a ); b = input( Enter b ); if b ~= 0 y = a/b endif
10 1 Octave 1.4 x { 1 if x 0 y = 1 if x<0 (1.2) x=input( Enter x ); if x >= 0 y = 1; else y = -1; endif y 1.5 x 1 if x>0 y = 0 if x =0 1 if x<0 (1.3) x=input( Enter x ); if x > 0 y=1; elseif x<0 y=-1; else y=0; endif y if if elseif if while 1.2
1.7. 11 1.2: < < <= > > >= == = = 1.7.2 switch 1.6 n n = 1 minus one n =0 zero n =1 one other value n=input( Enter n ); switch n case -1 disp( minus one ); case 0 disp( zero ); case 1 disp( one ); otherwise disp( other value ); endswitch switch n=input( Enter n ); switch n case 1 disp( 1 ); case {2,3,4}
12 1 Octave disp( 2 or 3 or 4 ); case 5 disp( 5 ); otherwise disp( other value ); endswitch 1.7.3 while while while statements endwhile while statements while break 1.7 a ɛ =0.001 2 eps=0.001; a=input( Enter a ); while a>=eps a=a*0.5 endwhile 1.7.4 for for for index = start : increment : end statements endfor index increment statements end increment =1 1.8 n 1 n
1.7. 13 n=input( Enter n ); s = 0; for i = 1:n s = s + i; endfor s for break C goto Octave MATLAB endif endswitch endwhile endfor end 1. x n y =1+x + x 2 + + x n a = 1; y = 1; for i = 1:n a = a*x; y = y + a; endfor 2. x( x < 1) ɛ y =1+x + x 2 + x k <ɛ 3. n n n! =1 2 3 n
14 1 Octave y = 1; for i = 1:n y = y*i; endfor x = 1:n; y = prod(x) % x 4. n, r(n r) nc r = n(n 1)(n 2) (n r +1) 1 2 3 r 5. x ɛ ɛ (1) y =1+x + x 2 /2! + x 3 /3! + x =2 y 7.38906 (2) y =1 x 2 /2! + x 4 /4! + x =2 y 0.41615 (3) y = x x 3 /3! + x 5 /5! + x =2 y 0.90930 6. ɛ ɛ (1) y = 1 1 2 + 1 2 3 + 1 + (= 1) 3 4 (2) y = 1 2 1 3 + 1 2 3 1 3 5 + 1 2 3 4 (= 1 3 5 7 + π ) 2 1.8 1.8.1 x(i) A(i,j) n [ ] x = 1 2 n
1.8. 15 x = []; for i=1:n x(i) = i; endfor x = x x = 1:n Octave octave:1> x = linspace(0,1,5) % [0, 1] (5-1) x = 0.00000 0.25000 0.50000 0.75000 1.00000 octave:2> y = logspace(-1,1,5) % [1e-1, 1e+1] (5-1) y = 0.10000 0.31623 1.00000 3.16228 10.00000 n Jordan J n (λ) Jordan λ 1 0 0 λ 1 0 J 3 (λ) = 0 λ 1 J 4 (λ) = 0 λ 1 0 0 0 λ 1 0 0 λ 0 0 0 λ n=input( Enter n ); lambda=input( Enter lambda ); J=eye(n,n)*lambda; for i=1:n-1 J(i,i+1) = 1; endfor J 1.8.2 A
16 1 Octave d=size(a) A 3 4 d = 3 4 d(1)=3 d(2)=4 A Z Z=zeros(size(A)) A 1 S S=ones(size(A)) 1.8.3 A B C [ ] C = A B C = [A B] C = [ A B ] C = [A;B] A i X X = A; X(:,i)=[] A i X X = A; X(i,:)=[]
1.8. 17 A i j X X = A; X(:,i:j)=[] 1.9 ẋ(t) =Ax(t)+Bu(t) (1.4) A(n n) B(n r) U c := [ B AB A n 1 B ] (1.5) (A, B) 0 1 0 1 A = 0 0 1, B = 0 1 0 0 1 n=input( Enter n ); A=input( Enter A ); B=input( Enter B ); Uc=B; X=B; for i=1:n-1 X=A*X; Uc=[Uc X]; endfor Uc Enter n 3 Enter A [0 1 0; 0 0 1; 1 0 0] Enter B [1;0;1] Uc = 1 0 1 0 1 1 1 1 0
18 1 Octave 1. n [ ] 1 2 3 1 2 R 1 =1 R 2 = R 3 = 0 4 5 0 3 0 0 6 2. v =[ 1 1 1 1 ] diag(v,1), diag(v,-1), diag(v,2), diag(v,-2) 3. n 1 2 0 D =... 0 n n = input( Enter n ); v = 1:n; D = diag(v,0) 4. ẋ(t) = Ax(t) y(t) = Cx(t) } (1.6) A(n n) C(m n) C CA U o :=. CA n 1 (1.7) (A, C) 0 1 0 [ ] 1 0 0 A = 0 0 1, C = 1 0 0 U o = 0 1 0 1 0 0 0 0 1
1.8. 19 5. ẋ(t) = Ax(t)+Bu(t) y(t) = Cx(t) } (1.8) n rank U c = n, rank U o = n (1.9) 0 1 0 0 0 A = 0 0 0 1 0 1 0 0, B = 0 [ ] 0, C = 1 0 1 0 1 0 0 1 1 rank U c =3, rank U o =4 6. A(n n), x(n 1) x(k +1)=Ax(k), x(0) = x 0, k =0, 1,... k =0, 1,...,t f A, x 0,t f [ ] [ ] A = 0.8 1 1, x 0 = 0 0.8 1, t f =20 x = input( Enter x ); tf = input( Enter tf ); A = [0.8 1; 0 0.8] y = []; for i = 1:tf+1 y(:,i) = x; x = A*x; endfor y 7. y(k +2)+a 1 y(k +1)+a 0 y(k) =0, y(0) = y 0, y(1) = y 1, k =0, 1,... y(k), k=0, 1,...,t f a 0 =0.25, a 1 = 1, y(0) = 1, y(1) = 1, t f =20
20 1 Octave 8. n 1, 1, 2, 3, 5, 8,... 1 = 2 =1 i i 2 i 1 n 9. x n [ ] y = 1 x x 2... x n 10. [0, 1] n x 1,x 2,x 3,..., x n n=input( Enter n ); x = rand(1,n) xmax = max(x) xmin = min(x) y = sort(x) % 1 n % % % 11. [0, 1] n x 1,x 2,x 3,..., x n sort 1.9 sin, cos sin(t) t y = []; i = 0; for t = 0:0.1:10 i = i + 1; y(i) = sin(t); endfor y = y
1.9. 21 1.3: sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) atan2(y,x) sinh(x) cosh(x) tanh(x) exp(x) log(x) log10(x) sqrt(x) abs(x) real(x) imag(x) sign(x) sin x cos x tan x arcsin x arccos x arctan x 4 sinh x cosh x tanh x e x ln x log 10 x x x t = 0:0.1:10; y = sin(t); Octave t, y grid "on" plot(t,y) 1.1 1.3 1.10 y(t) =e 0.5t cos 5t (1.10) t [0, 10] 0.01
22 1 Octave 1 line 1 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8-1 0 2 4 6 8 10 1.1: y =sint t = 0:0.01:10; y = exp(-0.5*t).*cos(5*t); grid "on" plot(t,y).* 1.2 1 line 1 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8 0 2 4 6 8 10 1.2: y = e 0.5t cos 5t 1.11 1.3 f(x) = 4k 13 15 (sin x + sin 3x + sin 5x + ) π (1.11)
1.9. 23 n [ 10, 10] 0.02 f(x) k 2π π 0 π 2π k x 1.3: 1.5 line 1 1 0.5 0-0.5-1 -1.5-10 -5 0 5 10 1.4: 1.11 k =1,n=5 k = input( Enter k ); n = input( Enter n ); x = -10:0.02:10; y = zeros(size(x)); a = 1; for i=1:n y = y + sin(a*x)/a; a = a + 2; endfor
24 1 Octave y = y*4*k/pi; grid "on" plot(x,y) 1. 1.5 f(x) = π 2 4 (cos x + π 13 2 cos 3x + 15 2 cos 5x + ) (1.12) n [ 10, 10] 0.02 f(x) π 2π π 0 π 2π π x 1.5: 1.10 1.4 1.10.1 Octave p(x) = x 4 +20x 2 20x + 5 (1.13) octave:1> p = [-1 0 20-20 5] =0 roots
1.10. 25 1.4: roots poly poly polyval polyvalm polyfit conv deconv residue octave:2> r = roots(p) r = -4.92606 3.89858 0.57356 0.45393 octave:3> p1 = poly(r) p1 = 1.0000e+00-2.2760e-15-2.0000e+01 2.0000e+01-5.0000e+00 poly A 0 1 0 A = 1 0 0 0 1 2 octave:4> A = [0 1 0; -1 0 0; 0-1 2]; octave:5> p2 = poly(a) p2 = 1-2 1-2 p 2 (x) = xi A = x 3 2x 2 + x 2
26 1 Octave p(x) x =1 polyval octave:6> y = polyval(p,1) y = 4 1.12 polyval p(x) = x 4 +20x 2 20x +5 x [ 5, 5] 0.02 p =[ -1 0 20-20 5]; x = -5:0.02:5; y = polyval(p,x); grid "on" plot(x,y) 200 line 1 150 100 50 0-50 -100-150 -200-250 -6-4 -2 0 2 4 6 1.6: p(x) = x 4 +20x 2 20x +5 1.10.2 polyfit n [ ] x = 0 1 2 3 4 5 6 7 8 9 10
1.11. 27 [ y = 0 3 4 10 18 27 41 59 79 93 101 ] n n = input( Enter n ); x = 0:10; y = [0 3 4 10 18 27 41 59 79 93 101]; p = polyfit(x,y,n) x1 = 0:0.1:10; y1 = polyval(p,x1); grid "on" plot(x,y, +,x1,y1) % (x,y) + o,*,x % (x1,y1) n =3 1.7 120 line 1 line 2 100 80 60 40 20 0 0 2 4 6 8 10 1.7: 1.11 1.13 n x 1,x 2,,x n x σ 2 x = 1 n x i (1.14) n i=1
28 1 Octave σ 2 = 1 n (x i x) 2 (1.15) n i=1 x [ ] x := x 1 x 2 x n x x, σ 2 heikin.m function [a, b] = heikin(x) % a: b: n = length(x); % x a = sum(x)/n; % sum(x): b = sum((x-a).^2)/n; endfunction octave:1> x = rand(1,5) x = 0.70536 0.31711 0.52546 0.80152 0.14361 octave:2> [mean,sigma2] = heikin(x) mean = 0.49861 sigma2 = 0.058838.m function help M MATLAB endfunction
1.12. 29 1.12 fprintf fscanf C C octave:1> x = 0:0.1:1; octave:2> y = [x; exp(x)]; octave:3> fid = fopen("exp.dat, w ); octave:4> fprintf(fid, %6.2f %12.8f \n,y); octave:5> fclose(fid); exp.dat 0.00 1.00 0.10 1.11 0.20 1.22 0.30 1.35 0.40 1.49 0.50 1.65 0.60 1.82 0.70 2.01 0.80 2.23 0.90 2.46 1.00 2.72 exp.dat a octave:6> fid = fopen("exp.dat"); octave:7> a = fscanf(fid, %f %f,[2 inf]); octave:8> a = a ; octave:9> fclose(fid); % It has two rows now. fopen "r" [2 inf] 2
[1] MATLAB http://www.ecs.shimane-u.ac.jp/ kyoshida/matlab.htm, 2001. [2] John W. Eaton: GNU Octave A high-level interactive language for numerical computations Edition 3 for Octave version 2.0.5, http://www.octave.org/doc/octave toc.html, 1997. [3] 1978. [4] 30 30 1988 [5] 1987. [6] pp.470-477, 2000. [7] 1987. [8] 2000. [9] 2001. [10] Octave/Matlab 2000. Octave 2002 2 1 c