aasdadwe 2021-09-12 15:19 采纳率: 85.7%
浏览 76
已结题

如何使用python计算fractional integrals and derivative

img

img


如何用python计算和表示这两个公式?
第一个是fractional integrals (分数阶积分)
第二个是fractional derivatives(分数导数)

  • 写回答

1条回答 默认 最新

  • 技术专家团-Joel 2021-09-13 14:16
    关注

    同学,你好,我是matlab专栏下的专家。你的这种算法在matlab上早已实现,而且代码也很简单,你可以看看:

    可以把代码放在这里
    积分

    function output = fint(x, alpha, n, h)
    % FINT Calculate the integral of x
    %    x      Sampled function
    %    alpha  Fractional order
    %    n      Size of the memory
    %    h      Sampling rate
    %    Please refer to http://www3.nd.edu/~msen/Teaching/UnderRes/FracCalc1.pdf
    %    See equ. 36
    %    Read section 4 p. 19: since gamma(z) where z > 172, MATLAB gives inf
    %    Therefore, the size of memory is limited by  0 < n < 171
    N = length(x);
    output = zeros(1, N);
    h_a = h^alpha;
    for i = 1 : N
        sigma = 0;
        for m = 0 : 1 : n
        
        if i - m < 1
            break;
        end
            
            sigma = sigma +  gamma(alpha + m) / (factorial(m) * ...
                    gamma(alpha)) * x(i - m);
        end
        output(i) = sigma * h_a;
    end
    end
    

    微分

    function output = fderiv(x, alpha, n, h)
    % FDERIVE Calculate the derivative of x
    %    x      Sampled function
    %    alpha  Fractional order
    %    n      Size of the memory
    %    h      Sampling rate
    %
    %    Please refer to http://www3.nd.edu/~msen/Teaching/UnderRes/FracCalc1.pdf
    %    See equ. 32
    N = length(x);
    output = zeros(1, N);
    h_a = h^alpha;
    for i = 1 : N
        sigma = 0;
        for m = 0 : 1 : n
        
        if i - m < 1
            break;
        end
            
            sigma = sigma + (-1)^m * gamma(alpha + 1) / (factorial(m) * ...
                    gamma(alpha - m + 1)) * x(i - m);
        end
        
        output(i) = sigma / h_a;
    end
    end
    

    计算微分和积分

    ts = 0.01;
    n = 170;
    t = 0:ts:2;
    x = t;
    figure;
    hold on;
    plot(t, x)
    x_d = fderiv(x, 1, n, ts);
    x_i = fint(x, 1,   n, ts);
    plot(t, x_d)
    plot(t, x_i)
    legend('x', 'xd', 'xint')
    xlabel('t');
    ylabel('x(t)');
    

    当然转化成python也很容易
    转化后的例子

    import numpy as np
    from math import gamma, factorial
    def fint(x, alpha, n, h):
        N = len(x)
        output = np.zeros(N)
        h_a = h**alpha
        for i in range(N):
            sigma = 0
            for m in range(n+1):
                if(i-m<0):
                    break 
                sigma = sigma + gamma(alpha + m) / (factorial(m)*gamma(alpha)) * x[i-m]
            output[i] = sigma*h_a
        return output
    
    def fderiv(x, alpha, n, h):
        N = len(x);
        output = np.zeros(N)
        h_a = h**alpha
        for i in range(N):
            sigma = 0
            for m in range(n+1):
                if (i - m < 0 or alpha - m + 1<=0):
                    break
                sigma = sigma + ((-1)**m) * gamma(alpha + 1) / (factorial(m) * gamma(alpha - m + 1)) * x[i-m]
            output[i] = sigma / h_a
        return output
    
    ts = 0.01
    n = 170
    t = np.arange(0,2,ts)
    x = t
    
    from matplotlib import pyplot as plt
    
    x_i = fint(x, 1,  n, ts);
    x_d = fderiv(x, 1,  n, ts);
    plt.plot(t, x, 'r-', t, x_i, 'b--', t, x_d,'c-.')
    plt.legend(['x', 'x_{int}', 'x_{deriv}'])
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月21日
  • 已采纳回答 9月13日
  • 创建了问题 9月12日

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据