MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: IntegerPart

On 4/1/13 at 12:58 AM, dougwangsaif at wrote:

>can anyone help with this. There seems to be something wrong with
>the IntegerPart function. Please help!

>Clear["Global`*"] nSize = 100;

>prefstate = Table[0, {i, 1, nSize}]; Do[
>{prefstate[[i]] = If[EvenQ[i], 0.8, 0.4]}, {i, 1, nSize}

>Plot[{prefstate[[IntegerPart[x]]]}, {x, 1, nSize + 1},
>AxesOrigin -> {0, 0}, TicksStyle -> Directive[18], PlotStyle ->

No, the issue isn't with IntegerPart. Instead it is the sampling
routine used by Plot. To see this is the case, force Plot to
sample with higher resolution. That is

Plot[{prefstate[[IntegerPart[x]]]}, {x, 1, nSize + 1},
  AxesOrigin -> {0, 0}, TicksStyle -> Directive[18],
  PlotStyle -> {Black}, PlotPoints -> 150]

will generate the plot you were expecting.

A couple of other things you might want to be aware of.

There is no point to doing Do[....]; since the return value from
Do is (Null)

A more efficient way to generate prefstate would be

prefstate=Table[If[EvenQ@n, 0.8, 0.4],{n, nSize}]

There is no need to initialize prefstate with 0's prior to
setting it to alternating values.

But if you are going to use initialize prefstate and use an
explicit Do loop, the following produces the same result for
prefstate but will run faster

  prefstate={prefstate, If[EvenQ@n, 0.8, 0.4]},{n, nSize}];

Notice here there is a point to doing Do[...];prefstate=...

  • Prev by Date: Re: A question about numerically solving differential equations
  • Next by Date: Re: IntegerPart
  • Previous by thread: Re: IntegerPart
  • Next by thread: Re: IntegerPart