MathGroup Archive 2003

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

Search the Archive

Re: couple of small problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40852] Re: couple of small problems
  • From: Raibatak Das <rd54 at cornell.edu>
  • Date: Tue, 22 Apr 2003 06:43:49 -0400 (EDT)
  • References: <b80k2i$m3n$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

jr -

in the second problem (where mathematica goes into an infinite loop) the 
problem arises because of the line

Blist = AppendTo[Blist, x]

the AppendTo command automatically updates the list. so you should use 
either,

While[Length[Blist] < 100, x = Random[];
If[h[x] < g[x], AppendTo[Blist, x]]]

or 

While[Length[Blist] < 100, x = Random[];
Blist = Append[Blist, x]

i believe that will take care of the problem with generating the list 
with exactly 100 elements using the while loop.

- rd.

Jake Rodriguez Rosales wrote:

>Dear All,
>
>Hi. I have a couple of small problems which I hope you can help me with.
>First, suppose I have a function which behaves one way in a certain
>interval and behaves another way in a different interval. Suppose:
>
>	f1[x_] := Cos[x] 	0 < Cos[x] < 0.6
>and
>	f2[x_] := (1 + Sin[x]/3)*Cos[x] 	-1 < Cos[x] < 0
>
>How do I combine these two functions into one main function, FMain[x],
>which incorporates the behaviour in both intervals (i.e. which gives
>me f1[x] if 0 < Cos[x] < 0.6 and gives me f2[x] for the other interval)?
>Is there a specific command in Mathematica to deal with this?
>
>The next problem mainly concerns with looping structures. I initially have
>an empty list, say Blist, which I want to continually fill up until
>I have say, 100 elements in the list. I do this by appending an
>element x, to the list, if the element satisfies some particular
>condition.  So, for example, say I have two functions, h[x] and
>g[x]. I generate x (how I generate x is not important), and check if
>h[x] < g[x]. If it is, I decide to keep x and append it to the
>list Blist. I want to do this until the Length[Blist] = 100.
>I tried this code:
>
>Blist = {}
>
>While[Length[Blist] < 100, x = Random[];
>If[h[x] < g[x], Blist = AppendTo[Blist, x]]]
>
>Blist
>
>but somehow Mathematica simply runs for a long time and then hangs.
>I have tried another simple code:
>
>Blist = {}
>
>Do[x = Random[]; If[h[x] < g[x],
>Blist = AppendTo[Blist, x]], {i, 1, 100}]
>
>Blist
>
>This gives me a list of x values, but falls way short of 100 values
>due to the condition checking statement (obviously, I could iterate
>it until say 150 {i, 1, 150} so that I can get somewhere close to
>100, but controlling the number of elements in Blist is important).
>Essentially, I want to be able to check if x satisfies a certain
>condition, and append it to Blist until I have say 100 elements in
>Blist. Any comments are welcome.
>
>
>Jake
>
>
>
>  
>

-- 
------------------------------------------------------------------------
* /Raibatak Das / *
Department of Chemistry and Chemical Biology, Cornell University.
Ithaca, NY 14853.
Ph : 1-607-255-6141
email : rd54 at cornell.edu <mailto:rd54 at cornell.edu>



  • Prev by Date: Re: couple of small problems
  • Next by Date: Re: New Mathematica book with new codes
  • Previous by thread: Re: couple of small problems
  • Next by thread: Re: couple of small problems