5.4每日总结
今天学习了matlif,学会了其基本的使用
并且编写了一些程序,如下:
golds.m
function [xmin, fmin, iter] = golds(fun, a, b, tol)
rho = (sqrt(5) - 1) / 2;
x1 = a + (1 - rho) * (b - a);
x2 = a + rho * (b - a);
f1 = fun(x1);
f2 = fun(x2);
iter = [a, b, x1, x2; zeros(floor(log2((b - a) / tol)), 4)];
for k = 1:size(iter, 1) - 1
if f1 < f2
b = x2;
x2 = x1;
x1 = a + (1 - rho) * (b - a);
f2 = f1;
f1 = fun(x1);
else
a = x1;
x1 = x2;
x2 = a + rho * (b - a);
f1 = f2;
f2 = fun(x2);
end
iter(k + 1, :) = [a, b, x1, x2];
if abs(b - a) < tol
break;
end
end
if f1 < f2
xmin = x1;
fmin = f1;
else
xmin = x2;
fmin = f2;
end
iter = iter(1:k + 1, :);
untitled2.m
[umin, fmin, iter] = golds(@(x) x^2 - sin(x), 0, pi, 1e-6);
disp(['极小点 = ' num2str(umin)]);
disp(['极小值 = ' num2str(fmin)]);
disp(iter);