文科系のための線形代数・解析

2.1変数および演算

a = pi
format long
a
format
a

format long… long型固定小数点形式というらしい。
format…デフォルトの4桁表現に戻る。

vpa(a)
vpa(a, 30)
vpa(a, 50)

vpa…精度を変える。引数に精度を指定しなければMATLABの内部表現(16桁)の倍精度である32桁で出力される。


2.2ベクトルの定義

x1 = [1 2 3 4 5]
y1 = [1; 2; 3; 4; 5]
x = 0:0.1:1.0;

 x1のように空白「」かカンマ「,」で区切ると横ベクトルが、y1のようにセミコロン「;」で区切ると列ベクトルができる。
 xは0から1.0まで0.1刻みで11個の要素から成る行ベクトル。行の一番後ろに「;」を書くことで出力されない。


2.3行列の定義

A=[1 2 3; 6 5 4; 16 25 36]
B=[1 2 3; 4 5 6; 7 8 9]
B.'

3行3列の正方行列A,Bを定義した。
B.'で転置、B'だと共役転置。

A*B
A.*B

A*B…行列としての掛け算
A.*B…要素同士の掛け算


2.4変数の定義

x = 0:0.01:2;
clf
f = sin(x); g = cos(x);
plot(x,f); hold on 
plot(x,g)
xlabel( 'x' ); ylabel( 'sin(x)' )
title( 'sin(x)' )
legend( 'sin(x)', 'cos(x)' )
grid on

定義した行ベクトルxは0から2まで0.01刻みで21個の要素から成る。
clf…現在のFigureウィンドウをクリアする
hold on…既存のグラフにプロットを追加する
plot…各点を直線で結ぶ。引数を指定することで色やラインスタイル、マーカータイプが変更できる。
legend…個々のラインを簡単に識別できる凡例を追加する。
grid on…グリッドラインを表示する。
※セミコロンを入れることで複数の命令を1行で書けるようになっている。


2.5初等関数

2.5.1べき乗とべき乗根

clf    %現在のFigureウインドウをクリア
x = (0 : 0.001 : 1);    %0,1間で0.001刻みの行ベクトルxを定義
y1 = x.^1; plot(x, y1); hold on
y10 = x.^10; plot(x, y10); hold on
y100 = x.^100; plot(x, y100); hold on
y01 = x.^0.1; plot(x, y01); hold on
y001 = x.^0.01; plot(x, y001); hold on
         %様々なべき関数(べき指数は正)を定義し、同一グラフにプロット
xlabel( 'x' ); ylabel( 'x^n' ); title( 'plot of x^n' )
legend( 'x^1', 'x^{10}', 'x^{100}', 'x^{0.1}',  'x^{0.01}', 'Location', 'west')

legendの'Location' 'West' で凡例の位置を座標軸の西方向に指定した。
べき指数は正なので全て右上がりのグラフになる。

clf        %現在のFigureウインドウをクリア
x = (0 : 0.001 : 1);               %0,1間で0.001刻みの行ベクトルxを定義
ym1 = x.^(-1); plot(x, ym1); hold on
ym5 = x.^(-5); plot (x, ym5); hold on
ym10 = x.^(-10); plot(x, ym10); hold on
ym05 = x.^(-0.5); plot(x, ym05); hold on
ym01 = x.^(-0.1); plot(x, ym01); hold on
  %様々なべき関数(べき指数は負)を定義し、同一グラフにプロット
