Plotting fourier series partial sums
Show older comments
Hi. I am struggling to get my plot for my partial sums correct. I have an
A0 = 0.9
An even and odd = 0
Bn even and odd = -3./(pi*n).*(cos(pi*n*1.5./2.5))-1
I didn't expect my partial sums to look like it does (below).
Any ideas why it is coming out the way it is.
This is my code:
A = 3; % Amplitude (Dataset 5)
L = 5; % Period (Dataset 5)
dutycycle = 30;
sampling = 0.01;
t = 0 : sampling : (L-sampling); % Define time vector
y(t<L*(dutycycle/100)) = A;
y(t>L*(dutycycle/100) & (t<L)) = 0;
x = 0:0.01:2*pi;
n = 25;
Fn = fourier(n,x);
plot(x,Fn)
title ('Partial Sums')
grid on
xlabel('X');
ylabel('Fn');
function Fn = fourier(n,x)
x = x(:);
a0 = 0.9;
a = zeros(1,n);
b = zeros(1,n);
for i = 1:2:n
b(i) = -3/(pi*i)*(cos(pi*i*1.5/2.5))-1;
end
Fn = a0/2 + sum(a.*cos((1:n).*x)+b.*sin((1:n).*x),2);
end
9 Comments
Paul
on 4 Jan 2024
Double check the derivations of A0, A(n), and B(n). Fourier Series (eqn 5)
Why is the plot from 0 to 2*pi when L = 5?
Double check the expression for Fn. Here's one source: Fourier Series (eqn 2)
AB29
on 4 Jan 2024
Paul
on 4 Jan 2024
I got different answers than you for A0, A(n), and B(n). Did you compute those by hand or using Matlab? If the latter, you're likely to get more help if you post the code.
The expression for Fn is not the same as in the Wikipedia link I provide.
If your function has period L, the fourier series and its coefficients are computed differently.
((eq.2) and (eq.5) with P = L).
By the way:
b(i) = -3/(pi*i)*(cos(pi*i*1.5/2.5))-1;
cannot be the sin Fourier coefficients of a function - they must converge to 0 as i -> Inf. Yours converge to -1.
AB29
on 4 Jan 2024
I get
syms n x
A_0 = 1/5 * int(3,x,0,1.5)
A_n = 2/5 * int(3*cos(2*pi/5*n*x),x,0,1.5)
B_n = 2/5 * int(3*sin(2*pi/5*n*x),x,0,1.5)
x = -7:0.01:7;
n = 25;
Fn = fourier(n,x);
plot(x,Fn)
title ('Partial Sums')
grid on
xlabel('X');
ylabel('Fn');
function Fn = fourier(n,x)
x = x(:);
a0 = 0.9;
a = 3./(pi*(1:n)).*sin(3*pi*(1:n)/5);
b = 6./(pi*(1:n)).*(sin(3*pi*(1:n)/10)).^2;
Fn = a0 + sum(a.*cos(2*pi/5*(1:n).*x)+b.*sin(2*pi/5*(1:n).*x),2);
end
Answers (1)
To compute and plot the partial sums of a Fourier series based on the coefficients you've provided (A0 = 0.9, An = 0, and Bn given by a specific formula), Calculates these coefficients and then uses them to compute the Fourier series up to the nth term.
% Define the given parameters
A0 = 0.9; % DC offset
L = 5; % Period
dutycycle = 30; % Duty cycle in percent
sampling = 0.01; % Sampling rate
n = 25; % Number of terms in the partial sum
% Define the time vector
t = 0 : sampling : (L - sampling);
% Initialize the output signal
y = zeros(size(t));
% Define the signal based on the duty cycle
y(t < L * (dutycycle / 100)) = 3; % Amplitude is 3 for the duty cycle
% Define the x values for plotting the Fourier series
x = 0 : 0.01 : 2 * pi;
% Calculate the Fourier series up to the n-th term
Fn = fourierSeries(A0, n, x);
% Plot the result
plot(x, Fn)
title('Partial Sums of Fourier Series')
grid on
function Fn = fourierSeries(n, x, A0)
Fn = A0 / 2; % Start with A0 term
for k = 1:n
An = 0; % Given An = 0
Bn = -3 / (pi * k) * (cos(pi * k * 1.5 / 2.5)) - 1; % Calculate Bn based on the formula
Fn = Fn + An * cos(k * x) + Bn * sin(k * x); % Sum up the series
end
end
Note
Adjust the function as needed based on your actual series formula and make sure that the fourierSeries function is consistent with the series you want to compute.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems
- Electrical and Electronics Engineering
Categories
Find more on Uniform Distribution (Continuous) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




