MathGroup Archive 2003

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

Search the Archive

Re: couple of small problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40880] Re: couple of small problems
  • From: Oliver Ruebenkoenig <ruebenko at imtek.uni-freiburg.de>
  • Date: Wed, 23 Apr 2003 01:15:47 -0400 (EDT)
  • Organization: Rechenzentrum der Universitaet Freiburg, Germany
  • References: <b80k2i$m3n$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Jake,

On Mon, 21 Apr 2003, Jake Rodriguez Rosales wrote:

> 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_ ] /; 0 <  Cos [ x ] < 0.6 := Cos[ x ] ;
f[ x_ ] /; -1 <  Cos[ x ] < 0 := ( 1 + Sin[ x ] / 3 ) * Cos[ x ];
f[ x_ ] := 0; (* else *)

Plot[ f[x], {x, -5, 5} ]

See the Help Browser for Condition

>
> 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 = {};

h[ x_ ] := Cos[ x ];
g[ x_ ] := Sin[ x ];

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

hope that helps,
oliver

Oliver Ruebenkoenig, <ruebenko at imtek.de>
   Phone: ++49 +761 203 7293



  • Prev by Date: Re: small program in Mathematica
  • Next by Date: Re: RE: Solve[] handles the same system differently
  • Previous by thread: Re: couple of small problems
  • Next by thread: New Mathematica book with new codes