ylim([0 10])
xlabel( 'x' ); ylabel( 'y' ); title( 'plot of x^{-n}' )
legend( 'x^{-1}' , 'x^{-5}', 'x^{-10}', x^{-0.5}', 'Location', 'north')
 

べき指数は負なので全て右下がりのグラフになる。

2.5.2三角関数

clf
x = -2*pi : pi/100 : 2*pi;
y1 = sin(x); plot(x, y1, 'LineWIdth', 2)
hold on;
       %LineWildthで軸のライン幅を2に指定
y2 = cos(x); plot(x, y2, ':', LineWidth', 2)
hold on;
        %':'で点線になる 
y3 = tan(x); plot(x, y3, ' . ', 'LineWidth', 2)
hold on;
ylim([-3 3]); xlim([-2*pi 2*pi]); grid on
xlabel('x'); ylabel( 'sin(x), cos(x), tan(x)' );
title( 'Plot of Sine, Cosine & Tangent Function' )
legend( 'sin', 'cos', 'tan', 'FontSize', 15) 


3.1指数関数

clf
x = linespace(-2, 4)
    %区間[-2,4]で等間隔の点を100個含むベクトルx
plot(x, 0.5.^x)
hold on
plot(x, 1.0.^x)
hold on
plot(x, 1.5.^x)
hold off
legend('0.5^x', '1^x', '1.5^x')
grid on
clf
x = linspace(0,10);
semilogy(x,x.^2)
      %片対数グラフ(yが対数)
hold on
semilogy(x,2.^x)
hold off
legend('x^2','2^x')
grid on

 y = 2^xのグラフは縦軸を対数で取ると傾き2の直線になる。

clf
x = linspace(1,10);
loglog(x,x.^2)
   %両対数グラフ
hold on
loglog(x,2.^x)
hold off
legend('x^2','2^x')
grid on

 y = x^2のグラフは両対数プロットすると傾き2の直線になる。


3.2逆関数

clf
x = linspace(-5,5);
plot(x,x) % 斜め45度の線
hold on
plot(x,2*x+1)
plot(x,(x-1)/2)
hold off
legend('x','2x+1','(x-1)/2')
daspect([1 1 1]) % 縦軸と横軸のスケールを同じにする
grid on

y = 2x+1の逆関数x=(x-1)/2


3.3対数関数

clf
x = linspace(0.2,15);
plot(x,log(x)/log(0.1))
hold on
plot(x,log(x)/log(2))
plot(x,log(x))
plot(x,log(x)/log(10))
hold off
legend('log_{0.5}x','log_2 x','log x','log_{10}x')
grid on

対数関数は底が1より小さいとき、単調減少になる。

clf
x = linspace(0.2,15);
semilogx(x,log(x)/log(0.1))   
       %片対数グラフ(xが対数)
hold on
semilogx(x,log(x)/log(2))
semilogx(x,log(x))
semilogx(x,log(x)/log(10))
hold off
legend('log_{0.5}x','log_2 x','log x','log_{10}x')
grid on

対数関数は横軸で対数を取ると底の対数の逆数を傾きとした直線となる。


3.4関数の極限

f = @(x) (x.^2-1) ./ (x-1);
clf
x = linspace(0,2);
plot(x,f(x))
hold on
plot(x,1./(x-1))
hold off
ylim([-4,8])
legend('(x^2-1)/(x-1)','1/(x-1)')
grid on

 f(x) = (x^2-1)/(x-1) のx→1の時の極限は、分子を因数分解して2。
g(x) = 1/(x-1)のx→1の時の極限は存在しない。


3.6多項式近似とテイラー展開

clf
x = linspace(-0.8,1);
plot(x,1./(1+x))
hold on
plot(x,1-x)
plot(x,1-x+x.^2)
plot(x,1-x+x.^2-x.^3)
hold off
ylim([0 4])
legend('f(x)','1st','2nd','3rd')

f(x) = 1/(x+1) のx=0周りでの多項式近似


3.7二変数関数のプロット

clf
[x,y] = meshgrid(-5:0.2:5); 
length(y)
z = x .^ 2 + y .^ 2;
surf(x, y, z)

mashgrid x,yともに-5から5間で0.2刻みのベクトル
surf 三次元プロット

clear
syms x y     
f(x, y) = x^2 + y^2;
fsurf(f)

syms シンボリック数を作成。
fsurf シンボリックな関数の3次元プロット

clf
[x,y] = meshgrid(-5:0.2:5);
z = x .^ 2 + y .^ 2;
contour(x, y, z)
daspect([1 1 1]) % 縦軸と横軸のスケールを同じにする

contour 等高線をプロット。四番目の引数を入れると等高線の数を指定できる。

clf
syms x y
f(x, y) = x^2 + y^2;
fcontour(f)
daspect([1 1 1]) % 縦軸と横軸のスケールを同じにする

fcontour シンボリック関数の等高線プロット

clf
[x,y] = meshgrid(-5:0.2:5);
z = x .^ 2 + y .^ 2;
contourf(x, y, z)
daspect([1 1 1]) % 縦軸と横軸のスケールを同じにする
colorbar;

countourf 塗りつぶした等高線

clf
[x,y] = meshgrid(-5:0.2:5);
z = x .^ 2 + y .^ 2;
contour3(x, y, z)

countour3 3次元の等高線


4.1論理関係

logical(a) aがゼロなら0を、ゼロでないなら1を返す
& かつ
| または
~ 否定


4.2 if文、for文

a = randi(100)
if a < 50
   disp('small')
else
   disp('large')
end  

randi(M) 1~Mの整数一様乱数を発生
randi(M,n) 1~Mの整数一様乱数をn×nの正方行列の形で発生
disp   printと一緒かな、多分

B= randi(100,5)
C=B<50 ;
C

これでCがBのlogical配列になるらしい。ほえー

x=0;
tic;
for k=1:1000000
   x=x+0.000001;
   sin(x);
end
toc
tic
x=0.01:0.000001:1;
sin(x);
toc

sinの計算をさせて時間を計測した。ベクトル演算よりもfor文の計算の方が早い。


4.3数式処理


6.1行列の記法

A = [1 2 3 4; 5 6 7 8]

行列は、各要素をコンマ(,)か空白で区切り、セミコロン(;)で次の行に移る。


6.2様々な行列の生成

i = 1; j = 2;
eq(i,j)
i = 1; j = 1;
eq(i,j)

クロネッカーのデルタ関数(単位行列を表すのに使う)はeq関数か==を使う。iとjの値が等しければ1を、等しくなければ0を返す。

A(2,3) = 1;  %Aの2行3列の要素を1とする
A(:,2)  %Aの2列目を抜き出して列ベクトルで返す
A(1,:)   %Aの1行目を抜き出して行ベクトルで返す
A'  %Aの転置(複素行列の場合は注意が必要)
diag(A)  %Aの対角成分を取り出す
E = eye(3)   %Eを3×3の対角行列とする
Z = zero(4)   %Zを4×4のゼロ行列とする
R = rand(4)   %4×4のランダム行列
A = [1 2 3; 2 3 4; 3 4 5]
D = diag(A
B = diag(diag(A))

DはAの対角成分を要素とする列ベクトルで、BはDを対角成分とする対角行列。このようにdiag関数を2回使うことで対角成分以外がゼロになった対角行列が作れる。

R = rand(4)
A = (R + R')

対称行列(転置しても変わらない行列)を作る。

R = rand(4) + 1i * rand(4)
A = (R + R')

自己随伴行列(エルミート行列)を作る。

v = rand(4,1)
U = eye(4) - 2 * v * v' / (v' * v)

直行行列(転置行列と逆行列が等しい行列)を作る。

v = rand(4,1) + 1i * rand(4,1)
U = eye(4) - 2 * v * v' / (v' * v)

ユニタリー行列を作る。


6.3トレース、行列式

A =rand(3)
trace(A)   %トレース(対角成分の和を求める)
det(A)   %行列式を求める


6.4 行列の演算

v = [1.0; 2.0; 1.5]
w = [0.5; 1.5; 1.0]
dot(v,w)
v * w'

dotでベクトルの内積(それぞれの要素ごとに掛け合わせたもの)を求める。結果はスカラー。v'*wと一緒。(vの転置とwの積と見る)
v*w'はベクトルの直積。結果は行列

A = rand(2,3)
B = rand(2,3)
A .* B   %フロベニウス積
kron(A,B)   %クロネッカー積



6.5行列のノルム

v = [1 -2 3]
n = norm(v)
A = rand(2,3)
norm(A,'fro')

ベクトルのノルムはnormで求める。行列のノルムは色々あるが、その一つがフロベニウスノルムである。


6.6逆行列

A = rand(2)
inv(A)


6.7行列関数



6.8ケーリーハミルトンの定理


8.1Cauchy-Schwarzの不等式

v=[4;2] %列ベクトルv
w=[1;1] %列ベクトルw
pv=[zeros(2,1), v] %一列目に原点(0,0),2列目にvが入った2次元配列
pw=[zeros(2,1), w] %一列目に原点(0,0), 2列目にwが入った2次元配列
plot( (pv(1,:),pv(2,:)),'o-' ) %pvの1行目(x座標)と2行目(y座標)をプロット。
hold on
plot( (pw(2,:),pw(2,:)),'o-' ) %pwの1行目(x座標)と2行目(y座標)をプロット。
hold off
xlabel('x');ylabel('y')
legend('v','w')
aspect([1,1,1]) %座標軸の長さを揃える


この記事が気に入ったらサポートをしてみませんか?