Newton's Method

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











up vote
2
down vote

favorite












enter image description here



I'm currently new to Mathematica and have been trying to solve this problem. I was digging around and found this code:



newtonmethod[error_, initial_, maxiteration_, errorpower_] := 
Module[,
g[x_] := D[f[x], x];
h[t_] := t - f[t]/g[t];
guess = initial;
tol = error;
errorset = ;
ratios = ;
Do[p = h[t] /. t -> guess;
tol = Abs[p - guess];
AppendTo[errorset, tol];
Print["n = ", n, ", x= ", N[ p], ", error =", N[ tol]];
guess = p;
If[tol <= error [Or] Chop[g[t] /. t -> guess] == 0,
Goto["errorcalculation"]], n, 1, maxiteration];
Label["errorcalculation"];
Do[AppendTo[ratios, errorset[[i + 1]]/errorset[[i]]^errorpower], i,
1, Length[errorset] - 1];
Print["Here are the error ratios n"];
TableForm[N[ratios]]
]


I'm not really sure on how to use it and/or if it's enough to complete the problem. Any help would be greatly appreciated.










share|improve this question









New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
    – bbgodfrey
    6 hours ago














up vote
2
down vote

favorite












enter image description here



I'm currently new to Mathematica and have been trying to solve this problem. I was digging around and found this code:



newtonmethod[error_, initial_, maxiteration_, errorpower_] := 
Module[,
g[x_] := D[f[x], x];
h[t_] := t - f[t]/g[t];
guess = initial;
tol = error;
errorset = ;
ratios = ;
Do[p = h[t] /. t -> guess;
tol = Abs[p - guess];
AppendTo[errorset, tol];
Print["n = ", n, ", x= ", N[ p], ", error =", N[ tol]];
guess = p;
If[tol <= error [Or] Chop[g[t] /. t -> guess] == 0,
Goto["errorcalculation"]], n, 1, maxiteration];
Label["errorcalculation"];
Do[AppendTo[ratios, errorset[[i + 1]]/errorset[[i]]^errorpower], i,
1, Length[errorset] - 1];
Print["Here are the error ratios n"];
TableForm[N[ratios]]
]


I'm not really sure on how to use it and/or if it's enough to complete the problem. Any help would be greatly appreciated.










share|improve this question









New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
    – bbgodfrey
    6 hours ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











enter image description here



I'm currently new to Mathematica and have been trying to solve this problem. I was digging around and found this code:



newtonmethod[error_, initial_, maxiteration_, errorpower_] := 
Module[,
g[x_] := D[f[x], x];
h[t_] := t - f[t]/g[t];
guess = initial;
tol = error;
errorset = ;
ratios = ;
Do[p = h[t] /. t -> guess;
tol = Abs[p - guess];
AppendTo[errorset, tol];
Print["n = ", n, ", x= ", N[ p], ", error =", N[ tol]];
guess = p;
If[tol <= error [Or] Chop[g[t] /. t -> guess] == 0,
Goto["errorcalculation"]], n, 1, maxiteration];
Label["errorcalculation"];
Do[AppendTo[ratios, errorset[[i + 1]]/errorset[[i]]^errorpower], i,
1, Length[errorset] - 1];
Print["Here are the error ratios n"];
TableForm[N[ratios]]
]


I'm not really sure on how to use it and/or if it's enough to complete the problem. Any help would be greatly appreciated.










share|improve this question









New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











enter image description here



I'm currently new to Mathematica and have been trying to solve this problem. I was digging around and found this code:



newtonmethod[error_, initial_, maxiteration_, errorpower_] := 
Module[,
g[x_] := D[f[x], x];
h[t_] := t - f[t]/g[t];
guess = initial;
tol = error;
errorset = ;
ratios = ;
Do[p = h[t] /. t -> guess;
tol = Abs[p - guess];
AppendTo[errorset, tol];
Print["n = ", n, ", x= ", N[ p], ", error =", N[ tol]];
guess = p;
If[tol <= error [Or] Chop[g[t] /. t -> guess] == 0,
Goto["errorcalculation"]], n, 1, maxiteration];
Label["errorcalculation"];
Do[AppendTo[ratios, errorset[[i + 1]]/errorset[[i]]^errorpower], i,
1, Length[errorset] - 1];
Print["Here are the error ratios n"];
TableForm[N[ratios]]
]


I'm not really sure on how to use it and/or if it's enough to complete the problem. Any help would be greatly appreciated.







equation-solving function-construction numerics homework






share|improve this question









New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 34 secs ago









Michael E2

141k11191457




141k11191457






New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









JVang10

111




111




New contributor




JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JVang10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
    – bbgodfrey
    6 hours ago












  • 1




    Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
    – bbgodfrey
    6 hours ago







1




1




Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
– bbgodfrey
6 hours ago




Define the function for which a zero is desired, for instance, f[x_] := (x - 1)^2. Then, execute newtonmethod, for instance, newtonmethod[.0001, .1, 20, 2].
– bbgodfrey
6 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote













I offer an implementation more of Mathematica's style, I suppose,



