在Scipy与MATLAB中的集成产生不同的结果

时间:2018-11-11 15:44:50

标签: python matlab numpy scipy integration

我想集成2维功能,但是当我使用Python和MATLAB进行操作时,会得到不同的结果。两种编程语言的实现如下:

  1. MATLAB:

       
    function [v] = RaisedC(t,alpha,trunc)
    T=1;
    if abs(t)<=trunc
      if abs(abs(alpha * t / T) - 0.5) > 1e-5
        v=sinc(t/T) .* cos(pi .* alpha .* t ./ T) ./ (1 - (2 .* alpha .* t ./ T).^2); % # typical case
      else
        v=sinc(t/T) .* pi .* sin(pi .* alpha .* t ./ T) ./ (8 .* alpha .* t ./ T);  % L'Hopital limit for alpha * tau = 0.5
      end
    else
      v=0;
    end
    
    str='RaisedC';
    alpha=0.25;
    trunc=10;
    snr=15;
    sir=10;
    L=1;
    n=21;
    dblquad(@An_test, 0,1, 0, 2*pi, 1e-6,[], str,alpha,trunc,snr,sir,L,n)
    end
    
    function R = An_test(Tau, Alpha, str,alpha,trunc,snr,sir,L,n)
    fh=str2func(str);
    Rs = 10^(snr/20);
    Ri= Rs/(sqrt(L)*10^(sir/20));
    P=10;
    w=2*pi/60;
    R=1;
      for kT=-P:1:P
        R=R.*[cos(n.*w.*fh(-Tau-kT,alpha,trunc).*datasample([1 -1],1).*Ri.*cos(Alpha)).*cos(n.*w.*fh(-Tau-kT,alpha,trunc).*datasample([1 -1],1).*Ri.*sin(Alpha))];
      end
    end
    
  2. Python

    def RC(t,alpha,trunc):
        v=np.float64(0)
        T=1.0;
        if np.absolute(t)<=trunc:
            if np.abs(np.abs(alpha * t / T) - 0.5) > 1e-5:
                v=np.sinc(t/T) * np.cos(np.pi * alpha * t / T) / (1 - np.power((2 * alpha * t / T),2)) # typical case
            else:
                v=np.sinc(t/T) * np.pi * np.sin(np.pi * alpha * t / T) / (8 * alpha * t / T)  # asdasdL'Hopital limit for alpha * tau = 0.5
        else:
            v=0 # asd
        return v
    
    def An(Tau_i,Alpha_i,str,alpha,trunc,snr,sir,L,n):
        Rs = 10**(snr/20);
        Ri = Rs/(np.sqrt(L)*10**(sir/20));
        P=10;
        w=2*np.pi/60;
        fh=eval(str)
        T=1
        y=np.float64(1)
        for k in range(-P,P+1):
            y=np.float64(y*np.cos(n*w*fh((-Tau_i-k*T),alpha,trunc)*(np.random.randint(0,2)*2-1)*Ri*np.cos(Alpha_i))*np.cos(n*w*fh((-Tau_i-k*T),alpha,trunc)*(np.random.randint(0,2)*2-1)*Ri*np.sin(Alpha_i)))
        return(y)
    
    str='RC'
    alpha=0.25
    trunc=10
    snr=15
    sir=10
    L=1
    n=21
    I = dblquad(An, 0,1,lambda x: 0, lambda x: 2*np.pi,args=    (str,alpha,trunc,snr,sir,L,n),epsabs=int(1e-12),epsrel=int( 1e-6))
    print(I)    
    

我的工作量非常小,积分的差异使最终结果有很大差异。另外,我在MATLAB中使用符号变量实现了函数integral2的实现,但其结果与dblquad的实现相同。

关于如何解决此问题的任何想法?

0 个答案:

没有答案