Curve Fitting for experimental data
Mostrar comentarios más antiguos
I have some experimental data:
x = [6.46:0.02:9.74]
y = [14200 14195 14155 14147 14145 14139 14107 14102 14101 14096 14067 14063 14057 14057 ...
14019 14016 14013 14006 14001 13994 13984 13970 13964 13948 13943 13935 13921 13903 ...
13902 13879 13867 13854 13836 13826 13813 13785 13775 13750 13733 13711 13694 13671 ...
13649 13636 13612 13604 13581 13563 13542 13529 13505 13495 13473 13464 13450 13433 ...
13426 13421 13418 13401 13387 13382 13380 13378 13377 13371 13373 13357 13342 13337 ...
13334 13334 13333 13331 13322 13295 13291 13289 13252 13246 13243 13202 13168 13155 ...
13145 13110 13104 13066 13062 13023 13018 12979 12971 12933 12925 12896 12884 12867 ...
12842 12836 12828 12796 12788 12784 12752 12754 12747 12745 12743 12742 12723 12710 ...
12710 12708 12705 12707 12706 12706 12705 12704 12703 12699 12698 12675 12662 12660 ...
12658 12649 12619 12616 12580 12570 12563 12527 12521 12482 12476 12438 12429 12394 ...
12386 12346 12331 12302 12297 12265 12248 12243 12213 12205 12199 12168 12164 12160 ...
12127 12121 12142 12076 12122 12126 12123 12130]
and I want to fit them with a function
y=a1*x+a2+sin(a3*x+a4)
I mention that I do not know anything about coefficients (a1,a2,a3,a4), I want to find out those coefficients value.
8 comentarios
What toolboxes are available so can know what tools are in your repertoire? Always begin with plotting any modeling to see if is even reasonable...

Doesn't look terrible (NB: your x/y vectors aren't the same length; I truncated x to match y) but there's an amplitude factor with the residual sine running on top of the linear trend. The trend does appear to be pretty linear however.
John D'Errico
el 12 de Abr. de 2018
Editada: John D'Errico
el 12 de Abr. de 2018
Ok, I pasted in your data. There are 165 values in the vector x. 162 in y. I could be arbitrary, and drop the last 3 elements in x.
The next problem is that sinusoidal oscillation seems to vary in amplitude. So a simple sine wave will be inadequate. Your model will fit poorly.
John D'Errico
el 13 de Abr. de 2018
Please don't answer your own question with an answer that merely adds information. Moved to a comment from nony:
"I have a multitude of such a pair of (x, y) experimental data that oscillates. I'm interested in finding out the amplitude of the oscillations, and I thought I could fit the datas with a more general function and make a small code to apply to all the experimental data. I mention that I am a beginner in matlab, and I can't handle it :(
I certainly do not have a linear trend, but rather a combination of something linear and a sinusoid. Another example of my data looks like this graph."

John D'Errico
el 13 de Abr. de 2018
Then it is time to learn about curve fitting. Learn to use the curve fitting toolbox. Or the optimization toolbox. Or the stats toolbox.
The curve fitting toolbox is the easiest to use, since it makes it so easy to define your model. There will be many examples in there.
"I certainly do not have a linear trend,..."
The dataset you first posted is quite linear for the trend; the plot I attached is the data and linear trend (blue/red lines LH axis) and the residual-> Y-Trend(Y) on the RH axis. The exploratory plot was to see whether would need a higher-order baseline term; both the visual fit and the residual slope being essential zero indicates that linear is pretty good for that part. As John and I both noted, there's an amplitude factor on the sinusoidal component that is varying with X that isn't going to be accounted for in the proposed model with a single constant amplitude coefficient.
You may not be able to fit the composite model as well shown because there's not enough cycles to help much and the x range unless it's given some help in finding the frequency term may have some trouble.
ADDENDUM Playing some more, the cftool on its own has much difficulty in estimating the custom model written in the "Sum of Sine" form as you cast the model; at least here with R2016b it completely fails in the easy-to-use interface even if try to fit the residual rather than the raw data producing a negative R-square. The builtin "Sum of Sine" form does a reasonable job; one term is roughly 83% variance explained whereas a two-term model is about 93%. The difficulty with the tool is that you can't combine the two in the interface; have to choose one or t'other from the menu.
John D'Errico
el 13 de Abr. de 2018
Editada: John D'Errico
el 13 de Abr. de 2018
What I don't like about the CFT is it uses default random starting values. They are NEVER good starting points it seems to me. Better to force the user to supply them, thus possibly forcing them to concentrate on providing a useful start point. I'll try suggesting this to the CFT people when I get a chance.
Indeed; at least being able to provide a start point would help albeit with a simplified user interface and inexperienced users whether they would stumble upon the importance without serious prompting is problematical I'd guess...but, definitely need some way to avoid issues like with the OPs data.
I wasn't at all surprised when trying with the original data that as a sine of roughly 100 pk-pk amplitude riding on a baseline of 10E4; just the linear term alone is an R-square > 0.93 so there's very little residual left to account for from the total albeit it's really what's of interest.
I did have (I thought reasonable) expectations when trying with the residual as the y variable but nothing I could do by trying to fudge terms inside the model helped a bit with the custom model; however it gets and whatever it uses for initial values were just too far out in left field.
dpb
el 13 de Abr. de 2018
Well, I hadn't known what it did for starting guesses; with your comment, John, I did
y = 60*(1+a)*sin(6*(1+b)*x+(c-5))
as the custom model after fitting the sine model with the specialized model and using approximate values for the coefficients thus found. This way was able to find
a=0.04007
b=0.05077
c=0.1772
R-sq = 0.838 RMSE 19.68 for the residual portion. That compares almost identically then with the 'Sum of Sines' model solution. "Going round Robin Hood's barn" comes to mind.............
Hmmm...that makes for interesting question...nope, using
z=zscore(r);
as the response variable isn't enough of a klew it still failed with the straight model as 'Custom Equation'. Disappointing, that... :(
Respuestas (0)
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!