#########################常用算法——导数与微分的近似计算#######################
###例题:用导数的近似计算求函数f(x) = -4*x^2+3*x+2在[0,1]的极值
f = function(x) -4*x^2+3*x+2
ub = 1
lb = 0
m = 1000000
step = (ub-lb)/m
x = 0
for(i in 1:1000000){
df = (f(x+step)-f(x))/step
if(abs(df)<1e-5){cat("x is ",x,"extreme value is ",f(x))}
x = x+step
}
#矩阵方法
f = function(x) -4*x^2+3*x+2
x = seq(0,1,by = .00001)
x0 = x[which.max(f(x))]
x0;f(x0)
###用导数的近似计算求出函数f(x) = (1/3)*x^3-(1/2)*x^2-2*x在[-2,3]的所有极值点。
#判定方法1
f = function(x) (1/3)*x^3-(1/2)*x^2-2*x
x = -2
step = 1/10000
for(i in 1:50000){
fd = (f(x-step)-f(x))*(f(x+step)-f(x)) #判断是否是极值点
if(fd>0)print(x)
x = x+step
}
#判定方法2——定义判定
f = function(x) (1/3)*x^3-(1/2)*x^2-2*x
ub = 3
lb = -2
m = 5000000
step = (ub-lb)/m
x = -2
for(i in 1:m){
df = (f(x+step)-f(x))/step
if(abs(df)<1e-5)cat("extreme value point is ",x,&#