Finding error and partial sum in a Fourier Series

10 vues (au cours des 30 derniers jours)
Ryan Spragg
Ryan Spragg le 19 Oct 2022
Réponse apportée : Ishu le 29 Août 2023
I am looking to claculate the eror of this fourier series:
The intended goal of this project is to perform the partial sum from 1 to some value of n which yields a root mean square error(RMSE) smaller thatn 10^-6. I have been working alongisde some people and this is what we've developed as our code.
I am not sure how to include the partial sum for each of these values and I am attempting to find a way to extrapolate the value of n where error is smaller than 10^-6.
Initially I had a for loop for with different conditions of n and x but it would not produce a reasonable error.
Any advice on how to simplify this process or extrapolate error would be greatly appreciated!
close all; clc;
L=10;
nx=7000 %% number of data points
nx = 7000
x = (linspace(0,L,nx)).' ;
n_max=1000 %% maximum boundary condition of summation
n_max = 1000
n=1:n_max
n = 1×1000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
f =(((-1).^n)-1).*(cos(n.*x))./((n.^2)*(3.14159))+((1-(2.*(-1).^n)).*(sin(n.*x))./n) ;
f_final=-pi/4+sum(f,2)
f_final = 7000×1
-1.5705 -0.2936 0.2702 0.1342 -0.1200 -0.0950 0.0736 0.0978 -0.0177 -0.0626
plot(x, f_final)
error=sqrt((f(n_max)-f(n_max-1)).^2)./n_max
error = 1.5159e-06

Réponses (1)

Ishu
Ishu le 29 Août 2023
Hi Ryan,
I understand that you are trying to calculate the “Root Mean Square Error smaller than 10^-6 from 1 to some value of n. So, in this case you can use "while" loop, whenever the error is less than 10^-6 then you can break the loop otherwise you can increment the value of n by 1.
To calculate the partial sum you can use "cumsum" function.
I have used the same values of nx and L as provided by you and then calculated the partial sums using these values for every value of n’ starting from 1 until the error comes less than 10^-6.
L=10;
nx=7000;
x = (linspace(0,L,nx)).'; % column vector with 7000 values
n=1;
error_max = 10e-6; % error limit
while true
f =(((-1).^n)-1).*(cos(n.*x))./((n.^2)*(3.14159))+((1-(2.*(-1).^n)).*(sin(n.*x))./n);
f_final = -pi/4+cumsum(f,2); %partial sums
% Calculate the error
mean_value = mean(f_final);
error = sqrt(sum(((f_final - mean_value).^2))./length(f_final)); % Root mean square error function
% Check if the error is smaller than the error limit
if error < error_max
break; % Exit the loop if the error is below the limit
end
n = n + 1; % Increment n for the next iteration
end
error
For further information on cumsum you can refer this documentation:
Hope this helps!

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by