dottie = FindRoot[Cos[x] == x, {x, 1}] // Values // First
0.739085
Plot[{Cos[x], x}, {x, -5, 5},
Epilog -> {Red, PointSize[0.02], Point[{dottie, dottie}]}]
Convergence can be seen with EvaluationMonitor
{res, {evx}} =
Reap[FindRoot[Cos[x] == x, {x, 0}, EvaluationMonitor :> Sow[x]]]
{{x -> 0.739085}, {{0., 1., 0.750364, 0.739113, 0.739085, 0.739085}}}
points = Point @ Transpose[{evx, evx}]
Plot[{Cos[x], x}, {x, -5, 5},
Epilog -> {Red, PointSize[0.02], points}]
Finding Dottie with Newton
fun = Cos[x] - x;
newton[fun_, n_] :=
With[{f = fun / D[fun, x]}, NestList[# - f /. x -> # &, 0., n]]
points = newton[fun, 5]
{0., 1., 0.750364, 0.739113, 0.739085, 0.739085}
dottie = Last @ points;
ListLinePlot[points,
Axes -> False,
Frame -> True,
FrameTicks -> {{{0, dottie, 1}, None}, {Automatic, None}},
GridLines -> {Automatic, {0, dottie, 1}},
Mesh -> All,
MeshStyle -> Directive[PointSize[Medium], Red],
ImageSize -> 500,
PlotRange -> {{0.9, 6.1}, {-0.1, 1.1}}]
FixedPointList
f = # / D[#, x] & [fun]
fpl1 = FixedPointList[# - f /. x -> # &, 0.0];
fpl2 = FixedPointList[# - f /. x -> # &, -0.5];
ListLinePlot[
{fpl1, fpl2},
Axes -> False,
Frame -> True,
FrameTicks -> {{{-0.5, 0, dottie, 2}, None}, {Automatic, None}},
GridLines -> {Automatic, {-0.5, 0, dottie, 2}},
Filling -> {1 -> {2}},
Mesh -> All,
MeshStyle -> Directive[PointSize[Medium], Red],
ImageSize -> 500,
PlotLegends -> {"Start at 0.0", "Start at -0.5"},
PlotRange -> {{0.9, 8.1}, {-0.6, 2.2}}]
Interpolation
fun = Cos[x] - x;
f = #/D[#, x] & [fun];
fpl = FixedPointList[# - f /. x -> # &, #] & /@ {0., -0.5, 3.0};
dottie = fpl[[1, -1]];
ListLinePlot[
fpl,
InterpolationOrder -> 2,
Axes -> False,
Frame -> True,
FrameTicks -> {{{-0.5, 0, dottie, 2, 3}, None}, {Automatic, None}},
GridLines -> {Automatic, {-0.5, 0, dottie, 2, 3}},
Filling -> {{1 -> {2}}, {2 -> {3}}},
FillingStyle -> Directive[Opacity[0.5], Gray],
Mesh -> False,
ImageSize -> 500,
PlotLegends -> {"Start at 0.0", "Start at -0.5", "Start at 3.0"},
PlotStyle -> Thickness[0.01],
PlotRange -> {{0.9, 7.1}, {-0.6, 3.1}}]
RSolve
and thenScope
. $\endgroup$