Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have data that can apparently be fitted by a sinusoidal function in form $D+A\sin(B(x-\frac CB))$. I tried doing the best fit in Mathematica using NonlinearModelFit function. I got a message saying it failed to converge with current accuracy and max iterations, so I tinkered a little bit with the function, but it made a model far from being the best fit for the data. I tried replacing parameters with constants and it turned out the problem was in finding $B$, or the horizontal shift, since I got a satisfactory fit when I specified the shift.

So my question is: Is there a work-around for this problem, so I can find the best horizontal shift?

Note: I tried it with the form $D+A\sin(Bx-C)$ with the same problem.

Edit: This is the requested data, note that in the second NonlinearModelFit, I specified b and it worked like a charm:

data = {{10, 716}, {20, 727}, {30, 744}, {40, 762}, {50, 783},{60, 804},
        {70, 826}, {80, 848}, {90, 872}, {100, 894}, {110, 915}, {120, 935},
        {130, 954}, {140, 969}, {150, 982}, {160, 990}, {170, 993}, 
        {180, 992}, {190, 986}, {200, 975}, {210, 961}, {220, 944},
        {230, 926}, {240, 905}, {250, 883}, {260, 861}, {270, 839},
        {280, 817}, {290, 795}, {300, 774}, {310, 755}, {320, 738},
        {330, 723}, {340, 712}, {350, 706}, {360, 706}}

NonlinearModelFit[data, d + a Sin[b (x - c)], {a, b, c, d}, x]["BestFit"]

During evaluation of NonlinearModelFit::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.

853.439 - 1717.86 Sin[0.94244 (10.0078 + x)]
NonlinearModelFit[data, d + a Sin[b (x - c)] /. b -> π/185, {a, c, d}, x]["BestFit"]
849.276 + 141.098 Sin[π/185 (-1930.24 + x)]
share|improve this question
1  
Yes, please add all relevant code and data. –  Yves Klett Jul 8 at 19:20
    
I've done it now. –  Imad Nabil Alnatsheh Jul 8 at 19:49
4  
It helps to provide reasonable initial guesses for the parameters so that the solver is more likely to converge to the right answer. In your case, it makes sense to start b at $\pi/180$ and d at the mean of the data: NonlinearModelFit[data, d + a Sin[b (x - c)], {a, {b, π/180}, c, {d, Mean@data[[All, 2]]}}, x]["BestFit"]; this gives the desired fit. –  Rahul Jul 8 at 19:57
1  
@RahulNarain Thank you for your comment, but it is actually a good answer, so if you would please post it as an answer so I would choose it. –  Imad Nabil Alnatsheh Jul 8 at 21:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.