partial sum of a series

조회 수: 10 (최근 30일)
SSBGH
SSBGH 2022년 5월 6일
편집: Paul 2022년 5월 7일
i have this function and i wrote the following code to calculate the Fourier coefficients
im just stuck with writing a loop which calculate the partial sum of the series till a given number N as well as Plotting the function and the corresponding partial sum (N = 12) on one figure.
can any one help?
  댓글 수: 3
SSBGH
SSBGH 2022년 5월 6일
syms x
f = 1/2*(sin(x)+abs(sin(x)));
%%a0
a0_sym= (1/pi)*int(f,x,-pi,pi);
a0= double(a0_sym)
%%an, bn
for n =1:12;
a_sym(n) = (1/pi)*int(f*cos(n*x),x,-pi,pi);
b_sym(n) = (1/pi)*int(f*sin(n*x),x,-pi,pi);
a(n)=double(a_sym(n))
b(n)=double(b_sym(n))
end
here you go
Walter Roberson
Walter Roberson 2022년 5월 6일
syms x
Pi = sym(pi);
f = 1/2*(sin(x) + abs(sin(x)))
f = 
X = linspace(-Pi, Pi);
F = double(subs(f, x, X));
plot(X, F)
fourier(f, x)
ans = 
f2 = piecewise(x >= 0 & x <= 3*Pi/2, sin(x), zeros(size(x)))
f2 = 
F2 = double(subs(f2, x, X));
plot(X, F2)
fourier(f2)
ans = 
sympref('HeavisideAtOrigin', 0);
f3 = (heaviside(x)-heaviside(x-3*Pi/2))*sin(x)
f3 = 
F3 = double(subs(f3, x, X));
plot(X, F3)
simplify(fourier(f3, x), 'steps', 10)
ans = 
Interesting, it appears that you can get a closed formula -- though you have to work at it a bit.

댓글을 달려면 로그인하십시오.

답변 (1개)

Paul
Paul 2022년 5월 6일
편집: Paul 2022년 5월 7일
Hi SSBGH,
To plot the function, we need a set of x-values to plot over.
As you've done, define the function and the CFS coefficients symbolically
syms x
syms n integer positive % missing from original code!
f = 1/2*(sin(x)+abs(sin(x)));
%%a0
% use sym(pi) when doing symbolic math
Pi = sym(pi);
a0_sym = (1/Pi)*int(f,x,-Pi,Pi); % this equation has been corrected
a_sym(n) = (1/Pi)*int(f*cos(n*x),x,-Pi,Pi)
a_sym(n) = 
b_sym(n) = (1/Pi)*int(f*sin(n*x),x,-Pi,Pi)
b_sym(n) = 
Now define the values of x to make the plot
xvals = -pi:.01:pi;
Now we loop over the CFS coefficients to sum them all up over all values of x
N = 12;
% intialize with a0
fr = double(a0_sym)/2;
for n = 1:N;
a = double(a_sym(n));
b = double(b_sym(n));
% at this point, fill in the RHS
fr = fr + ...
end
Now plot
plot(xvals,1/2*(sin(xvals) + abs(sin(xvals))),xvals,fr)

카테고리

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