gamma 函数在部分点的实现有些问题。
例如 gamma(-4) Matlab 选择的是 +Inf,目前北太会给出错误。
可以和 Matlab 行为保持一致,或者直接返回 NaN。这样对画图方便一些。
目前的实现要画图,就只能手动分段。计算 1/gamma(x) 也变成不连续的了。
即 Matlab 可以执行以下代码并画图
gamma([-5 -4 -3 -2 -1 0 5]) x = -5:0.01:5; plot(x, gamma(x))
Matlab R2023b 输出
>> gamma([-5 -4 -3 -2 -1 0 5]) ans = Inf Inf Inf Inf Inf Inf 24
北太 3.1.0 目前会报定义域错误
>> x = -5:0.01:5; >> plot(x, gamma(x)); 错误使用函数 gamma domain error 程序执行中显示有错误信息,请反馈给开发团队。
目前要绘制 gamma 函数只能手动分段绘图:
代码如下:
% 生成开区间 (start:stop)
function range=openRange(start, step, stop)
    range = (start+eps(start)):step:(stop-eps(stop));
end
% 绘制 gamma 函数
function gamma_plot()
    step = 0.01;
    x1 = [
        openRange(-5.0, step, -4.0)
        openRange(-4.0, step, -3.0)
        openRange(-3.0, step, -2.0)
        openRange(-2.0, step, -1.0)
        openRange(-1.0, step, -0.0)
    ]';
    x2 = [ openRange(0.0, step, 5.0) ]';
    
    % draw
    plot(...
        x1,gamma(x1),...
        x2,gamma(x2),...
        'LineWidth',2,...
    );
    grid on;
    xlim([-5, 5]);
    ylim([-10, 10]);
    title('Gamma(x) Line Plot')
    xlabel('x');
    ylabel('Gamma(x)');
end