Fourier series - Plot in Matlab -
here m-file 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; n=1:10 %% n 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, slow! should change in code in order inrease speed?
edit: in reference macduff's comment: 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; n=1:10000 %% n 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
i try , away symbolic toolbox. try has an
, bn
expressions input string can interpreted matlab(i'm guessing now). every loop, evaluate string in caller's workspace.
for n=1:10 %% n 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
Comments
Post a Comment