Expansion in a Fourier Series
228 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Aijalon Marsh
il 26 Ott 2020
Modificato: Walter Roberson
il 30 Dic 2023
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
0 Commenti
Risposta accettata
Walter Roberson
il 26 Ott 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 Commenti
Walter Roberson
il 26 Ott 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))
newtotal = subs(total, {cos(n*pi), sin(pi*n), cos(-n*pi)}, {(-1)^n, 0, cos(n*pi)});
disp(char(newtotal))
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.
Più risposte (4)
Aijalon Marsh
il 30 Ott 2020
Modificato: Walter Roberson
il 31 Ott 2020
4 Commenti
Walter Roberson
il 31 Ott 2020
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.
Walter Roberson
il 31 Ott 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))
After that you can do things like rewrite(F1,'tan') and expand() and simplify()
5 Commenti
Walter Roberson
il 31 Ott 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?
Abdallah
il 30 Dic 2023
Modificato: Walter Roberson
il 30 Dic 2023
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
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!