如何使用 trapz 函数与积分函数

问题描述 投票:0回答:1

我的代码使用 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 函数占用了太多内存)。

performance matlab fft numerical-integration
1个回答
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个积分评价点;您需要根据您使用此代码的方式将其修改为适当的值。

© www.soinside.com 2019 - 2024. All rights reserved.