Newton's Method
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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
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.
add a comment |Â
up vote
2
down vote
favorite
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
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, executenewtonmethod
, for instance,newtonmethod[.0001, .1, 20, 2]
.
– bbgodfrey
6 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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.
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
equation-solving function-construction numerics homework
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.
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, executenewtonmethod
, for instance,newtonmethod[.0001, .1, 20, 2]
.
– bbgodfrey
6 hours ago
add a comment |Â
1
Define the function for which a zero is desired, for instance,f[x_] := (x - 1)^2
. Then, executenewtonmethod
, 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
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
edited 36 mins ago
answered 49 mins ago


ΑλÎÂξανδÃÂο Ζεγγ
2,304725
2,304725
add a comment |Â
add a comment |Â
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.
JVang10 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Define the function for which a zero is desired, for instance,
f[x_] := (x - 1)^2
. Then, executenewtonmethod
, for instance,newtonmethod[.0001, .1, 20, 2]
.– bbgodfrey
6 hours ago