Re: couple of small problems
- To: mathgroup at smc.vnet.net
- Subject: [mg40862] Re: couple of small problems
- From: "Peter Pein" <peter1963 at totalise.co.uk>
- Date: Tue, 22 Apr 2003 06:47:11 -0400 (EDT)
- References: <b80k2i$m3n$1@smc.vnet.net>
- Reply-to: "Peter Pein" <peter1963 at totalise.co.uk>
- Sender: owner-wri-mathgroup at wolfram.com
"Jake Rodriguez Rosales" <jrosales at physics.ubc.ca> schrieb im Newsbeitrag news:b80k2i$m3n$1 at smc.vnet.net... > 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? f[x_] := Module [ {c=Cos[x]}, If[c<0, (1 + Sin[x]/3)*c-1, If[c<0.6, c, $Failed] ] ] > 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 > > Blist={} While [Length[Blist]<100, x = Random[]; If[h[x] < g[x], AppendTo[Blist, x]] ] Blist Peter Pein