Skip to main content
added 1 character in body
Source Link
Nasser
  • 150.5k
  • 12
  • 161
  • 374

I assume you wanwant to collect the values of c in cList?

Just replace your loop with

cList=First@Last@Reap@While[Abs[b-a]/2>ϵ,
      c=(a+b)/2;
      iter=iter+1;(*number of iterations*)
      Sow[c];
      If[f[a]*f[c]<0,b=c,a=c]
];

Mathematica graphics

Lookup help on Sow and Reap

Btw, iter=iter+1 can be written as iter++ in Mathematica.

I assume you wan to collect the values of c in cList?

Just replace your loop with

cList=First@Last@Reap@While[Abs[b-a]/2>ϵ,
      c=(a+b)/2;
      iter=iter+1;(*number of iterations*)
      Sow[c];
      If[f[a]*f[c]<0,b=c,a=c]
];

Mathematica graphics

Lookup help on Sow and Reap

Btw, iter=iter+1 can be written as iter++ in Mathematica.

I assume you want to collect the values of c in cList?

Just replace your loop with

cList=First@Last@Reap@While[Abs[b-a]/2>ϵ,
      c=(a+b)/2;
      iter=iter+1;(*number of iterations*)
      Sow[c];
      If[f[a]*f[c]<0,b=c,a=c]
];

Mathematica graphics

Lookup help on Sow and Reap

Btw, iter=iter+1 can be written as iter++ in Mathematica.

Source Link
Nasser
  • 150.5k
  • 12
  • 161
  • 374

I assume you wan to collect the values of c in cList?

Just replace your loop with

cList=First@Last@Reap@While[Abs[b-a]/2>ϵ,
      c=(a+b)/2;
      iter=iter+1;(*number of iterations*)
      Sow[c];
      If[f[a]*f[c]<0,b=c,a=c]
];

Mathematica graphics

Lookup help on Sow and Reap

Btw, iter=iter+1 can be written as iter++ in Mathematica.