Expansion in a Fourier Series

79 views (last 30 days)
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

Walter Roberson
Walter Roberson on 26 Oct 2020
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
Walter Roberson
Walter Roberson on 26 Oct 2020
% 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 (3)

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
Aijalon Marsh
Aijalon Marsh on 31 Oct 2020
ok well I chaged that but the bn is still wron and now so is the an

Sign in to comment.


Walter Roberson
Walter Roberson on 31 Oct 2020
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
Walter Roberson
Walter Roberson on 31 Oct 2020
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.

Community Treasure Hunt

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

Start Hunting!

Translated by