Matlab中的Fourier级数图

Matlab中的Fourier级数图,matlab,plot,fft,series,Matlab,Plot,Fft,Series,这是我的傅里叶级数图的m文件: clear clc syms n a0=input('Enter coefficient a0: '); an=input('Enter coefficient an: '); bn=input('Enter coefficient bn: '); a=input('Enter lower boundary: '); b=input('Enter upper boundary: '); t=linspace(a,b,10000); suma=0; for n=1:

这是我的傅里叶级数图的m文件:

clear
clc
syms n
a0=input('Enter coefficient a0: ');
an=input('Enter coefficient an: ');
bn=input('Enter coefficient bn: ');
a=input('Enter lower boundary: ');
b=input('Enter upper boundary: ');
t=linspace(a,b,10000);
suma=0;
for n=1:10 %% n could be any number, bigger n - better approximation
suma=suma+(subs(an,'n',n).*cos(2.*n.*pi.*t./(b-a))+subs(bn,'n',n).*sin(2.*n.*pi.*t./(b-a)));
end
series=a0+suma;
plot(t,series)
grid
问题是,它太慢了!为了提高速度,我应该在代码中更改什么

编辑:参考macduff的评论: 像这样的

clear
clc
a0=input('Enter coefficient a0: ');
an=input('Enter coefficient an: ','s');
bn=input('Enter coefficient bn: ','s');
a=input('Enter lower boundary: ');
b=input('Enter upper boundary: ');
t=linspace(a,b,10000);
suma=0;
for n=1:10000 %% n could be any number, bigger n - better approximation
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series=a0+suma;
plot(t,series)
grid
clear
clc
a0=input('Enter coefficient a0: ');
an=input('Enter coefficient an: ','s');
bn=input('Enter coefficient bn: ','s');
a=input('Enter lower boundary: ');
b=input('Enter upper boundary: ');
t=linspace(a,b,10000);
suma=0;
for n=1:10000 %% n could be any number, bigger n - better approximation
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series=a0+suma;
plot(t,series)
grid

我会试着离开象征性的工具箱。尝试使用
an
bn
表达式作为字符串输入的东西,该字符串可以被Matlab解释(我猜现在也是这样)。然后在每个循环中,计算调用者工作区中的字符串

for n=1:10 %% n could be any number, bigger n - better approximation
  ebn = evalin('caller',bn);
  ean = evalin('caller',an);
  suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
像这样的

clear
clc
a0=input('Enter coefficient a0: ');
an=input('Enter coefficient an: ','s');
bn=input('Enter coefficient bn: ','s');
a=input('Enter lower boundary: ');
b=input('Enter upper boundary: ');
t=linspace(a,b,10000);
suma=0;
for n=1:10000 %% n could be any number, bigger n - better approximation
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series=a0+suma;
plot(t,series)
grid
clear
clc
a0=input('Enter coefficient a0: ');
an=input('Enter coefficient an: ','s');
bn=input('Enter coefficient bn: ','s');
a=input('Enter lower boundary: ');
b=input('Enter upper boundary: ');
t=linspace(a,b,10000);
suma=0;
for n=1:10000 %% n could be any number, bigger n - better approximation
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series=a0+suma;
plot(t,series)
grid

是的,这就是我的意思,希望它有用!现在太快了:)非常感谢!是否可以检查代码是否产生与代码相同的结果?使用输入未定义函数或变量“n”时出错。