值
两种⽅法:
1、求导的⽅法:syms x y;
>>
y=x^3+x^2+1
>>
diff(y)
ans =3*x^2 + 2*x
>>
solve(ans)
ans
=-2/3
极值有两点。
同时也是最值;
2、直接⽤最⼩值函数:
求最⼤值,既求-y的最⼩值:
>>
f=@(x)(-x^3-x^2-1)
f =@(x)(-x^3-x^2-1)
>>
x=fminunc(f,-3,3)%
在-3;-3范围内Warning: Gradient must be provided for
trust-region method; using line-search method
instead. > In fminunc at
354Optimization terminated: relative infinity-norm of gradient less
than options.TolFun.
x =
-0.6667
>> f(x)
ans =
-1.1481在规定范围内的最⼤值是1.1481由于函数的局限性,
例⼦:syms x
f=(200+5*x)*(0.65-x*0.01)-x*0.45;
s=diff(f);%⼀阶导数
s2=diff(f,2);%⼆阶导数
h=double(solve(s));%⼀阶导数为零的点可能就是极值点,注意是可能,详情请见⾼数课本for
i=1:length(h)
if
subs(s2,x,h(i))<0
matlab求导disp(['函数在' num2str(h(i))
'处取得极⼤值,极⼤值为' num2str(subs(f,x,h(i)))])
elseif
subs(s2,x,h(i))>0
disp(['函数在' num2str(h(i))
'处取得极⼩值,极⼩值为'
num2str(subs(f,x,h(i)))])
else
disp(['函数在' num2str(h(i))
'处⼆阶导数也为0,故在该点处函数可能有极⼤值、极⼩值或⽆极值'])%%%详情见⾼数课本end
end
发布评论