Transcription of MATLABの基本的な使い方 - math.twcu.ac.jp
1 matlab 2017 4 18 : matlab n matlab n Scilab Octave n 2 matlab 3 u >> a=1, b=2, c=a+b a = 1. b = 2. c = 3. >> a=1+2+3 .. +4+5 a = 15 u matlab 4 function c = test1(a,b) c = a + b u M-file >> edit filename >> edit >> test1(a,b) ans = 3. matlab 5 u n % % n >> a=1, b=3; n matlab C 6 >> x=[1; 2; 3] x = 1. 2. 3. : : linspace >> linspace(1,9,5) ans = 1.
2 3. 5. 7. 9. >> A=[1 2 3; 4 5 6; 7 8 10] A = 1. 2. 3. 4. 5. 6. 7. 8. 10. 7 A i j A(i,j) A = 1. 2. 3. 4. 5. 6. 7. 8. 10. >> A(2,2) ans = 5. A m,:) A(:,n) >> A(2,:) ans = 4. 5. 6. >> A(:,1) ans = 1. 4. 7. 8 >> A=[1 2 ;3 4] A = 1. 2. 3. 4. >> B=[5 6 ;7 8] B = 5. 6. 7. 8. + >> A + B ans = 6. 8. 10. 12. - >> A - B ans = - 4. - 4. - 4. - 4. * >> A * B ans = 19. 22. 43. 50. A >> A^2 ans = 7. 10. 15. 22. ' >> A' ans = 1. 3. 2. 4. A, B inv(A) * B >> A B ans = -3. -4. 4. 5. / A * inv(B) >> A / B ans = 3. - 2. 2. - 1.
3 Windows A .* B Mac option+ 9 ~ && || shift + n matlab l u sin(x), log(x) l l n >> x = ; y = sin(x) >> [y1,y2,..] = func(x1,x2,..) 10 x y sin( ) y 1. >> edit test2 2. 3. 4. >> a = 5; b = 2; c = 1; [r,s] = test2(a,b,c) 11 function [x,y] = test2(a,b,c) x = a + b; y = a c; 12 function [x,y] = test2(a,b,c) x = a + b; y = a c; : test2 function u a b a + b, a b, a b, a / b test3 13 14 matlab >> x=[1 2] x = 1.
4 2. >> sin(x) ans = >> cos(x) ans = - >> tan(x) ans = - >> log(x) ans = 0. >> exp(x) ans = 15 A = 1. 2. 3. 4. >> R=inv(A) R = - 2. 1. - m n 1 >> C=ones(2,3) C = 1. 1. 1. 1. 1. 1. m n >> z=zeros(2,1) z = 0. 0. m n >> E=eye(2) E = 1. 0. 0. 1. (1) 16 >> x=linspace(0,2*pi,100); >> plot(x,sin(x)); >> plot(x,cos(x)); plot Figure plot Figure Save as jpg (2) 17 >> ezplot('sin(x)',[0,3]) ezplot Figure hold on hold off >> hold on >> ezplot('sin(x)',[0, ]) >> ezplot('cos(x)',[0, ]) (1) 18 02468100246810 2 1 >> t=linspace(0,10,40); >> [x,y]=meshgrid(t,t); >> z=sin(x)+cos(y/2); >> mesh(x,y,z).
5 T meshgrid xy z=sin(x)+cos(y/2) mesh (2) 19 >> ezplot3('sin(t)','cos(t)','t ) ezplot3 ezplot3(x,y,z,[min,max]) min < t < max x = x(t), y = y(t),z = z(t) 0 < t < 2 : 20 zeros ones eye diag magic 1 n^2 n n rand A = rand(n) (0,1) n n randn A = randn(n) n n linspace y = linspace(a,b) a b. 100 y y = linspace(a,b,n) a b n y : 21 length n = length(x) x (mldivide) X = A B AX = B X X = inv(A) * B Windows / (mrdivide) X = A / B XB = A X X = A * inv(B) det rank lu [L,U,P] = lu(A) P*A = L*U L U P qr [Q,R] = qr(A) A m n A = Q*R m n R m m Q eig d = eig(A) A [X,D] = eig(A) X D.
6 22 sin exp e log10 log sqrt round abs min x min(x) x A min(A) A max x max(x) A A max(A) A mean A mean(A) A A mean(A) A std s = std(x) x (1) X std(X) X sum x sum(x) A sum(A) A 23 matlab matlab M func1 24 A=[3 2 7; 1 6 3; 1 0 6]; b=[3 ;-2 ;1]; x=A b >> edit Ax=b function y = mean1(x) n = length(x); y = sum(x)/n; x >> linear1 x = >> x=[1 4 7 10] x = 1 4 7 10 >> mean1(x) ans = >> edit Mac option+ 25 for for end while While end if, elseif, else if end switch, case case switch case otherwise end for 26 for X Y Z 1 for X=Y:Z end X Y Z W for X=Y:W:Z end i 1 10 1 i for i=1:10 disp(i).
7 % disp(x) x end for i=1:2:10 1 10 2 10 for i=10:-1:1 10 1 -1 1 for 27 for i=1:n disp(i); end for i=n:-1:1 disp(i); end function s = sum1(a) n = length(a); s = 0; for i=1:n s = s + a(i); end >> x = [1 4 5 2 7]; >> t = sum1(x) >> n = 10; >> for1 >> n = 10; >> for2 for 1. n 1 n 1 2. 1 n for4 3. dot1 28 >> n=7; >> for3 1 3 5 7 while 29 k = 10; while k > 1 k = k/2; disp(k) end while k 1 k/2 >> while1 5 if 30 x 10 k if x >= 10 k = k + 1; end if else elseif if elseif else end if 31 if a > 4 disp(b); end function c = if2(a,b) if a > b c = a + b; else c = a - b; end >> a = 3; b = 5; >> c = if2(a,b) >> a = 3; b = 5; >> if1 switch 32 function evenodd(n) switch mod(n,2) case 0 disp( even number'); case 1 disp( odd number').
8 Otherwise disp( error'); end case mod(x,y) x mod y 2 0 even 1 odd (1) 33 matlab f(x) = e-x sin(10x) >> f=inline('exp(-x)*sin(10*x) ) f(3) >> f(3) ans = (2) 34 f(x) x f(x) * >> f=inline('exp(-x).*sin(10*x)') x >> x=[1 2 3]; >> f(x) ans = f(x) [0,7] >> x=0 :7; >> plot(x,f(x)) 1. : matlab 2. : matlab /Scilab 35