【问题标题】:How to use the trapz function verses the integral function如何使用 trapz 函数与积分函数
【发布时间】:2014-06-16 06:09:05
【问题描述】:

我的代码使用integral 函数工作,但我希望使用trapz 函数来实现它。

fc = 5*10^6;
fb = 0.2*10^6;
F = [0:10000:10^7];
Tb = 1/fb;
Sequence = [0 1 0 1 1 0 1 1 1 0 0 0 1 0 1 ];
A = sqrt(9/Tb);
w = 2*pi*fc;
I = zeros(1,length(F));
for counter1 = 1:length(Sequence) 
if Sequence ( 1, counter1) == 1   
    s_of_t = @(t) A*cos(w*t)*exp(-1i*2*pi*t*F); 
else
    s_of_t = @(t) -A*cos(w*t)*exp(-1i*2*pi*t*F);
end
    S_of_f = integral(s_of_t,((counter1-1)*Tb),(counter1*Tb),'ArrayValued', true);
for counter2 = 1:length(S_of_f)  
    I( 1, counter2) = I(1,counter2)+S_of_f(1,counter2);
end
    clear S_of_f s_of_t;
end
figure(1)
plot(F,abs(I));

我想要做的是使用 trapz 函数而不是像这样的积分函数:

for n = 1 : length(F)
    S_of_f(n) = trapz(F,s_of_t);
end

代替:

S_of_f = integral(s_of_t,((counter1-1)*Tb),(counter1*Tb),'ArrayValued', true);

我在使用这个函数实现我的代码时遇到了麻烦,所以如果您有任何建议,我将不胜感激。

错误包括: “function_handle”类型的输入参数的未定义函数“max”。 trapz 中的错误(第 43 行) perm = [dim:max(ndims(y),dim) 1:dim-1];

我不确定我缺少哪个函数句柄。

(是的,我正在使用傅里叶变换,但是我尝试使用 FFT 函数占用了太多内存)。

【问题讨论】:

    标签: matlab integration fft performance


    【解决方案1】:

    trapz 需要一个函数评估向量,而不是integral 所需要的函数句柄。

    if Sequence ( 1, counter1) == 1
            s_of_t = @(t,F) A*cos(w*t).*exp(-1i*2*pi*t*F);
        else
            s_of_t = @(t,F) -A*cos(w*t).*exp(-1i*2*pi*t*F);
        end
        for i=1:length(F)
            r=linspace(((counter1-1)*Tb),(counter1*Tb),100);
            S_of_f(i) = trapz(r,s_of_t(r,F(i)));
        end
      ...
    

    我使用trapz任意选择积分的100个评价点;您将需要根据您使用此代码的方式将其修改为适当的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-18
      • 2019-08-31
      • 2021-01-03
      • 1970-01-01
      • 2019-01-09
      • 2020-04-13
      • 1970-01-01
      • 2019-12-04
      相关资源
      最近更新 更多