4
$\begingroup$

I have created a For loop that solves f(x)=0 using the Bisection method:

a = -Pi/2;
b = 3 Pi/4;
tolorance = 0.0000;
f[x_] := Sin[x] && a <= x <= b;
For[i = 1, i <= 11, i++,
 {c = (a + b)/2, If[f[a]*f[c] < tolorance, b = c, a = c],
  Print[{i, N[a, 4], N[b, 4], N[c, 4], N[f[c], 4]}]}]

This prints the following output:

{1,-1.571,0.3927,0.3927,0.3827}

{2,-0.5890,0.3927,-0.5890,-0.5556}

{3,-0.09817,0.3927,-0.09817,-0.09802}

{4,-0.09817,0.1473,0.1473,0.1467}

{5,-0.09817,0.02454,0.02454,0.02454}

{6,-0.03682,0.02454,-0.03682,-0.03681}

{7,-0.006136,0.02454,-0.006136,-0.006136}

{8,-0.006136,0.009204,0.009204,0.009204}

{9,-0.006136,0.001534,0.001534,0.001534}

{10,-0.002301,0.001534,-0.002301,-0.002301}

{11,-0.0003835,0.001534,-0.0003835,-0.0003835}

Which is correct. However I can not find a method to transform this into a table. Do any of you have ideas on how to do this, preferably with headers?

Thanks a lot in advance.

$\endgroup$
3

3 Answers 3

7
$\begingroup$

Table is the preferred form of usage in Mma and you can assign this table a name for later use. With the function as defined above,

(a = -Pi/2;
  b = 3 Pi/4;
  tolerance = 0.0000;
  t = Table[c = (a + b)/2; If[f[a]*f[c] < tolerance, b = c, a = c];
     {i, N[a, 4], N[b, 4], N[c, 4], N[f[c], 4]}, {i, 1, 11}]);

t // Grid[#, Alignment -> "."] &

EDIT: Added headings/formatting

Headings and row numbers are required for presentation. At the same time, the table of data should also remain accessible. I have added a prec variable so that you can change precision easily. There are better ways of writing it all but let's defer that for another day.

(Clear["Global`*"];
  f[x_] := Sin[x] && a <= x <= b;
  a = -Pi/2;
  b = 3 Pi/4;
  prec = 6;
  tolerance = 0.0000;
  t = Table[c = (a + b)/2; If[f[a]*f[c] < tolerance, b = c, a = c];
     {i, N[a, prec], N[b, prec], N[c, prec], N[f[c], prec]}, {i, 1, 
     11}];
  PrependTo[t, #] & @ {"#", "a", "b", "c", "f[c]"};
  );

.. and now the improved presentation:

t // Grid[#, Alignment -> {".", Automatic, 
     ({1, #} -> Center) & /@ Range[5]
     },
   Frame -> All,
   Spacings -> {1, 1.4},
   Background -> {None,
     {Blend[{Cyan, Gray}], {White, Lighter@LightBlue}}}
   ] &

enter image description here

If you still want just the data (without the first row and first column), then you can have it as s:

(s = t[[2 ;; -1, 2 ;; -1]]) // MatrixForm
$\endgroup$
6
$\begingroup$
Clear["Global`*"]

a = -Pi/2;
b = 3 Pi/4;
tolorance = 0.0000;
f[x_] := Sin[x] && a <= x <= b;

EDIT: Added headings

Table[c = (a + b)/2;
  If[f[a]*f[c] < tolorance, b = c, a = c];
  {N[a, 4], N[b, 4], N[c, 4], N[f[c], 4]}, {i, 11}] //
 TableForm[#, TableHeadings -> {Range[11], {"a", "b", "c", "f(c)"}}] &

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Omit i from the output of Table[..] and the following looks nice: TableForm[ Table[..], TableHeadings -> {Automatic, List @@ HoldForm /@ Hold[a, b, c, f[c]]}, TableAlignments -> "." ] $\endgroup$
    – Michael E2
    Commented Sep 23, 2021 at 15:01
  • $\begingroup$ @MichaelE2 - Thanks. I had taken a slightly different approach prior to your comment. $\endgroup$
    – Bob Hanlon
    Commented Sep 23, 2021 at 15:05
  • $\begingroup$ @BobHanlon and MichaelE2, thank you both very much for your replies! I have tried to make the table function work without any luck. $\endgroup$
    – Sebastian
    Commented Sep 23, 2021 at 15:22
  • 1
    $\begingroup$ Aside from the automatic numbering, I was pointing out (for @Sebastian) that "." is a valid alignment, which is not mentioned in the docs for TableForm/TableAlignments. $\endgroup$
    – Michael E2
    Commented Sep 23, 2021 at 15:37
2
$\begingroup$

Try this:

P = {};(*empty list*)
a = -Pi/2;
b = 3 Pi/4;
tolorance = 0.0000;
f[x_] := Sin[x] && a <= x <= b;
For[i = 1, i <= 11, 
 i++, {c = (a + b)/2, If[f[a]*f[c] < tolorance, b = c, a = c], 
  AppendTo[P, {i, N[a, 4], N[b, 4], N[c, 4], N[f[c], 4]}]}]
P

and use Syed's code to improve the presentation:

PrependTo[P, #] &@{"#", "a", "b", "c", "f[c]"}// Grid[#, 
Alignment -> {".", Automatic, 
 ({1, #} -> Center) & /@ Range[5]
 },
Frame -> All,
Spacings -> {1, 1.4},
Background -> {None,
 {Blend[{Cyan, Gray}], {White, Lighter@LightBlue}}}
] &
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.