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 Center および File ExchangeMathematics についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by