Fourier series calculating n, an and bn as well as plotting it?

234 visualizaciones (últimos 30 días)
Ben Hischar
Ben Hischar el 30 de Mzo. de 2022
Respondida: Omega el 20 de Sept. de 2023
Hi all,
I need to compare the fourier series of given function and then plot and compare it to its orignal function
It is a piecewise function:
f(x) = 2 - (x/2)^2 if 0<= x <2
1 if -2<= x <0
the fourier series has a periodic function of 4

Respuestas (1)

Omega
Omega el 20 de Sept. de 2023
Hi Ben,
To calculate the Fourier series coefficients (an and bn) and plot the Fourier series approximation for the given piecewise function, you'll need to perform the following steps:
  1. Define the Piecewise function and its range and period.
  2. Calculate the Fourier series coefficients (an and bn).
  3. Create a Fourier series approximation using the calculated coefficients.
  4. Plot and compare the original function with its Fourier series approximation.
You can refer to the following MATLAB code:
% Define the period and the range for x
T = 4; % Period of the periodic function
x = -2:0.01:2; % Range of x values
% Define the given piecewise function
f = @(x) (2 - (x/2).^2).*(x >= 0 & x < 2) + (1).*(x >= -2 & x < 0);
% Number of terms in the Fourier series
N = 10;
% Calculate the Fourier series coefficients (an and bn)
a0 = (1/T) * integral(@(x) f(x), 0, T);
an = zeros(1, N);
bn = zeros(1, N);
for n = 1:N
an(n) = (1/T) * integral(@(x) f(x).*cos(n*pi*x/2), 0, T);
bn(n) = (1/T) * integral(@(x) f(x).*sin(n*pi*x/2), 0, T);
end
% Create the Fourier series approximation
F = a0/2;
for n = 1:N
F = F + an(n) * cos(n*pi*x/2) + bn(n) * sin(n*pi*x/2);
end
% Plot the original function and its Fourier series approximation
figure;
plot(x, f(x), 'b', 'LineWidth', 2, 'DisplayName', 'Original Function');
hold on;
plot(x, F, 'r--', 'LineWidth', 2, 'DisplayName', 'Fourier Series Approximation');
xlabel('x');
ylabel('f(x)');
title('Comparison of Original Function and Fourier Series Approximation');
legend('show');
grid on;
You can adjust the value of N to include more or fewer terms in the series for a better approximation.
To know more about Fourier Series, you can refer to the following documentation link:

Categorías

Más información sobre Mathematics en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by