Fitting a Cos^2 function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












7












$begingroup$


I have to fit a Cos^2 function to data I measured. The function is $a cos^2(fracbxpi180+c)+d $ and I tried the FindFit and Linear Model Function. I have 4 datasets which I have to fit, the first one worked. The other three only yielded not usable fits. I am pretty new to Mathematica so I hope its just a newbie mistake which is easy to fix.



Here's a minimal example:



data45 = Import["data45.txt", "table"]

0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
92.5, 320, 32, 340, 43

FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x]
a -> 45.2733, b -> 0.886263, c -> 39.01, d -> 157.974


This yields the following fit of the data:



Fit



Which is not usable.



I would really appreciate some help!



Greetings










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
    $endgroup$
    – Roman
    Feb 10 at 18:52
















7












$begingroup$


I have to fit a Cos^2 function to data I measured. The function is $a cos^2(fracbxpi180+c)+d $ and I tried the FindFit and Linear Model Function. I have 4 datasets which I have to fit, the first one worked. The other three only yielded not usable fits. I am pretty new to Mathematica so I hope its just a newbie mistake which is easy to fix.



Here's a minimal example:



data45 = Import["data45.txt", "table"]

0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
92.5, 320, 32, 340, 43

FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x]
a -> 45.2733, b -> 0.886263, c -> 39.01, d -> 157.974


This yields the following fit of the data:



Fit



Which is not usable.



I would really appreciate some help!



Greetings










share|improve this question











$endgroup$







  • 1




    $begingroup$
    Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
    $endgroup$
    – Roman
    Feb 10 at 18:52














7












7








7


3



$begingroup$


I have to fit a Cos^2 function to data I measured. The function is $a cos^2(fracbxpi180+c)+d $ and I tried the FindFit and Linear Model Function. I have 4 datasets which I have to fit, the first one worked. The other three only yielded not usable fits. I am pretty new to Mathematica so I hope its just a newbie mistake which is easy to fix.



Here's a minimal example:



data45 = Import["data45.txt", "table"]

0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
92.5, 320, 32, 340, 43

FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x]
a -> 45.2733, b -> 0.886263, c -> 39.01, d -> 157.974


This yields the following fit of the data:



Fit



Which is not usable.



I would really appreciate some help!



Greetings










share|improve this question











$endgroup$




I have to fit a Cos^2 function to data I measured. The function is $a cos^2(fracbxpi180+c)+d $ and I tried the FindFit and Linear Model Function. I have 4 datasets which I have to fit, the first one worked. The other three only yielded not usable fits. I am pretty new to Mathematica so I hope its just a newbie mistake which is easy to fix.



Here's a minimal example:



data45 = Import["data45.txt", "table"]

0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
92.5, 320, 32, 340, 43

FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x]
a -> 45.2733, b -> 0.886263, c -> 39.01, d -> 157.974


This yields the following fit of the data:



Fit



Which is not usable.



I would really appreciate some help!



Greetings







fitting trigonometry






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 24 at 2:42









J. M. is computer-less

97.2k10303463




97.2k10303463










asked Feb 10 at 18:06









ZannzorZannzor

386




386







  • 1




    $begingroup$
    Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
    $endgroup$
    – Roman
    Feb 10 at 18:52













  • 1




    $begingroup$
    Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
    $endgroup$
    – Roman
    Feb 10 at 18:52








1




1




$begingroup$
Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
$endgroup$
– Roman
Feb 10 at 18:52





$begingroup$
Since a Cos[(b x 𝜋)/180 + c]^2 + d == a/2 Cos[(b x 𝜋)/90 + 2 c] + (d + a/2) you may be better off (in terms of numerics) to fit a cosine instead of a squared cosine, and then re-interpret the fitting parameters. A further advantage of this would be that you can get an initial guess of the fitting parameters from looking at the peak of the Fourier transform.
$endgroup$
– Roman
Feb 10 at 18:52











5 Answers
5






active

oldest

votes


















14












$begingroup$

First, you better use NonlinearModelFit, Fit and FindFit haven't updated in a while, so newly introduced LinearModelFit and NonlinearModelFit will have greater accuracy and better performance.



Second, for arbitrary curve the initial values for parameters can lead to a local minimum for fitting, thus producing wrong curve fit. The best approach would be to specify some initial values based on some empirics: maximum and minimum values, median etc.



