I wrote a code for bisection method root approximation. However, I wanted to know the value of the root every iteration. I tried using the Union function to save each iterated value in a set but it saves the numbers in increasing order.
I am reading on NestWhileList but I am new to Mathematica and I still do not understand it much. Is there a way to use it?
f[x_] := x - E^(-1.2*x);
a = 0.5; b = 1;(*a<b*)
c = 0;
\[Epsilon] = 0.001; (*error*)
iter = 0;
cList = {};
If[f[a]*f[b] > 0,
Print["Conditions of Bisection Method are not met. Disregard \
solution below."], "See solution below"]
While[Abs[b - a]/2 > \[Epsilon],
c = (a + b)/2;
iter = iter + 1; (*number of iterations*)
cList = cList \[Union] {c};
f[a];(*left-hand side*)
f[b];(*right-hand side*)
f[c];(*mid-point*)
If[f[a]*f[c] < 0, b = c, a = c];
];
N[c] (*root*)
iter(*number of iterations*)
cList (*iterated roots*)
f
?? not defined anywhere. You sayIf[f[a]*f[b] > 0
but there is nof
to be seen before that. $\endgroup$Sow
andReap
but can't show you how, since your code does not run and missing definitions. $\endgroup$cList = cList \[Union] {c};
tocList = Append[cList, c];
$\endgroup$