Expansion in a Fourier Series

I created a code that is supposed to calculate a0, an, bn, and f(x), for some reason it won't work when I include cos(n*pi)=(-1)^n to cos(-n*pi)=cos(n*pi). I want these three rules to apply while the code is running cause it's need to calculate an and bn correctly. Below is the code I have so far can someone please help fix this code so I calculate for all four functions.
MatLab Code Below:
% Problem_1 the Fourier series of the function f(x)
% f(x)=0 -pi < x < 0
% f(x)=1 0 < x < pi
clear all;clc;
syms x n pi
% pi=3.14;
sum=0;
y=0 %function you want
y1=1
a0=1/pi*int(y,x,-pi,0)+1/pi*int(y1,x,0,pi)
% for n=1:50
%finding the coefficients
cos(n*pi)=(-1)^n
sin(pi*n)=0
cos(-n*pi)=cos(n*pi)
an=(1/pi)*int(y*cos(n*x),x,-pi,0)+(1/pi)*int(y1*cos(n*x),x,0,pi)
bn=(1/pi)*int(y*sin(n*x),x,-pi,0)+(1/pi)*int(y1*sin(n*x),x,0,pi)
sum=sum+(an*cos(n*x)+bn*sin(n*x))
% end

 Accepted Answer

cos(n*pi)=(-1)^n
that is an assignment statement. n is a symbolic variable. The only time that can have NAME(SYMBOLIC_EXPRESSION) on the left side of an assignment statement is if you are defining a symbolic function. So you are defining a symbolic function named cos with variable name n*pi. But that is an invalid variable name unless pi happens to equal 1.
Perhaps you want to use subs()

4 Comments

Hey so I tried the subs () method you suggested it didin't show an error but it just restated that cos(n*pi)=(-1)^n. I wanted cos(n*pi)=(-1)^n, sin(pi*n)=0, and cos(-n*pi)=cos(n*pi)tobe applied when calculatitng an and bn any other suggestions?
subs(an, {cos(n*pi), sin(pi*n), cos(-n*pi)}, {(-1)^n, 0, cos(n*pi)})
Note that this would be to match literal "n" and would not be a pattern match. It would, for example, not match cos(5*pi) to generate (-1)^5 .
Myself, instead of making these substitutions, I would call simplify() or perhaps rewrite(), as it already knows about these equivalences.
If you need to match the patterns like cos(5*pi) then you need to work with mapSymType, which can be a bit of a PITA and typically needs auxillary routines that use feval(symengine) to do anything useful.
Is there a certain line where I should incorporate this line of code you provided cause when I placed it right above the function an its that line thats returning an error.
% Problem_1 the Fourier series of the function f(x)
% f(x)=0 -pi < x < 0
% f(x)=1 0 < x < pi
syms x
syms n integer
syms pi %just a symbol, NOT the value
% pi=3.14;
total = sym(0);
y = sin(x); %function you want
y1 = 1;
a0 = 1/pi*int(y,x,-pi,0)+1/pi*int(y1,x,0,pi);
an = (1/pi)*int(y*cos(n*x),x,-pi,0)+(1/pi)*int(y1*cos(n*x),x,0,pi);
bn = (1/pi)*int(y*sin(n*x),x,-pi,0)+(1/pi)*int(y1*sin(n*x),x,0,pi);
total = total + (an*cos(n*x)+bn*sin(n*x));
disp(char(total))
piecewise(n == -1 | n == 1, sin(x)*((pi/2 - sin(2*pi)/4)/pi + (2*sin(pi/2)^2)/pi) - cos(n*x)*(sin(pi)^2/(2*pi) - sin(n*pi)/(n*pi)), n ~= -1 & n ~= 1, sin(n*x)*((sin(pi*(n - 1))/(2*(n - 1)) - sin(pi*(n + 1))/(2*(n + 1)))/pi + (2*sin((n*pi)/2)^2)/(n*pi)) + cos(n*x)*((sin((pi*(n - 1))/2)^2/(n - 1) - sin((pi*(n + 1))/2)^2/(n + 1))/pi + sin(n*pi)/(n*pi)))
newtotal = subs(total, {cos(n*pi), sin(pi*n), cos(-n*pi)}, {(-1)^n, 0, cos(n*pi)});
disp(char(newtotal))
piecewise(n == -1 | n == 1, sin(x)*((pi/2 - sin(2*pi)/4)/pi + (2*sin(pi/2)^2)/pi) - (cos(n*x)*sin(pi)^2)/(2*pi), n ~= -1 & n ~= 1, sin(n*x)*((sin(pi*(n - 1))/(2*(n - 1)) - sin(pi*(n + 1))/(2*(n + 1)))/pi + (2*sin((n*pi)/2)^2)/(n*pi)) + (cos(n*x)*(sin((pi*(n - 1))/2)^2/(n - 1) - sin((pi*(n + 1))/2)^2/(n + 1)))/pi)
If you look carefully at the results, you will see that a couple of sin(n*pi) have been replaced with 0.
However, notice that some sin(pi*(n-1)) and sin(pi*(n+1)) are left in.
As I indicated earlier, the replacement is strictly exact, and is not the general pattern of finding sin(pi*integer) or cos(pi*integer) and processing that -- that requires significantly more advanced use of MATLAB.

Sign in to comment.

More Answers (6)

