傅立叶正弦级数问题:此类变量不支持点索引

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

这是我的代码:

clearvars;
clc; 
close all;

%Define the domain: 
l=1;
%and consider, say, 4 periods
x=linspace(-4,4);
%Define the number of terms to consider
n_tot=50;
%Initialize the vectors containing each term of the series
FSS_term_x=zeros(n_tot,length(x));
%Loop to compute each term of the series up to n_tot
for n=1:n_tot
    %n-th term of the series for f(x)=x
    FSS_term_x(n,:)=((-2.*l.^2./n.pi).*cos(n.*pi)+2.*(l.^2)./(n.*pi).^2.*(cos(n.*pi)-1)).*sin(n.*pi.*x./l);
    %-2.*l./(n.*pi).*cos(n.*pi).*sin(n.*x.*pi/l);
end
%Add up all the terms of the series
FS_y=sum(FSS_term_x,1);
%Plot the series
figure
plot(x,FS_y)

这是错误消息:

Dot indexing is not supported for variables of this type.

Error in untitled3 (line 16)
    FSS_term_x(n,:)=((-2.*l.^2./n.pi).*cos(n.*pi)+2.*(l.^2)./(n.*pi).^2.*(cos(n.*pi)-1)).*sin(n.*pi.*x./l);

发生此错误是因为我尝试绘制的傅里叶正弦级数是分段平滑的吗?

如有任何反馈,我们将不胜感激!

最好的,

RK

helper continuous-fourier
1个回答
1
投票

您在

for
循环中的表达式中错过了乘号 (*)。它涉及右侧的第一个括号内的术语:在您的代码中它显示
(-2.*l.^2./n.pi)
注意末尾的
n.pi
),而它应该是
(-2.*l.^2./n.*pi)

所以,

for
循环中的表达式应该是:

FSS_term_x(n,:)=((-2.*l.^2./n.*pi).*cos(n.*pi)+2.*(l.^2)./(n.*pi).^2.*(cos(n.*pi)-1)).*sin(n.*pi.*x./l);
    %-2.*l./(n.*pi).*cos(n.*pi).*sin(n.*x.*pi/l);
© www.soinside.com 2019 - 2024. All rights reserved.