matlab - 如何使用matlab快速判断一个函数是否凸?

例如,FX = x ^ 2 + sin (x) 只是出于好奇,我不想使用 CVX 工具箱来执行此操作。

回答1

您可以通过检查二阶导数是否为非负数在某个区间 [a,b] 内进行检查。为此,您必须定义 x-values 的向量,找到数值二阶导数并检查它是否不太负:

a = 0;
b = 1;
margin = 1e-5;
point_count = 100;
f=@(x) x.^2 + sin(x);
x = linspace(a, b, point_count)
is_convex = all(diff(x, 2) > -margin);

由于这是一个数值测试,你需要将参数调整为函数的属性,即如果函数在小范围内做一些疯狂的事情,我们可能无法接受它。例如。使用上面的参数,测试会错误地将函数 f=@(x)sin(99.5*2*pi*x-3) 报告为凸函数。

回答2

clear
syms x real
syms f(x) d(x) d1(x)
f = x^2 + sin(x)
d = diff(f,x,2)==0
d1 = diff(f,x,2)
expSolution = solve(d, x)
if size(expSolution,1) == 0
    if eval(subs(d1,x,0))>0 
        disp("condition 1- the graph is concave upward");
    else
        disp("condition 2 - the graph is concave download");
    end
else
    disp("condition 3 -- not certain")
end

相似文章

随机推荐

最新文章