Aijalon Marsh
Aijalon Marsh on 30 Oct 2020
Edited: Walter Roberson on 31 Oct 2020
Hey I was writing this code for expansion in fouries series I have this code but the bn coefficient for some reason it wont integrate cause once calculated it should eqaul (1-cos(n*pi))/(n*pi) but for some reason it wont integrate can someone please help me fix this issue.
% Problem_1 the Fourier series of the function f(x)
% f(x)=0 -pi < x < 0
% f(x)=1 0 < x < pi
syms x n
syms n integer
P = pi
y = 0
y1= 1
a0=(1/P)*int(y,x,-P,0)+(1/P)*int(y1,x,0,P)
an=(1/P)*int(y*cos(n*x),x,-P,0)+(1/P)*int(y1*cos(n*x),x,0,P)
bn=(1/P)*int(y*sin(n*x),x,-P,0)+(1/P)*int(y1*sin(n*x),x,0,P)
F1=symsum(an*cos(n*P*x/P)+bn*sin(n*P*x/P),n,1,Inf)
total = (a0/2)+F1
Pretty(total)

4 Comments

Why do you have n*P*x/P ? The code would seem to make more sense if the /P were not there?
Because of the thats how to calculate the f(x) as depicted in this image
an and bn are defined in terms of cos(n*pi/p*x) and sin(n*pi/p*x) but your code has cos(n*x) and sin(n*x) . When p = pi the two are the same, but if you are going to bother with the *P/P in F1 then you should be consistent in the other equations, and use pi instead of P where appropriate. This is just to make your code clearer.
ok well I chaged that but the bn is still wron and now so is the an

Sign in to comment.

syms x real
syms n integer
P = sym(pi);
y = 0;
y1= 1;
a0=(1/P)*int(y,x,-P,0)+(1/P)*int(y1,x,0,P);
an=(1/P)*int(y*cos(n*x),x,-P,0)+(1/P)*int(y1*cos(n*x),x,0,P);
bn=(1/P)*int(y*sin(n*x),x,-P,0)+(1/P)*int(y1*sin(n*x),x,0,P);
F0 = an*cos(n*P*x/P)+bn*sin(n*P*x/P);
syms N integer
assume(-P < x & x < P)
F1 = simplify(symsum(subs(F0,n,2*N),N,1,inf) + symsum(subs(F0,n,2*N+1),N,1,inf));
disp(char(F1))
piecewise(x == 0, 0, x ~= 0, -(exp(-x*1i)*1i - atan(exp(-x*1i)*1i) + atan(exp(x*1i)*1i) - exp(-x*1i)*exp(x*2i)*1i)/pi)
After that you can do things like rewrite(F1,'tan') and expand() and simplify()

5 Comments

but bn is still not integrating because the integration of sine is -negative cosine yet the answer bn is coming to is sine.
Also does the version matter cause your using R2020b but Im using R2020a
I get the same output for F1 on R2020a.
The bn integral works out when you remember the substitution that sin(x)^2 = 1-cos(x)^2, so whether you choose to represent by 1-cos(x)^2 or by sin(x)^2 depends upon which constant of integration you introduce (only to have it cancel out later.)
so can I just use a subs method to that sine into cosine
Why do you care that bn is being represented in one of its equivalent forms that does not happen to be the one that you were expecting, considering that the later steps reason about the values without problem?

Sign in to comment.

Aijalon Marsh
Aijalon Marsh on 31 Oct 2020
Because when I did the hand calculations for this problem after sovling the definite integral for bn I got (1-cos(n*pi))/(n*pi) so since matlab is just some super calculator I figured that it should come to the exact same conclusion I came to.
clear
clc
syms t
t0 = 0
t0 = 0
T=3
T = 3
w = 2*pi / T
w = 2.0944
x= exp(-t)
x = 
a0= (1/T)*int(x,t,t0,t0+T)
a0 = 
for k=1:20
a(k)=(2/T)*int(x*cos(k*w*t),t,t0,t0+T);
end
for k=1:200
b(k)=(2/T)*int(x*sin(k*w*t),t,t0,t0+T);
end
xx=a0+sum(a.*cos(k*w*t)) + sum(b.*sin(k*w*t))
xx = 
ezplot(xx,[t0 t0+T])
grid
Guna
Guna on 2 Apr 2024
Find the Fourier series expansion of f (x) = mod(x) in [-pi,pi] up to 4 harmonic.
% Problem_1 the Fourier series of the function f(x)
% f(x)=0 -pi < x < 0
% f(x)=1 0 < x < pi
%syms x n pi
% pi=3.14;
syms x n
Pi = sym(pi);
y=0; %function you want
y1=1;
T = 2*Pi;
a0=1/T*int(y,x,-Pi,0)+1/T*int(y1,x,0,Pi)
a0 = 
% for n=1:50
%finding the coefficients
%cos(n*pi)=(-1)^n
%sin(pi*n)=0
%cos(-n*pi)=cos(n*pi)
%an=(1/pi)*int(y*cos(n*x),x,-pi,0)+(1/pi)*int(y1*cos(n*x),x,0,pi)
%bn=(1/pi)*int(y*sin(n*x),x,-pi,0)+(1/pi)*int(y1*sin(n*x),x,0,pi)
an=(2/T)*int(y*cos(2*Pi/T*n*x),x,-Pi,0)+(2/T)*int(y1*cos(2*Pi/T*n*x),x,0,Pi)
an = 
bn=(2/T)*int(y*sin(2*Pi/T*n*x),x,-Pi,0)+(2/T)*int(y1*sin(2*Pi/T*n*x),x,0,Pi)
bn = 
simplify(expand(rewrite(bn,'cos')))
ans = 
signal_n = (an*cos(2*Pi/T*n*x) + bn*sin(2*Pi/T*n*x))
signal_n = 
assume(n,'positive');
assumeAlso(n,'integer');
signal_n = simplify(signal_n)
signal_n = 
signal = a0 + sum(subs(signal_n,n,1:50));
fplot(signal,[-pi pi])

Community Treasure Hunt

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

Start Hunting!