PSE@BUCT
ID:000077gamma 函數(shù)在部分點(diǎn)的實現(xiàn)有些問題。
例如 gamma(-4) Matlab 選擇的是 +Inf,目前北太會給出錯誤。
可以和 Matlab 行為保持一致,或者直接返回 NaN。這樣對畫圖方便一些。
目前的實現(xiàn)要畫圖,就只能手動分段。計算 1/gamma(x) 也變成不連續(xù)的了。
即 Matlab 可以執(zhí)行以下代碼并畫圖
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)); 錯誤使用函數(shù) gamma domain error 程序執(zhí)行中顯示有錯誤信息,請反饋給開發(fā)團(tuán)隊。
目前要繪制 gamma 函數(shù)只能手動分段繪圖:
代碼如下:
% 生成開區(qū)間 (start:stop)
function range=openRange(start, step, stop)
range = (start+eps(start)):step:(stop-eps(stop));
end
% 繪制 gamma 函數(shù)
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