MATLAB: Fourier series expansion using the Curve Fitting Tool

curve fittingfourier series representationMATLAB

I have some very perplexing results I obtained using the Curve Fitting Tool which I am having difficulty interpreting and reconciling the results. This is the 1st time I used this tool so I hope someone with some experience can assist me with the results I obtained. I am using MATLAB 2012b.
I used the code below to generate a pressure profile vs time. The time vector & the P results I have attached in the Pdata.mat file.
A=1; p=A*(1-time/0.01).*exp(-time/0.01);
I used the code generation wizard within the Curve Fitting Tool to generate the m-file that is attached. Within that m-file the following initial guess at the Fourier series coefficients:
opts.StartPoint = [7.1054e-15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 12.5664];
I then executed the FIT operation & the results for Fourier series coefficients are in the .txt file. I then copied those results & pasted them into MATLABcurvefit.m to check the results. I got nothing that resembled what is in Pdata.mat. Despite this discrepancy the Curve Fitting Tool generated fitted curve depicted in curve fit results.jpg. The fit depicted here is EXCELLENT!
So I am completely BAFFLED. How does the Curve Fitting Tool show excellent results, yet when I used the results from the .txt file in MATLABcurvefit.m the results are dreadful?
Really would appreciate an explanation.

Best Answer

Almost always when someone complains of exactly this problem, it is because they did not in fact use the correct coefficients. Instead, they copied 4 significant digits, pasted them in. But MATLAB did not estimate 5 digits. So what did you do?
Here are the coefficients from the text file, exactly as you said.
a0 = 2.589e+07 (1.61e+07, 3.567e+07)
a1 = -2.918e+07 (-4.097e+07, -1.739e+07)
b1 = -3.622e+07 (-4.928e+07, -2.317e+07)
a2 = -7.159e+06 (-8.454e+06, -5.865e+06)
b2 = 3.284e+07 (2.019e+07, 4.55e+07)
a3 = 1.73e+07 (1.139e+07, 2.321e+07)
b3 = -8.643e+06 (-1.298e+07, -4.311e+06)
a4 = -7.895e+06 (-1.112e+07, -4.667e+06)
b4 = -3.616e+06 (-4.282e+06, -2.951e+06)
a5 = 7.232e+05 (1.553e+05, 1.291e+06)
b5 = 2.853e+06 (1.888e+06, 3.819e+06)
a6 = 4.273e+05 (3.462e+05, 5.083e+05)
b6 = -5.679e+05 (-8.266e+05, -3.092e+05)
a7 = -1.092e+05 (-1.471e+05, -7.122e+04)
b7 = 3528 (-1.4e+04, 2.105e+04)
a8 = 5249 (2399, 8099)
b8 = 6116 (4905, 7326)
w = 3.406 (3.325, 3.488)
Then look at what you did...
a0=2.589e+07;
a1=-2.918e+07;
b1=-3.622e+07;
a2=-7.159e+06;
b2=3.284e+07;
...
And, exactly as I said, you used 4 digit approximations to a list of numbers. That is a BAD idea. It is assured to result in crapola. What did you get? Yep. Crapola.
The point is, you did not in fact use the results. You used a poor approximation to the results, and you got a poor result. SURPRISE!