傅里叶级数-在Matlab中绘制 [英] Fourier series - Plot in Matlab

查看:817
本文介绍了傅里叶级数-在Matlab中绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is my m-file for Fourier series plot:

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

问题是,它是如此缓慢!为了提高速度,我应该在代码中进行哪些更改?

Problem is, it is so slow! What should I change in my code in order to inrease speed?

关于macduff的评论: 像这样吗?

In reference to macduff's comment: Something like this?

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解释的字符串的东西(我猜现在是这样).然后在每个循环中,在调用者的工作区中评估字符串.

I would try and get away from the symbolic toolbox. Try something that has the an and bn expressions input as a string that can be interpreted by Matlab(I'm guessing it is even now). Then every loop, evaluate the string in the caller's workspace.

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中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