我已经为傅立叶级数编写了代码。这是我到目前为止所拥有的:
function FS = FourierSeries(f,degree)
cosCoefficients = zeros(1,degree);
sinCoefficients = zeros(1,degree);
syms x;
a0 = double((1/pi)*int(f,-pi,pi));
for n = 1:degree
cosCoefficients = cosCoefficients + (1/pi)*int(f*cos(n*x),-pi,pi);
sinCoefficients = sinCoefficients + (1/pi)*int(f*sin(n*x),-pi,pi);
end
for n = 1:degree
FS = 0.5*a0 + cosCoefficients.*cos(n*x) + sinCoefficients.*sin(n*x);
end
然后,我还创建了以下函数文件:
function y = func1(x)
syms x
y = (x^2);
end
问题是,当我尝试绘制func1
and时FS1 = FourierSeries(func1, 4)
,我不断收到一条错误消息
数据必须是数字、日期时间、持续时间或可转换为双精度的数组。
我如何绘制这个傅里叶级数?