# curve fitting tool custom equation

10 views (last 30 days)
Michal Naim Ben Eliyahu on 18 Apr 2022
Answered: Alex Sha on 19 Apr 2022
Hi,
I used cf tool to get a function to describe my data but it seems to give me a straight line.
I know the custom equation is the right one is there anything else I can do to make the function more like my data?
Thank you all for your time! Michal Naim Ben Eliyahu on 19 Apr 2022
Hi,
thank you so much!
The eqution is the right one according to my teachers. the data describes damped harmonic oscillator so I have to make the fit with it.
I'll try to change the equation's parameters. I was hoping there was a diffrent way than guessing random numbers and hoping for a good fit.
If you do have a diffrent idea or if I misunderstood you I'll upload my data.
in any case thank you so much!!

John D'Errico on 18 Apr 2022
Edited: John D'Errico on 18 Apr 2022
A general problem with the curve fitting toolbox is that it does not understand the mathematics of a model you pose to it. Not that this is truly a fixable problem. It is not, at least not until computers get far more intelligent than they are now. And I sort of fear that day anyway. :) But many users do not understand what has happened in a fit, and why they get a poor fit.
The curve fitting toolbox is just a search tool, that looks for good sets of parameters. But if it starts the search in a bad place, it gets lost. And I don't have your data, so I cannot easily show how to fit it.
@Torsten is totally correct, of course, that the data out past roughly t=7 is actively useless in the fit. Worse, it can cause some subtle problems. So toss that part.
But as I said, the biggest problem here is poor starting values. But there is also probably a lack of fit problem. That model is not perfect, would be my guess, because the initial transient seems to be larger than I would expect for an exponential decay. Again, lacking your data, it is hard to tell.
What are good starting values? e is the long term average behavior. from your plot, e should be around -11.
b is the exponential decay constant. It tells how fast the exponential drops off. You can guess that value from seeing the decay of each peak in the curve. As a wild guess from your plot, b might be around 0.3.
However, the most important parameter in this curve is the value of c. If you get that number wrong by as much as a factor of 2 too large or too small, your model will likely fit very poorly. A cosine curve goes through one period in a span of 2*pi. But from your plot, I can see roughly 5.5 oscillations in the interval from 4 to 6. That would suggest c should be approximately
c0 = 2*pi/(6 - 4)*5.5
c0 = 17.2788
fplot(@(t) cos(c0*t),[4 6]) So a good starting value for c would be roughly 17. As you can see from my plot, 5.5 periods in the span from 4 to 6, with that value for c.
a+e is the y-intercept of the curve at t==0. But we don't see any data near t==0. Luckily, the fit will tend to be insensitive to errors in your initial estimate of a and d. I don't even know the sign if a. But the magnitude of a should be roughly 4.
Again, without any real data, all of the above are just educated guesses.
The problem is, the curve fitting toolbox does not understand that it needs better startign values than just random numbers as start points. And if you get that initial estimate for c off by a factor of as much as 2, things will fit poorly.
When all is said and done however, my bet is the curve will fit poorly near the initial transient, since those peaks do not seem to behave as a truly exponential decay would. Again, that is just a guess. If you want better help in fitting that data, I would need to have the data itself.
##### 2 CommentsShowHide 1 older comment
Sam Chak on 19 Apr 2022
From your Book1 data, the output signal is quite large and appears to be some kind of a delayed signal. Thus, I rescaled and restructured for better fitting.
minh = min(h);
t1 = t/1e7;
h1 = h/abs(minh);
v = find(abs(t1) < 0.7);
[~, idx] = min(h1)
t2 = t1(idx:length(v)+1,:);
h2 = h1(idx:length(v)+1,:);
If you insist to use your teacher's suggested model: then  Since the displacement d is known, the amplitude a can be determined.
With the shifted center is known, you need to determine only two parameters: , .
If you consider the phase shift in the cosine wave, then above assumptions are invalid.

Torsten on 18 Apr 2022
Don't use the data for t > 6.3 in the fitting process.
Michal Naim Ben Eliyahu on 19 Apr 2022
I tried guessing the parameters but I was hoping for a shortcut.
I'll try again until I find the right match.
Than you so much for your help!

Alex Sha on 19 Apr 2022
if taking only part of data, for example, from No. 105 to No. 300, then the result will looks good
Sum Squared Error (SSE): 1111312565.89975
Root of Mean Square Error (RMSE): 2381.16821558602
Correlation Coef. (R): 0.969742933902271
R-Square: 0.940401357853383
Determination Coef. (DC): 0.940401357853381
Chi-Square: -43481.4424771284
F-Statistic: 753.442740102336
Parameter Best Estimate
---------- -------------
a -291924.957940865
b 7.9210511350074E-7
c -1.68465029667797E-5
d -2953.68909153569
e -9546.50019773865 