Re: couple of small problems
- To: mathgroup at smc.vnet.net
- Subject: [mg40872] Re: couple of small problems
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Tue, 22 Apr 2003 06:49:03 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <b80k2i$m3n$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
a)
fMain[x_] /; 0 < Cos[x] < 0.6 := Cos[x]
fMain[x_] /; Cos[x] < 0 := (1 + Sin[x]/3)*Cos[x]
b) some thing like
Block[{i, tmp},
i = 0;
Blist = Table[0, {100}];
While[i < 100,
tmp = Random[];
If[tmp < 0.5, Blist[[++i]] = tmp];
];
Blist
]
Regards
Jens
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