Message Boards Message Boards

0
|
2274 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

problem with drawing in a loop

Posted 10 years ago
Hi! I draw graphic using function 

gr1[f_, xmin_, xmax_, slices_] :=
  Module[{span = (xmax - xmin)/slices, ext},
   ext[op_, r_] := First@Quiet@op[{f, r <= x <= r + span}, x];
   Plot[f, {x, xmin, xmax},
    Prolog -> {FaceForm, EdgeForm,
      Rectangle @@@ ({{#, ext[Minimize, #]}, {# + span,
            ext[Maximize, #]}} & /@ Most@Range[xmin, xmax, span])}]];
f[x_] := x + 3*Sin[x + 2];

gr1[f, -5, -3, 10]

then I want loop this drawing ,for example  a=-5,b=-3,eps=0.1   

While[abs(b-a)>eps,gr1[f, a, b, 10]; a=(a+b)/2+0,5,b=(a+b)/2+0,5)]
but when I run this code it doesn't work, please help)
3 Replies
Well, fixed; Even if you initalize eps, a, and b and use function name Abs[], it does not plot (see evaluation of While[])
With[{eps = 0.1},
a = 10; b = 20;
While[Abs[b - a] > eps,
  gr1[f, a, b, 10];
  a = (a + b)/2 - 1;
  b = (a + b)/2 + 1]
]
so, you manage to see the array of graphics this way
gr1[f, Sequence @@ #, 10]& /@ FixedPointList[{(#[[1]] + #[[2]])/2 - 1, (#[[1]] + #[[2]])/2 + 1} &, {10, 20}]
POSTED BY: Udo Krause
try this
Attachments:
If you enter the x'es to the places where they are needed,
 Clear[gr1]
 
 gr1[f_, xmin_, xmax_, slices_] :=
  Module[{span = (xmax - xmin)/slices, ext, x},
   ext[op_, r_] := First@Quiet@op[{f[x], r <= x <= r + span}, x];
   Plot[f[x], {x, xmin, xmax},
    Prolog -> {FaceForm, EdgeForm,
      Rectangle @@@ ({{#, ext[Minimize, #]}, {# + span,
            ext[Maximize, #]}} & /@ Most@Range[xmin, xmax, span])}]]

you get a picture throwing an error message "EdgeForm is not a Graphics primitve or directive"
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract