a0 =
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
採用された回答
その他の回答 (6 件)
Aijalon Marsh
2020 年 10 月 30 日
編集済み: Walter Roberson
2020 年 10 月 31 日
4 件のコメント
Walter Roberson
2020 年 10 月 31 日
Why do you have n*P*x/P ? The code would seem to make more sense if the /P were not there?
Aijalon Marsh
2020 年 10 月 31 日
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
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))
After that you can do things like rewrite(F1,'tan') and expand() and simplify()
5 件のコメント
Aijalon Marsh
2020 年 10 月 31 日
Aijalon Marsh
2020 年 10 月 31 日
Walter Roberson
2020 年 10 月 31 日
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.)
Aijalon Marsh
2020 年 10 月 31 日
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?
Abdallah
2023 年 12 月 30 日
編集済み: Walter Roberson
2023 年 12 月 30 日
clear
clc
syms t
t0 = 0
T=3
w = 2*pi / T
x= exp(-t)
a0= (1/T)*int(x,t,t0,t0+T)
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))
ezplot(xx,[t0 t0+T])
grid
Guna
2024 年 4 月 2 日
0 投票
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)
% 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)
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)
simplify(expand(rewrite(bn,'cos')))
signal_n = (an*cos(2*Pi/T*n*x) + bn*sin(2*Pi/T*n*x))
assume(n,'positive');
assumeAlso(n,'integer');
signal_n = simplify(signal_n)
signal = a0 + sum(subs(signal_n,n,1:50));
fplot(signal,[-pi pi])
カテゴリ
ヘルプ センター および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







