How to easily find the coefficients of a cubic polynomial and its plot for the given 4 points

Multi tool use
Multi tool use

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











up vote
4
down vote

favorite












Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d; and its plot.



How to solve this in Mathematica easily?










share|improve this question



























    up vote
    4
    down vote

    favorite












    Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d; and its plot.



    How to solve this in Mathematica easily?










    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d; and its plot.



      How to solve this in Mathematica easily?










      share|improve this question















      Given 4 points as points = 0, 4, 1, 0, 2, -1, 3, 0;. I want to find the coefficients of f[x_] := a x^3 + b x^2 + c x + d; and its plot.



      How to solve this in Mathematica easily?







      plotting equation-solving interpolation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 15 at 17:03









      Carl Woll

      57.3k273149




      57.3k273149










      asked Aug 15 at 7:43









      Friendly Ghost

      2076




      2076




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You can also use LinearSolve



          A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
          B = points[[All, 2]];



          $A=left(
          beginarraycccc
          0 & 0 & 0 & 1 \
          1 & 1 & 1 & 1 \
          8 & 4 & 2 & 1 \
          27 & 9 & 3 & 1 \
          endarray
          right) qquad B=left(
          beginarrayc
          4 \
          0 \
          -1 \
          0 \
          endarray
          right)$




          LinearSolve[A, B]



          -(1/6), 2, -(35/6), 4







          share|improve this answer



























            up vote
            5
            down vote













            Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve



            points = 0, 4, 1, 0, 2, -1, 3, 0
            (* 0, 4, 1, 0, 2, -1, 3, 0 *)

            f[x_] := a x^3 + b x^2 + c x + d


            (f[#1] == #2) & @@@ points
            (*

            d == 4,
            a + b + c + d == 0,
            8 a + 4 b + 2 c + d == -1,
            27 a + 9 b + 3 c + d == 0

            *)


            Solve[
            (f[#1] == #2) & @@@ points
            , a, b, c, d
            ]
            (* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)





            share|improve this answer



























              up vote
              5
              down vote













              fit = FindFit[points, f[x], a, b, c, d, x]



              a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.




              Plot[Evaluate[f[x] /. fit], x, 0, 5, 
              Epilog -> PointSize[Large], Red, Point@points]


              enter image description here



              To get exact results, you can use



              Rationalize[fit] 



              a -> -(1/6), b -> 2, c -> -(35/6), d -> 4




              Alternatively, you can use Reduce or Solve (as in rhermans's answer) with alternative specification of the first argument:



              ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
              Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]



              a -> -(1/6), b -> 2, c -> -(35/6), d -> 4







              share|improve this answer






















              • Could you make the coefficients in exact forms?
                – Friendly Ghost
                Aug 15 at 7:51










              • @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                – kglr
                Aug 15 at 7:54










              • @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                – rhermans
                Aug 15 at 10:12

















              up vote
              2
              down vote













              You can use InterpolatingPolynomial:



              Expand @ InterpolatingPolynomial[
              0,4,1,0,2,-1,3,0,
              x
              ]



              4 - (35 x)/6 + 2 x^2 - x^3/6







              share|improve this answer




















                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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f180038%2fhow-to-easily-find-the-coefficients-of-a-cubic-polynomial-and-its-plot-for-the-g%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                6
                down vote



                accepted










                You can also use LinearSolve



                A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
                B = points[[All, 2]];



                $A=left(
                beginarraycccc
                0 & 0 & 0 & 1 \
                1 & 1 & 1 & 1 \
                8 & 4 & 2 & 1 \
                27 & 9 & 3 & 1 \
                endarray
                right) qquad B=left(
                beginarrayc
                4 \
                0 \
                -1 \
                0 \
                endarray
                right)$




                LinearSolve[A, B]



                -(1/6), 2, -(35/6), 4







                share|improve this answer
























                  up vote
                  6
                  down vote



                  accepted










                  You can also use LinearSolve



                  A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
                  B = points[[All, 2]];



                  $A=left(
                  beginarraycccc
                  0 & 0 & 0 & 1 \
                  1 & 1 & 1 & 1 \
                  8 & 4 & 2 & 1 \
                  27 & 9 & 3 & 1 \
                  endarray
                  right) qquad B=left(
                  beginarrayc
                  4 \
                  0 \
                  -1 \
                  0 \
                  endarray
                  right)$




                  LinearSolve[A, B]



                  -(1/6), 2, -(35/6), 4







                  share|improve this answer






















                    up vote
                    6
                    down vote



                    accepted







                    up vote
                    6
                    down vote



                    accepted






                    You can also use LinearSolve



                    A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
                    B = points[[All, 2]];



                    $A=left(
                    beginarraycccc
                    0 & 0 & 0 & 1 \
                    1 & 1 & 1 & 1 \
                    8 & 4 & 2 & 1 \
                    27 & 9 & 3 & 1 \
                    endarray
                    right) qquad B=left(
                    beginarrayc
                    4 \
                    0 \
                    -1 \
                    0 \
                    endarray
                    right)$




                    LinearSolve[A, B]



                    -(1/6), 2, -(35/6), 4







                    share|improve this answer












                    You can also use LinearSolve



                    A = CoefficientArrays[f@points[[All, 1]], a, b, c, d] //Last;
                    B = points[[All, 2]];



                    $A=left(
                    beginarraycccc
                    0 & 0 & 0 & 1 \
                    1 & 1 & 1 & 1 \
                    8 & 4 & 2 & 1 \
                    27 & 9 & 3 & 1 \
                    endarray
                    right) qquad B=left(
                    beginarrayc
                    4 \
                    0 \
                    -1 \
                    0 \
                    endarray
                    right)$




                    LinearSolve[A, B]



                    -(1/6), 2, -(35/6), 4








                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 15 at 13:56









                    Okkes Dulgerci

                    3,0841615




                    3,0841615




















                        up vote
                        5
                        down vote













                        Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve



                        points = 0, 4, 1, 0, 2, -1, 3, 0
                        (* 0, 4, 1, 0, 2, -1, 3, 0 *)

                        f[x_] := a x^3 + b x^2 + c x + d


                        (f[#1] == #2) & @@@ points
                        (*

                        d == 4,
                        a + b + c + d == 0,
                        8 a + 4 b + 2 c + d == -1,
                        27 a + 9 b + 3 c + d == 0

                        *)


                        Solve[
                        (f[#1] == #2) & @@@ points
                        , a, b, c, d
                        ]
                        (* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)





                        share|improve this answer
























                          up vote
                          5
                          down vote













                          Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve



                          points = 0, 4, 1, 0, 2, -1, 3, 0
                          (* 0, 4, 1, 0, 2, -1, 3, 0 *)

                          f[x_] := a x^3 + b x^2 + c x + d


                          (f[#1] == #2) & @@@ points
                          (*

                          d == 4,
                          a + b + c + d == 0,
                          8 a + 4 b + 2 c + d == -1,
                          27 a + 9 b + 3 c + d == 0

                          *)


                          Solve[
                          (f[#1] == #2) & @@@ points
                          , a, b, c, d
                          ]
                          (* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)





                          share|improve this answer






















                            up vote
                            5
                            down vote










                            up vote
                            5
                            down vote









                            Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve



                            points = 0, 4, 1, 0, 2, -1, 3, 0
                            (* 0, 4, 1, 0, 2, -1, 3, 0 *)

                            f[x_] := a x^3 + b x^2 + c x + d


                            (f[#1] == #2) & @@@ points
                            (*

                            d == 4,
                            a + b + c + d == 0,
                            8 a + 4 b + 2 c + d == -1,
                            27 a + 9 b + 3 c + d == 0

                            *)


                            Solve[
                            (f[#1] == #2) & @@@ points
                            , a, b, c, d
                            ]
                            (* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)





                            share|improve this answer












                            Four points define the polynomial unambiguously, not fitting is necesary, so for an exact solution I would do a system of equations and use Solve



                            points = 0, 4, 1, 0, 2, -1, 3, 0
                            (* 0, 4, 1, 0, 2, -1, 3, 0 *)

                            f[x_] := a x^3 + b x^2 + c x + d


                            (f[#1] == #2) & @@@ points
                            (*

                            d == 4,
                            a + b + c + d == 0,
                            8 a + 4 b + 2 c + d == -1,
                            27 a + 9 b + 3 c + d == 0

                            *)


                            Solve[
                            (f[#1] == #2) & @@@ points
                            , a, b, c, d
                            ]
                            (* a -> -(1/6), b -> 2, c -> -(35/6), d -> 4 *)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 15 at 8:40









                            rhermans

                            21.6k439104




                            21.6k439104




















                                up vote
                                5
                                down vote













                                fit = FindFit[points, f[x], a, b, c, d, x]



                                a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.




                                Plot[Evaluate[f[x] /. fit], x, 0, 5, 
                                Epilog -> PointSize[Large], Red, Point@points]


                                enter image description here



                                To get exact results, you can use



                                Rationalize[fit] 



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4




                                Alternatively, you can use Reduce or Solve (as in rhermans's answer) with alternative specification of the first argument:



                                ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
                                Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4







                                share|improve this answer






















                                • Could you make the coefficients in exact forms?
                                  – Friendly Ghost
                                  Aug 15 at 7:51










                                • @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                  – kglr
                                  Aug 15 at 7:54










                                • @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                  – rhermans
                                  Aug 15 at 10:12














                                up vote
                                5
                                down vote













                                fit = FindFit[points, f[x], a, b, c, d, x]



                                a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.




                                Plot[Evaluate[f[x] /. fit], x, 0, 5, 
                                Epilog -> PointSize[Large], Red, Point@points]


                                enter image description here



                                To get exact results, you can use



                                Rationalize[fit] 



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4




                                Alternatively, you can use Reduce or Solve (as in rhermans's answer) with alternative specification of the first argument:



                                ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
                                Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4







                                share|improve this answer






















                                • Could you make the coefficients in exact forms?
                                  – Friendly Ghost
                                  Aug 15 at 7:51










                                • @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                  – kglr
                                  Aug 15 at 7:54










                                • @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                  – rhermans
                                  Aug 15 at 10:12












                                up vote
                                5
                                down vote










                                up vote
                                5
                                down vote









                                fit = FindFit[points, f[x], a, b, c, d, x]



                                a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.




                                Plot[Evaluate[f[x] /. fit], x, 0, 5, 
                                Epilog -> PointSize[Large], Red, Point@points]


                                enter image description here



                                To get exact results, you can use



                                Rationalize[fit] 



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4




                                Alternatively, you can use Reduce or Solve (as in rhermans's answer) with alternative specification of the first argument:



                                ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
                                Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4







                                share|improve this answer














                                fit = FindFit[points, f[x], a, b, c, d, x]



                                a -> -0.166667, b -> 2., c -> -5.83333, d -> 4.




                                Plot[Evaluate[f[x] /. fit], x, 0, 5, 
                                Epilog -> PointSize[Large], Red, Point@points]


                                enter image description here



                                To get exact results, you can use



                                Rationalize[fit] 



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4




                                Alternatively, you can use Reduce or Solve (as in rhermans's answer) with alternative specification of the first argument:



                                ToRules @ Reduce[f /@ points[[All, 1]] == points[[All, 2]]] (* or *)
                                Solve[f /@ points[[All, 1]] == points[[All, 2]], a, b, c, d][[1]]



                                a -> -(1/6), b -> 2, c -> -(35/6), d -> 4








                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Aug 15 at 13:02

























                                answered Aug 15 at 7:49









                                kglr

                                160k8184384




                                160k8184384











                                • Could you make the coefficients in exact forms?
                                  – Friendly Ghost
                                  Aug 15 at 7:51










                                • @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                  – kglr
                                  Aug 15 at 7:54










                                • @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                  – rhermans
                                  Aug 15 at 10:12
















                                • Could you make the coefficients in exact forms?
                                  – Friendly Ghost
                                  Aug 15 at 7:51










                                • @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                  – kglr
                                  Aug 15 at 7:54










                                • @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                  – rhermans
                                  Aug 15 at 10:12















                                Could you make the coefficients in exact forms?
                                – Friendly Ghost
                                Aug 15 at 7:51




                                Could you make the coefficients in exact forms?
                                – Friendly Ghost
                                Aug 15 at 7:51












                                @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                – kglr
                                Aug 15 at 7:54




                                @FriendlyGhost, you can use Rationalize[fit] to get a -> -(1/6), b -> 2, c -> -(35/6), d -> 4
                                – kglr
                                Aug 15 at 7:54












                                @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                – rhermans
                                Aug 15 at 10:12




                                @FriendlyGhost Four points define the polynomial unambiguously, so an exact solution is possible solving the system of equations, see my answer.
                                – rhermans
                                Aug 15 at 10:12










                                up vote
                                2
                                down vote













                                You can use InterpolatingPolynomial:



                                Expand @ InterpolatingPolynomial[
                                0,4,1,0,2,-1,3,0,
                                x
                                ]



                                4 - (35 x)/6 + 2 x^2 - x^3/6







                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  You can use InterpolatingPolynomial:



                                  Expand @ InterpolatingPolynomial[
                                  0,4,1,0,2,-1,3,0,
                                  x
                                  ]



                                  4 - (35 x)/6 + 2 x^2 - x^3/6







                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    You can use InterpolatingPolynomial:



                                    Expand @ InterpolatingPolynomial[
                                    0,4,1,0,2,-1,3,0,
                                    x
                                    ]



                                    4 - (35 x)/6 + 2 x^2 - x^3/6







                                    share|improve this answer












                                    You can use InterpolatingPolynomial:



                                    Expand @ InterpolatingPolynomial[
                                    0,4,1,0,2,-1,3,0,
                                    x
                                    ]



                                    4 - (35 x)/6 + 2 x^2 - x^3/6








                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Aug 15 at 17:01









                                    Carl Woll

                                    57.3k273149




                                    57.3k273149



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180038%2fhow-to-easily-find-the-coefficients-of-a-cubic-polynomial-and-its-plot-for-the-g%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        L,SDt4WkKpCsjZ9a7HMZrQMA,wBZiGSZD882O8wZljCLvussz6XAetXlRO s2
                                        hMfEy2mWmKHI6nqN fjhceWNHU8Nw3Q,3 v,6F6s 4n9dJ9RLzhPAnCvD,OIsTefo BAHYY ZrSEcrdZkcCEirV9xUM1

                                        Popular posts from this blog

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

                                        How many registers does an x86_64 CPU actually have?

                                        Displaying single band from multi-band raster using QGIS