Fourier Series using MATLAB

2 views (last 30 days)
MathWizardry
MathWizardry on 11 May 2021
Answered: Paul on 11 May 2021
MATLAB CODE:
syms x
n=1:1;
k=1*n;
L=pi;
f1(x)=piecewise(x>pi&<0,-x-pi;x>0&<pi,x+i);
a1=int(f1(x),x,-L,L);
a0=a1/(2*pi);
ak=it(f1(x)*cos(k*x),x,-L,L);
bk=int(f1(x)*sin(k*x),x,-L,L);
a=ak/pi;
b=bk/pi;
F1=a0+sum(a.*(cos(k*pix/L))+b.*(sin(k*pi*x/L)));
please correct my codes.
it says that:
f1(x)=piecewise(x>pi&<0,-x-pi;x>0&<pi,x+i);
Invalid use of operator.

Answers (1)

Paul
Paul on 11 May 2021
You probably want
f1(x) = piecewise(x < -sym(pi), 0,x < 0,-x-sym(pi), x<=sym(pi), x+sym(pi), x>sym(pi), 0);
You'll find some other typos in your code, like using "it" instead of "int" in the equation for ak and pix instead of pi*x in the equation for F1.
Recommend defining
syms L
L = sym(pi)
and then use L everywhere instead of pi.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!