Plotting Fourier Series (first 5, 15, 25 partial sums, (3 Plots))

42 views (last 30 days)
Hi there. I am a very new inexperienced user of MAtlab and have been asked to plot the first 5, 15, 25 partial sums across 3 plots for my fourier series.
Fourier series values shared below - any help with simple anotated breakdown would be appreciated.
  • a0 = 1.5
  • aN odd and even = 0
  • bN odd=
  • bN even = 0

Answers (2)

Hassaan
Hassaan on 2 Jan 2024
Assuming you get the formula for bN​ (odd), here's one of the approach you can use in MATLAB:
% Define the basic parameters
a0 = 1.5;
T = 2*pi; % Period of the Fourier series, change according to your function
t = linspace(0, 2*T, 1000); % Time variable from 0 to 2*T with 1000 points
% Function to calculate bN for odd N, replace this with the actual formula
function bN = calculate_bN(N)
% Your formula for bN when N is odd goes here
% For example, a common form might be: bN = (-1)^(someFunction(N))/N;
bN = 1/N; % Placeholder, replace with actual formula
end
% Function to calculate the partial sum of the Fourier series
function F = fourierPartialSum(t, N)
F = a0 / 2; % Start with a0/2
for n = 1:N
if mod(n,2) == 1 % If n is odd
bN = calculate_bN(n);
F = F + bN*sin(n*t); % Update the sum with the nth term
end
% Since aN and bN for even are 0, those terms are skipped
end
end
% Calculate and plot the first 5, 15, and 25 partial sums
figure;
subplot(3,1,1);
plot(t, fourierPartialSum(t, 5));
title('First 5 Partial Sums');
subplot(3,1,2);
plot(t, fourierPartialSum(t, 15));
title('First 15 Partial Sums');
subplot(3,1,3);
plot(t, fourierPartialSum(t, 25));
title('First 25 Partial Sums');
  • Formula for bN: You need to provide the actual formula for bN when N is odd. Replace the placeholder function calculate_bN(N) with your specific formula.
  • Time Domain and Period: Adjust the time variable t and the period T according to the specific function you're analyzing.
  • Resolution: The number of points in the linspace function determines the resolution of the plot. Increase the number of points for a smoother curve.
------------------------------------------------------------------------------------------------------------------------------------------------
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
  7 Comments
AB29
AB29 on 3 Jan 2024
A = 3; % Amplitude (Dataset 5)
L = 5; % Period (Dataset 5)
dutycycle = 50;
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;

Sign in to comment.


Torsten
Torsten on 3 Jan 2024
Edited: Torsten on 3 Jan 2024
You should be able to add the missing plots.
x = 0:0.01:2*pi;
n = 25;
Fn = fourier(n,x);
plot(x,Fn)
function Fn = fourier(n,x)
x = x(:);
a0 = 1.5;
a = zeros(1,n);
b = zeros(1,n);
for i = 1:2:n
b(i) = -3/(pi*i)*(cos(pi*i)-cos(0));
end
Fn = a0/2 + sum(a.*cos((1:n).*x)+b.*sin((1:n).*x),2);
end
  5 Comments
AB29
AB29 on 4 Jan 2024
Edited: AB29 on 4 Jan 2024
Hi once again. After looking at my code and the graph produced I still feel this does not follow the trend I expect to see. This is what I get. The peaks seem too high.
I am dealing with an A0 = 0.9
An even and odd = 0
Bn even and odd = -3/(pi*i)*(cos(pi*i*1.5/2.5))-1
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
However I expected to see something more along the lines of the original trend too?
Please let me know your thoughts any help is much appreciated.
Torsten
Torsten on 4 Jan 2024
Edited: Torsten on 4 Jan 2024
The code I gave you is for a 2*pi periodic function (P = 2*pi). Its Fourier expansion is described by (eq.2) and (eq.5) under
If you have a different period P, the Fourier series and its coefficients are computed differently (again see (eq.2) and (eq.5)).
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.

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!