5月18日

代码
f = @(x) x.^2 - sin(x); a = 0; b = 1; tol = 1e-6; x1 = a + 0.382*(b-a); x2 = a + 0.618*(b-a); f1 = f(x1); f2 = f(x2); % Iterate until the interval [a,b] is smaller than the tolerance while abs(b-a) > tol if f1 < f2 b = x2; x2 = x1; f2 = f1; x1 = a + 0.382*(b-a); f1 = f(x1); else a = x1; x1 = x2; f1 = f2; x2 = a + 0.618*(b-a); f2 = f(x2); end end min_point = (a+b)/2; min_value = f(min_point); fprintf('Minimum point: %f\nMinimum value: %f\n', min_point, min_value);
结果


浙公网安备 33010602011771号