This code where I try to visualize Newton's Method on the function $f(x)=\sqrt{|x|}$ works fine when I define $f(x)$ piecewise. Then why doesn't it work when I define f[x_] = Sqrt[Abs[x]]
?
This works:
Clear["Global`*"];
f[x_] = Piecewise[{{Sqrt[x], x >= 0}, {Sqrt[-x], x < 0}}];
guess = 5;
iter = 10;
xn = NestList[# - f[#]/f'[#] &, guess, iter] // N;
fn = f /@ xn;
dots = Transpose[{xn, fn}] /. {x_, y_} :> Sequence[{x, 0}, {x, y}];
Show[Plot[f[x], {x, -5, 5}], Graphics[Line[dots]]]
This doesn't work:
Clear["Global`*"];
f[x_] = Sqrt[Abs[x]];
guess = 5;
iter = 10;
xn = NestList[# - f[#]/f'[#] &, guess, iter] // N;
fn = f /@ xn;
dots = Transpose[{xn, fn}] /. {x_, y_} :> Sequence[{x, 0}, {x, y}];
Show[Plot[f[x], {x, -5, 5}], Graphics[Line[dots]]]
But why???