(* Examples of solving equations/finding roots *) (* Simply paste each one at the Mathemtica prompt *) (* Execute the first lines first. You may not need them if working within a notebook *) < {"Newton", "StepControl" -> None}] FindRoot[x^2 -1 == 0., {x, -1.1}, Method -> {"Newton", "StepControl" -> None}] (* The one below visualizaes the iterations. Just give the function, not f(x) == 0. Syntax quirk. *) (* may no longer work, need "ResourceFunction" below *) FindRootPlot[x^2 -1, {x, 0.2}, Method -> {"Newton", "StepControl" -> None}] ResourceFunction["FindRootPlot"][x^2 -1, {x, 0.2}, Method -> {"Newton", "StepControl" -> None}] (* "StepControl" -> None forces the text-book Newton's. We will not bother with this in what follows, it is still Newton's *) (* Newton's method's pitfalls *) FindRoot[x^2 -1 == 0., {x, 0}, Method -> Newton] (* A fix *) FindRoot[x^2 -1 == 0., {x, Random[]}, Method -> Newton] (* The one below visualizaes the iterations. Just give the function, no f(x) == 0. Syntax quirk. *) FindRootPlot[x^2 -1, {x, Random[]}, Method -> Newton] (* Quickly finds the correct solution at x=0 IF you start close to it. *) (* First, see what the function looks like *) Plot[x^2*Exp[-x^2], {x, 0, 4}] FindRootPlot[x^2*Exp[-x^2], {x, 0.2}, Method -> Newton] (* Run-away problem: unstable algorithm with the starting value not close to the root at x=0. *) FindRootPlot[x^2*Exp[-x^2], {x, 1.2}, Method -> Newton] (* The material below is for self-study *) (* If no analytic derivatives are available for the Erf[x] function, Mathematica will use Newton's method where the derivative is quickly estimated numerically (via secant line), see C&K book. *) (* Will be a bit slower *) FindRoot[Erf[x] == x, {x, {0, 1}} ] (* Below is an ill-conditioned problem: f(x) is too shallow around the root x=0, the convergence is poor, you may never get to the correct answer. *) FindRootPlot[x^30, {x, 0.1}, Method -> Newton] (* Two equations. Will not be on the quiz. *) FindRoot[{Sin[x] == Cos[y], x+y == 1}, {x, 0.1}, {y, 0.1}]