如何在matlab中使用for循环进行求和

时间:2016-05-16 13:33:30

标签: matlab

我有这个和Σ((4./pi)*((( - 1)。^ n)/(2 * n + 1))* cos((2 * n + 1)* x)) x = 0到4 * pi和n = 0到24.我想用for循环来计算它的总和。

这是我的代码:

x=4*pi % it does not work when I put x=0:4*pi , matlab says : In an assignment  A(I) = B, the number of elements in B and I must be the same.
for n=0:length(x)
    f(i+1)= (4./pi).*(((-1).^n)./(2*n+1))*cos((2*n+1)*x);

end
xl=sum(f);

如何修复x从0变为4 * pi?

由于

编辑:

 x=0:4*pi;
    for n=0:13
        f(i+1)= (4./pi).*(((-1).^n)./(2*n+1))*cos((2*n+1)*x(n));

    end

    xl=sum(f);

0 个答案:

没有答案
相关问题