res = NonlinearModelFit[data45, a Cos[b x + c]^2 + d, a, 150, b, 1/70, c, 12, d, 170, x];
Show[ListPlot[data45, PlotStyle -> Red], Plot[res[x], x, 0, 400]]







share|improve this answer











$endgroup$




















    9












    $begingroup$

    The two answers so far (@MassDefect and @m0nhawk) are exactly what you want to do: have good initial guesses (especially for anything dealing with sines and cosines).



    If you are fitting many sets of data to the same model, then automating the initial values is recommended. For your example, you could use:



    sol = FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d,
    a, (Max[data45[[All, 2]]] - Min[data45[[All, 2]]])/2, b, c, d, Mean[data45[[All, 2]]], x]





    share|improve this answer









    $endgroup$




















      8












      $begingroup$

      Fitting arbitrary curves is actually a pretty difficult task as it's hard for a computer to make a good guess about what the initial parameters should be. We can help it out by providing some initial values that seem to make sense.



      params = FindFit[
      data45,
      a Cos[b x + c]^2 + d,
      a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
      x]


      Cos square plot with fitting



      If the fitting algorithm is still having difficulty, you can also add in constraints to force it to only look for the best values in a given range like this:



      params = FindFit[
      data45,
      a Cos[b x + c]^2 + d, 250 < a < 350, 2[Pi]/350 < b < 2[Pi]/250,
      0 < c < [Pi], 25 < d < 75,
      a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
      x]





      share|improve this answer









      $endgroup$




















        5












        $begingroup$

        As always you can minimize cost function using NMinimize.



        ClearAll["Global`*"]

        data = 0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
        173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
        230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
        92.5, 320, 32, 340, 43;

        model[x_] := a Cos[b x + c]^2 + d

        cost = Total[(#2 - model[#1])^2 & @@@ data];
        fit = NMinimize[cost, 0 < a < 500, 0 < b < 0.1, 0 < c < 20,
        0 < d < 50, a, b, c, d, Method -> "DifferentialEvolution",
        MaxIterations -> 1000]



        5477.29, a -> 300.499, b -> 0.0172855, c -> 8.49213, d -> 28.431




        Thread[a, b, c, d = a, b, c, d /. Last@fit];
        Show[Plot[model[x], x, 0, 350], ListPlot[data, PlotStyle -> Red]]


        enter image description here






        share|improve this answer









        $endgroup$












        • $begingroup$
          For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
          $endgroup$
          – JimB
          Feb 10 at 20:25







        • 3




          $begingroup$
          Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
          $endgroup$
          – Okkes Dulgerci
          Feb 10 at 20:40











        • $begingroup$
          Even better! Thanks!
          $endgroup$
          – JimB
          Feb 10 at 20:55


















        0












        $begingroup$

        You can solve the problem with FindFit-Option Method->"NMinimize



        FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x, 


        Method -> "NMinimize"]
        (a -> 300.499, b -> 0.990388, c -> 65.0408, d -> 28.431)






        share|improve this answer









        $endgroup$












        • $begingroup$
          What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
          $endgroup$
          – Szabolcs
          Feb 11 at 9:32










        • $begingroup$
          @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
          $endgroup$
          – Ulrich Neumann
          Feb 11 at 11:24










        Your Answer





        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191262%2ffitting-a-cos2-function%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        14












        $begingroup$

        First, you better use NonlinearModelFit, Fit and FindFit haven't updated in a while, so newly introduced LinearModelFit and NonlinearModelFit will have greater accuracy and better performance.



        Second, for arbitrary curve the initial values for parameters can lead to a local minimum for fitting, thus producing wrong curve fit. The best approach would be to specify some initial values based on some empirics: maximum and minimum values, median etc.



        res = NonlinearModelFit[data45, a Cos[b x + c]^2 + d, a, 150, b, 1/70, c, 12, d, 170, x];
        Show[ListPlot[data45, PlotStyle -> Red], Plot[res[x], x, 0, 400]]







        share|improve this answer











        $endgroup$

















          14












          $begingroup$

          First, you better use NonlinearModelFit, Fit and FindFit haven't updated in a while, so newly introduced LinearModelFit and NonlinearModelFit will have greater accuracy and better performance.



          Second, for arbitrary curve the initial values for parameters can lead to a local minimum for fitting, thus producing wrong curve fit. The best approach would be to specify some initial values based on some empirics: maximum and minimum values, median etc.



          res = NonlinearModelFit[data45, a Cos[b x + c]^2 + d, a, 150, b, 1/70, c, 12, d, 170, x];
          Show[ListPlot[data45, PlotStyle -> Red], Plot[res[x], x, 0, 400]]







          share|improve this answer











          $endgroup$















            14












            14








            14





            $begingroup$

            First, you better use NonlinearModelFit, Fit and FindFit haven't updated in a while, so newly introduced LinearModelFit and NonlinearModelFit will have greater accuracy and better performance.



            Second, for arbitrary curve the initial values for parameters can lead to a local minimum for fitting, thus producing wrong curve fit. The best approach would be to specify some initial values based on some empirics: maximum and minimum values, median etc.



            res = NonlinearModelFit[data45, a Cos[b x + c]^2 + d, a, 150, b, 1/70, c, 12, d, 170, x];
            Show[ListPlot[data45, PlotStyle -> Red], Plot[res[x], x, 0, 400]]







            share|improve this answer











            $endgroup$



            First, you better use NonlinearModelFit, Fit and FindFit haven't updated in a while, so newly introduced LinearModelFit and NonlinearModelFit will have greater accuracy and better performance.



            Second, for arbitrary curve the initial values for parameters can lead to a local minimum for fitting, thus producing wrong curve fit. The best approach would be to specify some initial values based on some empirics: maximum and minimum values, median etc.



            res = NonlinearModelFit[data45, a Cos[b x + c]^2 + d, a, 150, b, 1/70, c, 12, d, 170, x];
            Show[ListPlot[data45, PlotStyle -> Red], Plot[res[x], x, 0, 400]]








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 10 at 20:10

























            answered Feb 10 at 18:29









            m0nhawkm0nhawk

            2,82711532




            2,82711532





















                9












                $begingroup$

                The two answers so far (@MassDefect and @m0nhawk) are exactly what you want to do: have good initial guesses (especially for anything dealing with sines and cosines).



                If you are fitting many sets of data to the same model, then automating the initial values is recommended. For your example, you could use:



                sol = FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d,
                a, (Max[data45[[All, 2]]] - Min[data45[[All, 2]]])/2, b, c, d, Mean[data45[[All, 2]]], x]





                share|improve this answer









                $endgroup$

















                  9












                  $begingroup$

                  The two answers so far (@MassDefect and @m0nhawk) are exactly what you want to do: have good initial guesses (especially for anything dealing with sines and cosines).



                  If you are fitting many sets of data to the same model, then automating the initial values is recommended. For your example, you could use:



                  sol = FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d,
                  a, (Max[data45[[All, 2]]] - Min[data45[[All, 2]]])/2, b, c, d, Mean[data45[[All, 2]]], x]





                  share|improve this answer









                  $endgroup$















                    9












                    9








                    9





                    $begingroup$

                    The two answers so far (@MassDefect and @m0nhawk) are exactly what you want to do: have good initial guesses (especially for anything dealing with sines and cosines).



                    If you are fitting many sets of data to the same model, then automating the initial values is recommended. For your example, you could use:



                    sol = FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d,
                    a, (Max[data45[[All, 2]]] - Min[data45[[All, 2]]])/2, b, c, d, Mean[data45[[All, 2]]], x]





                    share|improve this answer









                    $endgroup$



                    The two answers so far (@MassDefect and @m0nhawk) are exactly what you want to do: have good initial guesses (especially for anything dealing with sines and cosines).



                    If you are fitting many sets of data to the same model, then automating the initial values is recommended. For your example, you could use:



                    sol = FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d,
                    a, (Max[data45[[All, 2]]] - Min[data45[[All, 2]]])/2, b, c, d, Mean[data45[[All, 2]]], x]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 10 at 18:57









                    JimBJimB

                    17.7k12763




                    17.7k12763





















                        8












                        $begingroup$

                        Fitting arbitrary curves is actually a pretty difficult task as it's hard for a computer to make a good guess about what the initial parameters should be. We can help it out by providing some initial values that seem to make sense.



                        params = FindFit[
                        data45,
                        a Cos[b x + c]^2 + d,
                        a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                        x]


                        Cos square plot with fitting



                        If the fitting algorithm is still having difficulty, you can also add in constraints to force it to only look for the best values in a given range like this:



                        params = FindFit[
                        data45,
                        a Cos[b x + c]^2 + d, 250 < a < 350, 2[Pi]/350 < b < 2[Pi]/250,
                        0 < c < [Pi], 25 < d < 75,
                        a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                        x]





                        share|improve this answer









                        $endgroup$

















                          8












                          $begingroup$

                          Fitting arbitrary curves is actually a pretty difficult task as it's hard for a computer to make a good guess about what the initial parameters should be. We can help it out by providing some initial values that seem to make sense.



                          params = FindFit[
                          data45,
                          a Cos[b x + c]^2 + d,
                          a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                          x]


                          Cos square plot with fitting



                          If the fitting algorithm is still having difficulty, you can also add in constraints to force it to only look for the best values in a given range like this:



                          params = FindFit[
                          data45,
                          a Cos[b x + c]^2 + d, 250 < a < 350, 2[Pi]/350 < b < 2[Pi]/250,
                          0 < c < [Pi], 25 < d < 75,
                          a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                          x]





                          share|improve this answer









                          $endgroup$















                            8












                            8








                            8





                            $begingroup$

                            Fitting arbitrary curves is actually a pretty difficult task as it's hard for a computer to make a good guess about what the initial parameters should be. We can help it out by providing some initial values that seem to make sense.



                            params = FindFit[
                            data45,
                            a Cos[b x + c]^2 + d,
                            a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                            x]


                            Cos square plot with fitting



                            If the fitting algorithm is still having difficulty, you can also add in constraints to force it to only look for the best values in a given range like this:



                            params = FindFit[
                            data45,
                            a Cos[b x + c]^2 + d, 250 < a < 350, 2[Pi]/350 < b < 2[Pi]/250,
                            0 < c < [Pi], 25 < d < 75,
                            a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                            x]





                            share|improve this answer









                            $endgroup$



                            Fitting arbitrary curves is actually a pretty difficult task as it's hard for a computer to make a good guess about what the initial parameters should be. We can help it out by providing some initial values that seem to make sense.



                            params = FindFit[
                            data45,
                            a Cos[b x + c]^2 + d,
                            a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                            x]


                            Cos square plot with fitting



                            If the fitting algorithm is still having difficulty, you can also add in constraints to force it to only look for the best values in a given range like this:



                            params = FindFit[
                            data45,
                            a Cos[b x + c]^2 + d, 250 < a < 350, 2[Pi]/350 < b < 2[Pi]/250,
                            0 < c < [Pi], 25 < d < 75,
                            a, 300, b, 2 [Pi]/300, c, [Pi]/2, d, 50,
                            x]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 10 at 18:28









                            MassDefectMassDefect

                            1,763311




                            1,763311





















                                5












                                $begingroup$

                                As always you can minimize cost function using NMinimize.



                                ClearAll["Global`*"]

                                data = 0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
                                173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
                                230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
                                92.5, 320, 32, 340, 43;

                                model[x_] := a Cos[b x + c]^2 + d

                                cost = Total[(#2 - model[#1])^2 & @@@ data];
                                fit = NMinimize[cost, 0 < a < 500, 0 < b < 0.1, 0 < c < 20,
                                0 < d < 50, a, b, c, d, Method -> "DifferentialEvolution",
                                MaxIterations -> 1000]



                                5477.29, a -> 300.499, b -> 0.0172855, c -> 8.49213, d -> 28.431




                                Thread[a, b, c, d = a, b, c, d /. Last@fit];
                                Show[Plot[model[x], x, 0, 350], ListPlot[data, PlotStyle -> Red]]


                                enter image description here






                                share|improve this answer









                                $endgroup$












                                • $begingroup$
                                  For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:25







                                • 3




                                  $begingroup$
                                  Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                  $endgroup$
                                  – Okkes Dulgerci
                                  Feb 10 at 20:40











                                • $begingroup$
                                  Even better! Thanks!
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:55















                                5












                                $begingroup$

                                As always you can minimize cost function using NMinimize.



                                ClearAll["Global`*"]

                                data = 0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
                                173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
                                230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
                                92.5, 320, 32, 340, 43;

                                model[x_] := a Cos[b x + c]^2 + d

                                cost = Total[(#2 - model[#1])^2 & @@@ data];
                                fit = NMinimize[cost, 0 < a < 500, 0 < b < 0.1, 0 < c < 20,
                                0 < d < 50, a, b, c, d, Method -> "DifferentialEvolution",
                                MaxIterations -> 1000]



                                5477.29, a -> 300.499, b -> 0.0172855, c -> 8.49213, d -> 28.431




                                Thread[a, b, c, d = a, b, c, d /. Last@fit];
                                Show[Plot[model[x], x, 0, 350], ListPlot[data, PlotStyle -> Red]]


                                enter image description here






                                share|improve this answer









                                $endgroup$












                                • $begingroup$
                                  For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:25







                                • 3




                                  $begingroup$
                                  Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                  $endgroup$
                                  – Okkes Dulgerci
                                  Feb 10 at 20:40











                                • $begingroup$
                                  Even better! Thanks!
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:55













                                5












                                5








                                5





                                $begingroup$

                                As always you can minimize cost function using NMinimize.



                                ClearAll["Global`*"]

                                data = 0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
                                173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
                                230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
                                92.5, 320, 32, 340, 43;

                                model[x_] := a Cos[b x + c]^2 + d

                                cost = Total[(#2 - model[#1])^2 & @@@ data];
                                fit = NMinimize[cost, 0 < a < 500, 0 < b < 0.1, 0 < c < 20,
                                0 < d < 50, a, b, c, d, Method -> "DifferentialEvolution",
                                MaxIterations -> 1000]



                                5477.29, a -> 300.499, b -> 0.0172855, c -> 8.49213, d -> 28.431




                                Thread[a, b, c, d = a, b, c, d /. Last@fit];
                                Show[Plot[model[x], x, 0, 350], ListPlot[data, PlotStyle -> Red]]


                                enter image description here






                                share|improve this answer









                                $endgroup$



                                As always you can minimize cost function using NMinimize.



                                ClearAll["Global`*"]

                                data = 0, 132, 20, 279.5, 40, 289, 60, 312, 80, 307, 100,
                                173, 120, 92, 140, 25, 160, 44.5, 180, 109.5, 200,
                                230.5, 220, 305, 240, 339, 260, 246.5, 280, 181.5, 300,
                                92.5, 320, 32, 340, 43;

                                model[x_] := a Cos[b x + c]^2 + d

                                cost = Total[(#2 - model[#1])^2 & @@@ data];
                                fit = NMinimize[cost, 0 < a < 500, 0 < b < 0.1, 0 < c < 20,
                                0 < d < 50, a, b, c, d, Method -> "DifferentialEvolution",
                                MaxIterations -> 1000]



                                5477.29, a -> 300.499, b -> 0.0172855, c -> 8.49213, d -> 28.431




                                Thread[a, b, c, d = a, b, c, d /. Last@fit];
                                Show[Plot[model[x], x, 0, 350], ListPlot[data, PlotStyle -> Red]]


                                enter image description here







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 10 at 20:13









                                Okkes DulgerciOkkes Dulgerci

                                5,1871917




                                5,1871917











                                • $begingroup$
                                  For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:25







                                • 3




                                  $begingroup$
                                  Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                  $endgroup$
                                  – Okkes Dulgerci
                                  Feb 10 at 20:40











                                • $begingroup$
                                  Even better! Thanks!
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:55
















                                • $begingroup$
                                  For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:25







                                • 3




                                  $begingroup$
                                  Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                  $endgroup$
                                  – Okkes Dulgerci
                                  Feb 10 at 20:40











                                • $begingroup$
                                  Even better! Thanks!
                                  $endgroup$
                                  – JimB
                                  Feb 10 at 20:55















                                $begingroup$
                                For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                $endgroup$
                                – JimB
                                Feb 10 at 20:25





                                $begingroup$
                                For whatever it's worth, the method you suggest (NMinimize with "DifferentialEvolution" does seem to be relatively robust to not-so-hot starting values. But NonlinearModelFit does automatically give essential summary statistics (such as standard errors for predictions and prediction intervals). I consider such summary statistics essential but in this forum folks often seem not to share that view. So your approach to get starting values for NonlinearModelFit might be the way to go for troublesome fits.
                                $endgroup$
                                – JimB
                                Feb 10 at 20:25





                                3




                                3




                                $begingroup$
                                Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                $endgroup$
                                – Okkes Dulgerci
                                Feb 10 at 20:40





                                $begingroup$
                                Actually one can use NonlinearModelFit with Method option NMinimize like so NonlinearModelFit[ data, a Cos[b x + c]^2 + d, 0 < a < 500, 0 < b < 0.1, 0 < c < 20, 0 < d < 50, a, b, c, d, x, Method -> "NMinimize", Method -> "DifferentialEvolution", MaxIterations -> 1000]; Get the same result and do more analysis.
                                $endgroup$
                                – Okkes Dulgerci
                                Feb 10 at 20:40













                                $begingroup$
                                Even better! Thanks!
                                $endgroup$
                                – JimB
                                Feb 10 at 20:55




                                $begingroup$
                                Even better! Thanks!
                                $endgroup$
                                – JimB
                                Feb 10 at 20:55











                                0












                                $begingroup$

                                You can solve the problem with FindFit-Option Method->"NMinimize



                                FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x, 


                                Method -> "NMinimize"]
                                (a -> 300.499, b -> 0.990388, c -> 65.0408, d -> 28.431)






                                share|improve this answer









                                $endgroup$












                                • $begingroup$
                                  What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                  $endgroup$
                                  – Szabolcs
                                  Feb 11 at 9:32










                                • $begingroup$
                                  @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                  $endgroup$
                                  – Ulrich Neumann
                                  Feb 11 at 11:24















                                0












                                $begingroup$

                                You can solve the problem with FindFit-Option Method->"NMinimize



                                FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x, 


                                Method -> "NMinimize"]
                                (a -> 300.499, b -> 0.990388, c -> 65.0408, d -> 28.431)






                                share|improve this answer









                                $endgroup$












                                • $begingroup$
                                  What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                  $endgroup$
                                  – Szabolcs
                                  Feb 11 at 9:32










                                • $begingroup$
                                  @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                  $endgroup$
                                  – Ulrich Neumann
                                  Feb 11 at 11:24













                                0












                                0








                                0





                                $begingroup$

                                You can solve the problem with FindFit-Option Method->"NMinimize



                                FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x, 


                                Method -> "NMinimize"]
                                (a -> 300.499, b -> 0.990388, c -> 65.0408, d -> 28.431)






                                share|improve this answer









                                $endgroup$



                                You can solve the problem with FindFit-Option Method->"NMinimize



                                FindFit[data45, a Cos[(b x Pi)/180 + c]^2 + d, a, b, c, d, x, 


                                Method -> "NMinimize"]
                                (a -> 300.499, b -> 0.990388, c -> 65.0408, d -> 28.431)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 11 at 7:25









                                Ulrich NeumannUlrich Neumann

                                9,406516




                                9,406516











                                • $begingroup$
                                  What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                  $endgroup$
                                  – Szabolcs
                                  Feb 11 at 9:32










                                • $begingroup$
                                  @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                  $endgroup$
                                  – Ulrich Neumann
                                  Feb 11 at 11:24
















                                • $begingroup$
                                  What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                  $endgroup$
                                  – Szabolcs
                                  Feb 11 at 9:32










                                • $begingroup$
                                  @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                  $endgroup$
                                  – Ulrich Neumann
                                  Feb 11 at 11:24















                                $begingroup$
                                What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                $endgroup$
                                – Szabolcs
                                Feb 11 at 9:32




                                $begingroup$
                                What made it work wasn't really NMinimize though, but rescaling the argument of cosine, so that automatic initial guesses for b are more likely to be reasonable.
                                $endgroup$
                                – Szabolcs
                                Feb 11 at 9:32












                                $begingroup$
                                @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                $endgroup$
                                – Ulrich Neumann
                                Feb 11 at 11:24




                                $begingroup$
                                @Szabolcs Thanks, in MMA 11.0.1 you get two different results!
                                $endgroup$
                                – Ulrich Neumann
                                Feb 11 at 11:24

















                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Mathematica Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                Use MathJax to format equations. MathJax reference.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f191262%2ffitting-a-cos2-function%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?