Finding error and partial sum in a Fourier Series
9 views (last 30 days)
Show older comments
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
x = (linspace(0,L,nx)).' ;
n_max=1000 %% maximum boundary condition of summation
n=1:n_max
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)
plot(x, f_final)
error=sqrt((f(n_max)-f(n_max-1)).^2)./n_max
0 Comments
Answers (1)
Ishu
on 29 Aug 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!
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!