Expansion in a Fourier Series

388 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2020-10-26
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 个评论
Aijalon Marsh
Aijalon Marsh 2020-10-26
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.
Walter Roberson
Walter Roberson 2020-10-26
% 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.

请先登录,再进行评论。

更多回答(6 个)

Aijalon Marsh
Aijalon Marsh 2020-10-30
编辑:Walter Roberson 2020-10-31
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 个评论
Walter Roberson
Walter Roberson 2020-10-31
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.
Aijalon Marsh
Aijalon Marsh 2020-10-31
ok well I chaged that but the bn is still wron and now so is the an

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-10-31
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 个评论
Aijalon Marsh
Aijalon Marsh 2020-10-31
so can I just use a subs method to that sine into cosine
Walter Roberson
Walter Roberson 2020-10-31
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?

请先登录,再进行评论。


Aijalon Marsh
Aijalon Marsh 2020-10-31
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.

Abdallah
Abdallah 2023-12-30
编辑:Walter Roberson 2023-12-30
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 2024-4-2,15:46
Find the Fourier series expansion of f (x) = mod(x) in [-pi,pi] up to 4 harmonic.

Paul
Paul 2024-4-2,18:34
% 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])

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by