gpt4 book ai didi

matlab - 傅里叶级数 - 在 Matlab 中绘图

转载 作者:行者123 更新时间:2023-12-04 18:06:10 32 4
gpt4 key购买 nike

这是我的傅立叶级数图的 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

最佳答案

我会尝试摆脱符号工具箱。尝试将 anbn 表达式输入为可以由 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

关于matlab - 傅里叶级数 - 在 Matlab 中绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25979541/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com