clc;clear;
syms x
a = -10;
b = 10;
f =3*x^4-16*x^3+30*x^2-24*x+8;
dfdx =diff(f,x);
while(1)
x0 = 1/2*(a+b);
dfdx0 = subs(dfdx,x,x0);
if abs(dfdx0)<1e-4
break;
end
if dfdx0>0
b = x0;
else if dfdx0<0
a = x0;
end
end
end
fprintf('平分法求得的极小值为%9.9f\n',x0)
程序输出结果:
平分法求得的极小值为2.000007629