Using the fit function, the maximum number of terms that can be made using a Fourier fit in MATLAB is 8:
f = fit(xs,ys,'fourier8')
Unfortunately, an 8-term series isn't going to cut it for my purposes. The goal is to fit a data set with a Fourier model and extract the coefficients, and the coefficients need to be as precise as possible for later analysis. I was wondering if anyone had an idea of how to modify the pre-existing fit function using:
fitoptions('fourier8')
that would create a n-term Fourier series, where n is the number of terms (finite). If this is unfeasible, how one would go about writing a program that creates an n-term Fourier series (finite) with extractable coefficients using the general shown formula in the image below NOTE: THE BOUND WOULD NOT BE INF AND WOULD INSTEAD BE n.
Update: I found code that takes in known coefficients, but how could this be adjusted so that it actively adjusts/fine-tunes the coefficients for n-numbered terms when they are not known prior? Also, how could one find the upper and lower bounds for the period (as those are inputs for this script)?
a0 = input('a0: ');
an = input('an: ');
bn = input('bn: ');
a = input('lower boundary: ');
b = input('upper boundary: ');
t = linspace(a,b,10000); % note: this is just for testing the code
suma =0;
for n=1:100
ebn = evalin('caller',bn);
ean = evalin('caller',an);
suma = suma + (ean.*cos(2.*n.*pi.*t./(b-a)) + ebn.*sin(2.*n.*pi.*t./(b-a)));
end
series = a0 + suma;
plot(t,series)