I wonder if you can help me with my program - it involves using Newton's Method but my main issue is getting a defined function to return a given value.
Essentially, I want to get my function findRoot
to return the value of the root or Null
if it doesn't find one. Here is the code for this function:
findRoot[x0_] := Module[{Root},
(*Initialise dummy variable and counter index thing *)
dumx = x0;
l = 0;
While[l < 100,
Root = Iter[dumx];
If[Norm[dumx - Root] < TOL, Break[], dumx = Root]
(*If true, break the while loop and return root, if false let dumx=Root*)
; l++]
If[l == 100, Null, Root]
];
Iter[]
is just another function that performs iterations of Newton's method, I made it separate to allow ease of debugging!
Essentially, if I plug
{-0.3,-0.3,-0.3,-0.3}
into this algorithm, (With certain global variables set in ways where I already know what roots should come out of this function) I get
{-0.2Null,-0.2Null,-0.2Null,-0.2Null}
and I have no idea where the null is coming from. The result should read {-0.2,-0.2,-0.2,-0.2}
. I suspect the semi-colon on line 7 is responsible for this Null
, but removing it causes greater issues.
FixedPoint[{#[[1]] + 1, iter[#[[2]]]} &, {0, x0}, 100, SameTest -> (Abs[#1[[2]] - #2[[2]]] < tol &)] /. {{100,x_} :> Null, {a_, b_} :> b }
, by the way. $\endgroup$ – Patrick Stevens Sep 4 '15 at 14:10