clear; // *** 一次元ガウス分布 *** function y = func(x) mu = 3; sigma = 1; y = -1 / sqrt(2*%pi*sigma^2) * exp(-1 * ((x - mu) .^ 2) ./ (2*sigma^2)) // y = cos(x) endfunction // *** 数値微分 *** function y = dfunc(x) h = 1E-4; y = (func(x+h) - func(x-h)) ./ (2*h) endfunction // *** グラフのプロット *** X = linspace(0,6); Y = func(X); dY = dfunc(X); subplot(2,1,1); plot(X, Y); subplot(2,1,2); plot(X, dY); // *** 最小値の計算 *** // 停止条件 err = 1E-3; a = 0.5; // 初期値 x = 1; y = func(x); dx = dfunc(x); subplot(2,1,1); plot(x, y, ".r"); subplot(2,1,2); plot(x, dx, ".r"); // *** 最小値の計算 *** while abs(dx) > err x = x - a * dx; dx = dfunc(x); y = func(x); subplot(2,1,1); plot(x, y, ".r"); subplot(2,1,2); plot(x, dx, ".r"); end // *** 計算結果 *** x subplot(2,1,1); plot(x, y, "xk");