matlabの基本的な使い方 - oishi.info.waseda.ac.jptakayasu/classes/slides/matlab_basic.pdf ·...
TRANSCRIPT
-
MATLAB
:
-
MATLAB
n MATLAB
n
ScilabOctave
n
2
-
MATLAB3
u
>> a=1, b=2, c=a+b a = 1. b = 2. c = 3.
>> a=1+2+3 ... +4+5 a = 15
u
-
MATLAB4
function c = test1(a,b) c = a + b
u M-file
>> edit filename >> edit test1.m test1.m
>> test1(a,b) ans = 3.
-
MATLAB5
u n % % n
>> a=1, b=3; n
MATLABC
-
6
>> x=[1; 2; 3] x = 1. 2. 3.
: :
linspace>> linspace(1,9,5) ans = 1. 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
AijA(i,j) A = 1. 2. 3. 4. 5. 6. 7. 8. 10. >> A(2,2) ans = 5. Am,:)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.
Windows
A .* B
Mac option+
-
9
~
&&
|| shift +
-
n MATLAB l
u sin(x), log(x) l l
n >> x = 1.0; y = sin(x) >> [y1,y2,...] = func(x1,x2,...)
10
x y sin(1.0)y
-
1. >> edit test2 2.
3. test2.m 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 aba + b, a b, a b, a / b test3
13
-
14
MATLAB>> x=[1 2] x = 1. 2. >> sin(x) ans = 0.8414710 0.9092974 >> cos(x) ans = 0.5403023 - 0.4161468 >> tan(x) ans = 1.5574077 - 2.1850399
>> log(x) ans = 0. 0.6931472 >> exp(x) ans = 2.7182818 7.3890561
-
15
A = 1. 2. 3. 4. >> R=inv(A) R = - 2. 1. 1.5 - 0.5 mn1 >> d=ones(2,3) d = 1. 1. 1. 1. 1. 1.
mn >> d=zeros(2,1) d = 0. 0. mn>> E=eye(2) E = 1. 0. 0. 1.
-
(1)16
>> x=linspace(0,2*pi,100);>> plot(x,sin(x));>> plot(x,cos(x));
plotFigure plot
FigureSave as jpg
-
(2)17
>> ezplot('sin(x)',[0,3])
ezplotFigure hold onhold off
>> hold on >> ezplot('sin(x)',[0,6.28]) >> ezplot('cos(x)',[0,6.28])
-
(1)18
02
46
810
02
46
8102
1.5
1
0.5
0
0.5
1
1.5
2
>> t=linspace(0,10,40); >> [x,y]=meshgrid(t,t); >> z=sin(x)+cos(y/2); >> mesh(x,y,z);
tmeshgridxy 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
(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
-
: 22
sin exp e
log10 logsqrt
round
minAmin(A) A A min(A) A
maxA max(A) A A max(A) A
meanA mean(A) A A mean(A) A
stds = std(X) X (1) Xstd(X) X
sumA sum(A) A sum(A) A
-
23
MATLAB
MATLAB M
func1func1.m
-
24
A=[3 2 7; 1 6 3; 1 0 6]; b=[3 ;-2 ;1]; x=A b
>> edit linear1.mAx=b
function y =mean1(x) n = length(x); y = sum(x)/n;
x
>> linear1 x = 1.6000 -0.5500 -0.1000
>> x=[1 4 7 10] x = 1 4 7 10 >> mean1(x) ans = 5.5000
>> edit mean1.m
Macoption+
-
25
for for end
while while end
if, elseif, else if end
switch, case case
switch case otherwise end
-
for
26
for XYZ1 for X=Y:Z end XYZW for X=Y:W:Z end
i1101i for i=1:10 disp(i); % disp(x)x end
for i=1:2:10 110210
for i=10:-1:1 101-11
-
for
27
for i=1:n disp(i); end
for i=n:-1:1 disp(i); end
for2.m
function s = sum1(a) n = length(a); s = 0; for i=1:n s = s + a(i); end
sum1.m>> x = [1 4 5 2 7]; >> t = sum1(x)
>> n = 10; >> for1
>> n = 10; >> for2
for1.m
-
for1. n1n1
for3.m
2. 1n for4 3. dot1
28
>> n=7; >> for3 1 3 5 7
-
while
29
Eps = 1; while 1+Eps > 1 Eps = Eps/2; end Eps = Eps*2
while
eps1.m
Eps1
Eps/2
1.0
>> eps1 Eps = 2.2204e-16
-
if
30
p.9 x 10k if x >= 10 k = k + 1; end ifelse elseif if elseif else end
-
if if1.m if2.m
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'); otherwise disp(error'); end
case
evenodd.m
mod(x,y) x mod y 2even odd
-
(1)
33
MATLAB f(x) = ex sin(10x) >> f=inline('exp(-x)*sin(10*x)) f(3) >> f(3) ans = -0.0492
-
(2)
34
f(x)xf(x)* >> f=inline('exp(-x).*sin(10*x)') x >> x=[1 2 3]; >> f(x) ans = -0.2001 0.1236 -0.0492 f(x)[0,7] >> x=0:0.01:7; >> plot(x,f(x))
-
1. : MATLAB 2. : MATLAB/Scilab
35