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

조회 수: 229 (최근 30일)
Ben Hischar
Ben Hischar 2022년 3월 30일
답변: Omega 2023년 9월 20일
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

답변 (1개)

Omega
Omega 2023년 9월 20일
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:

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by