golden.m~

来自「optimization using golden section search」· M~ 代码 · 共 55 行

M~
55
字号
%xmin - returns this%fxmin - this%n - and thisfunction[xmin,fxmin,n] = golden (xl,xu,maxit,tol,g)% xl = 0.5;% xu = 2;% maxit = 100;% tol = 0.001;% f= x^3 - 2*sin(x);R = (5^0.5 - 1)/2;d = R.* (xu - xl);x1 = xl + d;x2 = xu - d;f1 = feval(g,x1);f2 = feval(g,x2);if f1 > f2    xopt = x1;    fx = f1;else    xopt = x2;    fx = f2;endwhile (ea > tol & n<maxit)    d = R*d;    if f1 > f2        xl = x2;        x2 = x1;        x1 = xl + d;        f2 = f1;        f1 = feval(g,x1);    else         xu = x1;        x1 = x2;        x2 = xu - d;        f1 = f2;        f2 = f);    end    n = n +1;    if f1 > f2        xopt = x1;        fx = f1;    else        xopt = x2;        fx = f2;    end    if xopt ~= 0;        ea = (1.-R)*abs((xu-xl)/xopt)*100;    endendxmin = xopt;fxmin = fx;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?