problem in curve fitting using summation of sine functions
Show older comments
i wanted to fit the summation of sine functions in the following data: https://drive.google.com/file/d/1OzD-Dcra6j4Xn6nCWy4zFUUTObcIHbwq/view?usp=drive_link and https://drive.google.com/file/d/1gC00X5qEbqPlIBzsFNk9kQSmhVM44lEr/view?usp=drive_link, i am using curve fitter tool box for this, but fitted curve is not coming what i want. i am attaching the pic for reference. please any one help.

5 Comments
John D'Errico
on 9 Mar 2024
Edited: John D'Errico
on 9 Mar 2024
First, you need to actually attach your data, not as a broken Google drive link that we cannot access. Since that is trivial to do, then just do it. You were able to attach the picture. So attach your data too!
Second, you need to show what you did get. Show what you tried. Did you use only a sum of sines, or sines and cosines? How many terms, and what you don't like about the result. (We can guess of course, but that would be just a wild guess.) If we don't know what you saw as inadequate, then we cannot easily help you to improve on what you did try.
nihal
on 9 Mar 2024
nihal
on 9 Mar 2024
John D'Errico
on 10 Mar 2024
So what was wrong with the multiplle answers you did get here? Was there a compelling reason to keep on asking the same question?
nihal
on 10 Mar 2024
Accepted Answer
More Answers (2)
John D'Errico
on 9 Mar 2024
Edited: John D'Errico
on 9 Mar 2024
0 votes
Ok. Thank you for the data. The plot does help a lot actually. At first glance, the plot of the fit itself looks quite reasonable. But then I focused my old bleary eyes on it, and I see the sinusoidal oscillations in the curve near the flat regions. (When I said I might guess what the problem was, this is what I was thinking.)
That is a common consequence of trying to fit a flat curve (or a curve with flat or nearly flat sections) with a sum of sine waves. Higher frequency sine functions will just introduce more (higher frequency) wiggles in the flats. Take enough terms and eventually the bumps go away, at least to the point you do not see them. But this just argues you did not take enough terms in the model.
It is exactly like the presence of Gibbs phenomena in a fit. In fact, if you look at the figures in the wiki page I link here, you will see that same behavior.
Again, taking more terms in that series will improve the fit, beause the oscillations will eventually cancel out. You don't have any sharp transition in your data, so there are no large overshoots near the edges. But even so, there will still be the same characteristic oscillations in the region of the curve that want to be nearly flat.
So, how can you improve on that result? Again, the simple solution is to just throw more terms at the problem. If you insist on having a model that is a sum of sines (and cosines usually) that will work, to drive down the oscillations. They will still be there of course, only at a finer level. Nothing you can do will prevent that in such a model.
Alternatively, you could use a spline model of sorts. Perhaps a smoothing spline would be a good choice, or a least squares spline. But splines have their own issues of course. People always want to write down the function, and you simply cannot write down some nice looking function from a spline. You can use them to evaluate, or make a nice smooth plot.
John D'Errico
on 9 Mar 2024
Edited: John D'Errico
on 9 Mar 2024
I'll post this as a separate answer, so as to not get it lost in the comments, and since I will show you how to solve it as a trig series. I'll use backslash to perform the solve. I could have used many other tools, but this is actually a purely linear problem! (In the parameters, that is.)
phidot = readtable('phi_dot.csv');
x = phidot.Var1;
y = phidot.Var2;
plot(x,y)
yline(0)
per = 6.9; % approximate length of the period, determined by observation, where the curve crosses the x axis.
nterms = 8;
n = 1:nterms;
% model as a sum of sine and cosine terms
% the first coefficient is just an additive constant,
% the next nterms coefficients apply to the sine terms,
% then the last nterms coefficients apply to cosine terms
A = [ones(size(x)),sin(2*pi*x*n/per),cos(2*pi*x*n/per)];
coeffs = A\y;
ypred = A*coeffs;
plot(x,y,'b-',x,ypred,'-r')
With 8 terms, this will be approximately what the curve fitting toolbox would have done (using the 'sin8' option for fittype. Next, I'll redo the fit, using 20 terms in the model.
nterms = 20;
n = 1:nterms;
% model as a sum of sine and cosine terms
A = [ones(size(x)),sin(2*pi*x*n/per),cos(2*pi*x*n/per)];
coeffs = A\y;
ypred = A*coeffs;
plot(x,ypred,'-r')
I plotted only the curve this time, since you would miss what it does in the flat spots otherwise. As you can see, the oscillations decrease. No matter what you do, with more terms the wiggles will get smaller, but this is a FUNDAMENTAL characteristic of a trig series when used to fit such a problem.
nterms = 50;
n = 1:nterms;
% model as a sum of sine and cosine terms
A = [ones(size(x)),sin(2*pi*x*n/per),cos(2*pi*x*n/per)];
coeffs = A\y;
ypred = A*coeffs;
plot(x,ypred,'-r')
The last plot was with 50 terms in the series. You should see there the wiggles are almost impossible to see. Again, this is just a fundamental characterisitic of such a model.
Finally, note that you can ALWAYS transform a pair of terms of the form
a1*sin(b*x) + a2*cos(b*x)
combining them both into a single phase shifted sine term only. That is just a basic trig identity. I won't show how to do that unless there is some need for it.
So while the model I fit used both sine and cosine terms, you could do the transformation I mention easily enough to reduce the number of terms.
Categories
Find more on Get Started with Curve Fitting Toolbox 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!











