发帖
日期

Gamma 函数的特殊值计算

gamma 函数在部分点的实现有些问题。例如 gamma(-4) Matlab 选择的是 +Inf,目前北太会给出错误。可以和 Matlab 行为保持一致,或者直接返回 NaN。这样对画图方便一些。目前的实现要画图,就只能手动分段。计算 1/gamma(x) 也变成不连续的了。即 Matlab 可以执行以下代码并画图

  1. gamma([-5 -4 -3 -2 -1 0 5])
  2. = -5:0.01:5;
  3. plot(x, gamma(x))
 Matlab R2023b 输出
  1. >> gamma([-5 -4 -3 -2 -1 0 5])
  2. ans = 
  3.     Inf   Inf   Inf   Inf   Inf   Inf    24
北太 3.1.0 目前会报定义域错误
  1. >> x = -5:0.01:5;
  2. >> plot(x, gamma(x));
  3. 错误使用函数 gamma
  4. domain error
  5. 程序执行中显示有错误信息,请反馈给开发团队。
目前要绘制  gamma 函数只能手动分段绘图:代码如下:
  1. % 生成开区间 (start:stop)
  2. function range=openRange(start, stepstop)
  3.     range = (start+eps(start)):step:(stop-eps(stop));
  4. end
  5. % 绘制 gamma 函数
  6. function gamma_plot()
  7.     step = 0.01;
  8.     x1 = [
  9.         openRange(-5.0step, -4.0)
  10.         openRange(-4.0step, -3.0)
  11.         openRange(-3.0step, -2.0)
  12.         openRange(-2.0step, -1.0)
  13.         openRange(-1.0step, -0.0)
  14.     ]';
  15.     x2 = [ openRange(0.0step5.0) ]';
  16.     
  17.     % draw
  18.     plot(...
  19.         x1,gamma(x1),...
  20.         x2,gamma(x2),...
  21.         'LineWidth',2,...
  22.     );
  23.     grid on;
  24.     xlim([-55]);
  25.     ylim([-1010]);
  26.     title('Gamma(x) Line Plot')
  27.     xlabel('x');
  28.     ylabel('Gamma(x)');
  29. end

Cyhan 2 0 2024-01-27

对于高数里的极限、导数的计算有吗?积分有的

想尝试将高数里的使用MATLAB计算函数极限、导数、积分和求解微分方程转换为使用baltamatica,对于学习高数的学生而言,基本的计算能够解决就可以将此软件推广起来优点一:支持中文变量、中文符号-这个就很符合日常的习惯优点二:软件相比MATLAB可是要小太多啦

匿名 3 0 2023-10-06

abs命令或者变量的类型发生变化导致,无法求值

写了一个自适应 Simpson 求积公式的代码, 运行过程中出现了 abs命令或者变量的类型发生变化导致,无法求值 的错误, 如图所示我写的 adapsimp 的代码如下

  1. function [s, err] = adapsimp(func, a, b, tol)
  2.     s = comsimp(func, a, b, 2);
  3.     c = (a + b) / 2;
  4.     s1 = comsimp(func, a, c, 2);
  5.     s2 = comsimp(func, c, b, 2);
  6.     s12 = s1 + s2;
  7.     err = abs(s12 - s) / 15;
  8.     if err < tol
  9.         s = s12;
  10.     else
  11.         [s1, err1] = adapsimp(func, a, c, tol/2);
  12.         [s2, err2] = adapsimp(func, c, b, tol/2);
  13.         s = s1 + s2;
  14.         err = err1 + err2;
  15.     end
  16. end
里面用到了 comsimp 函数, 是这样写的
  1. function s = comsimp(funcabn)
  2.     h = (b - a) / n;
  3.     s0 = func(a) + func(b);
  4.     s1 = 0;     % summation of f(x_{2k-1})
  5.     s2 = 0;     % summation of f(x_{2k})
  6.     for k = 1:n-1
  7.         x = a + k * h;
  8.         if rem(k , 2) == 0
  9.             s2 = s2 + func(x);
  10.         else
  11.             s1 = s1 + func(x);
  12.         end
  13.     end
  14.     s = h * (s0 + 4 * s1 + 2 * s2) / 3;
  15. end
这部分代码在 octave 上运行是没有问题的用的版本是2.2.0最新版的.

邱彼郑楠 1 0 2023-04-30

运行这段在MATLAB中写的迎风法解双曲型PDE时稳定闪退

  1. h=0.01;
  2. r=0.5;
  3. tao=r*h;
  4. x=0:h:5;
  5. t=0:tao:10;
  6. length_t=length(t);
  7. length_x=length(x);
  8. U=zeros(length_t,length_x);
  9. length_in=length_x-1;
  10. A=diag((1-r)*ones(length_in,1))+r*diag(ones(length_in-1,1),-1);
  11. [X,T]=meshgrid(x,t);
  12. F=2*T.*sin(X)+T.^2.*cos(X);
  13. for i=2:length(t)
  14.     U(i,2:end)=A*U(i-1,2:end)'+tao*F(i,2:end)';
  15. end
  16. U_acc=T.^2.*sin(X);
  17. figure(1)
  18. subplot(1,2,1)
  19. mesh(x,t,U)
  20. title("解曲面")
  21. subplot(1,2,2)
  22. mesh(x,t,U_acc-U)
  23. title("误差曲面")
发现是FOR循环内的矩阵转置处出现了问题,但是无法清晰定位具体发生了什么导致的问题,希望开发组能解决。

Dicer 1 0 2023-04-26