每日总结5.25
五月二十五,周四,
今天的课程并没有那么密集,临近学期末了,大多数的课程都是在机房度过的,我现在已经逐渐习惯与每天都背着电脑上机去了。
今天的上机课是工程数学,这里贴上部分代码
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, :);
浙公网安备 33010602011771号