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

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

Risposte (2)

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 Commenti

Hi thanks for the help. I have added in my values. However i am still getting an error code here.
My bn Value is
Any ideas what I may have done wrong.
Once again thank you so much for the help
For reference here are some of the other figures used.
Please include your code fragments as plain ascii text, not as a picture.
Otherwise we cannot use it to reproduce problems you have or results you got.
Apologies. I’ll update my comments soon.
% Define the basic parameters
a0 = 1.5;
T = 2*pi; % Period of the Fourier series, change according to your function
t = 0 % Time variable from 0 to 2*T with 1000 points
% Function to calculate bN for odd N, replace this with the actual formula
A = 3;
Bn= (-A./(pi*n)).*(cos(pi*n)-(cos(0)))
% Your formula for bN when N is odd goes here
% For example, a common form might be: bN = (-1)^(someFunction(N))/N;
Bn= (-A./(pi*n)).*(cos(pi*n)-(cos(0))) % Placeholder, replace with actual formula
% 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
subplot(3,1,1);
Function definitions in a script must appear at the end of the file.
Move all statements after the "fourierPartialSum" function definition to before the first local function definition.
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');
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;
Here is my code.

Accedi per commentare.

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 Commenti

Thank you so much for the help.
If i was to change my bn to this
b(i) = -3/(pi*i)*(cos(pi*i*1.5/2.5)-1);
Would I need to change my FN function.
This time i have values for bn when even too so I am assuming I modify 1:2:n to 1:1:n
Just change the b values in the loop.
Your picture says
for i = 1:n
b(i) = -3/(pi*i)*cos(pi*i*1.5/2.5)-1;
end
your code line says
for i = 1:n
b(i) = -3/(pi*i)*(cos(pi*i*1.5/2.5)-1);
end
Decide yourself which one is correct.
Thanks. Can see my mistake with the brackets.
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 il 4 Gen 2024
Modificato: Torsten il 4 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Prodotti

Release

R2023b

Richiesto:

il 2 Gen 2024

Modificato:

il 4 Gen 2024

Community Treasure Hunt

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

Start Hunting!

Translated by