Clear[findRootByNewton]
findRootByNewton[f : _Symbol | _Function, initPt_Real, η_:1.*^-6] := Module[df,
df = Derivative[1][func];
NestWhileList[# - f[#]/df[#] &, initPt, Abs[Subtract[##]] >= η &, 2]
]


One needs to provide the function (of pure function form or defined by SetDelayed (:=)) whose root to be found, the initial guess and an optional precision goal with a default value of $ 1.0times 10^-6 $.



Then I use $ f(x)=x^2-2 $ as an example. Either



findRootByNewton[#^2 - 2 &, 1.]


or



f[x_] := x^2 - 2
findRootByNewton[f, 1.]


returns




1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421



The result shows root approximations found at each iteration, until the preset precision goal is reached.




P.S.



If one just wants the final result, use NestWhile instead of NestWhileList.






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
    );



    );






    JVang10 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















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

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    I offer an implementation more of Mathematica's style, I suppose,



    Clear[findRootByNewton]
    findRootByNewton[f : _Symbol | _Function, initPt_Real, η_:1.*^-6] := Module[df,
    df = Derivative[1][func];
    NestWhileList[# - f[#]/df[#] &, initPt, Abs[Subtract[##]] >= η &, 2]
    ]


    One needs to provide the function (of pure function form or defined by SetDelayed (:=)) whose root to be found, the initial guess and an optional precision goal with a default value of $ 1.0times 10^-6 $.



    Then I use $ f(x)=x^2-2 $ as an example. Either



    findRootByNewton[#^2 - 2 &, 1.]


    or



    f[x_] := x^2 - 2
    findRootByNewton[f, 1.]


    returns




    1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421



    The result shows root approximations found at each iteration, until the preset precision goal is reached.




    P.S.



    If one just wants the final result, use NestWhile instead of NestWhileList.






    share|improve this answer


























      up vote
      3
      down vote













      I offer an implementation more of Mathematica's style, I suppose,



      Clear[findRootByNewton]
      findRootByNewton[f : _Symbol | _Function, initPt_Real, η_:1.*^-6] := Module[df,
      df = Derivative[1][func];
      NestWhileList[# - f[#]/df[#] &, initPt, Abs[Subtract[##]] >= η &, 2]
      ]


      One needs to provide the function (of pure function form or defined by SetDelayed (:=)) whose root to be found, the initial guess and an optional precision goal with a default value of $ 1.0times 10^-6 $.



      Then I use $ f(x)=x^2-2 $ as an example. Either



      findRootByNewton[#^2 - 2 &, 1.]


      or



      f[x_] := x^2 - 2
      findRootByNewton[f, 1.]


      returns




      1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421



      The result shows root approximations found at each iteration, until the preset precision goal is reached.




      P.S.



      If one just wants the final result, use NestWhile instead of NestWhileList.






      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        I offer an implementation more of Mathematica's style, I suppose,



        Clear[findRootByNewton]
        findRootByNewton[f : _Symbol | _Function, initPt_Real, η_:1.*^-6] := Module[df,
        df = Derivative[1][func];
        NestWhileList[# - f[#]/df[#] &, initPt, Abs[Subtract[##]] >= η &, 2]
        ]


        One needs to provide the function (of pure function form or defined by SetDelayed (:=)) whose root to be found, the initial guess and an optional precision goal with a default value of $ 1.0times 10^-6 $.



        Then I use $ f(x)=x^2-2 $ as an example. Either



        findRootByNewton[#^2 - 2 &, 1.]


        or



        f[x_] := x^2 - 2
        findRootByNewton[f, 1.]


        returns




        1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421



        The result shows root approximations found at each iteration, until the preset precision goal is reached.




        P.S.



        If one just wants the final result, use NestWhile instead of NestWhileList.






        share|improve this answer














        I offer an implementation more of Mathematica's style, I suppose,



        Clear[findRootByNewton]
        findRootByNewton[f : _Symbol | _Function, initPt_Real, η_:1.*^-6] := Module[df,
        df = Derivative[1][func];
        NestWhileList[# - f[#]/df[#] &, initPt, Abs[Subtract[##]] >= η &, 2]
        ]


        One needs to provide the function (of pure function form or defined by SetDelayed (:=)) whose root to be found, the initial guess and an optional precision goal with a default value of $ 1.0times 10^-6 $.



        Then I use $ f(x)=x^2-2 $ as an example. Either



        findRootByNewton[#^2 - 2 &, 1.]


        or



        f[x_] := x^2 - 2
        findRootByNewton[f, 1.]


        returns




        1., 1.5, 1.41667, 1.41422, 1.41421, 1.41421



        The result shows root approximations found at each iteration, until the preset precision goal is reached.




        P.S.



        If one just wants the final result, use NestWhile instead of NestWhileList.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 36 mins ago

























        answered 49 mins ago









        Αλέξανδρος Ζεγγ

        2,304725




        2,304725




















            JVang10 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            JVang10 is a new contributor. Be nice, and check out our Code of Conduct.












            JVang10 is a new contributor. Be nice, and check out our Code of Conduct.











            JVang10 is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














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

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery