[Date Index]
[Thread Index]
[Author Index]
RE: RE: RE: Define a function
*To*: mathgroup at smc.vnet.net
*Subject*: [mg34700] RE: [mg34650] RE: [mg34595] RE: [mg34556] Define a function
*From*: "DrBob" <majort at cox-internet.com>
*Date*: Sat, 1 Jun 2002 04:29:10 -0400 (EDT)
*Reply-to*: <drbob at bigfoot.com>
*Sender*: owner-wri-mathgroup at wolfram.com
>>the employed time to carry out the calculations is smaller.
Really? We should test that theory.
Here are timings for six equivalent functions:
f[x_] := x UnitStep[x - 10] - x/2UnitStep[x - 20]
g[x_] := Which[x < 10, 0, x < 20, x, True, x/2]
h[x_] := x (UnitStep[x - 10] - UnitStep[x - 20]) + x/2 UnitStep[x - 20]
i = #UnitStep[# - 10] - #/2UnitStep[# - 20];
j[x_] /; x < 10 := 0
j[x_] /; x < 20 := x
j[x_] := x/2
k := Which[# < 10, 0, # < 20, #, True, #/2] &
Timing[f /@ Range[10^5];]
Timing[g /@ Range[10^5];]
Timing[h /@ Range[10^5];]
Timing[i /@ Range[10^5];]
Timing[j /@ Range[10^5];]
Timing[k /@ Range[10^5];]
{2.047 Second, Null}
{1.25 Second, Null}
{2.219 Second, Null}
{1.86 Second, Null}
{1.328 Second, Null}
{1.063 Second, Null}
Bobby
-----Original Message-----
From: Juan Egea Garcia [mailto:jeg at um.es]
To: mathgroup at smc.vnet.net
Subject: [mg34700] [mg34650] RE: [mg34595] RE: [mg34556] Define a function
Yes, I think the same thing if what we want is to integrate the
function,
but if what we want is to carry out a great number of calculations that
involve (or not) complicated functions is better definig piecewise
functions
using bifurcation sentences because the employed time to carry out the
calculations is smaller.
============================
Juan Egea Garcia
Atica - Dpto. Matematica Aplicada
Universidad de Murcia - http://www.um.es
España
+34 968367144
-----Mensaje original-----
De: David Park [mailto:djmp at earthlink.net]
Enviado el: miércoles, 29 de mayo de 2002 8:45
Para: mathgroup at smc.vnet.net
Asunto: [mg34595] RE: [mg34556] Define a function
Michael,
The best method for defining piecewise functions is to use UnitStep.
f[x_] := x(UnitStep[x - 10] - UnitStep[x - 20]) + x/2UnitStep[x - 20]
This will not only plot...
Plot[f[x], {x, 0, 30}];
but you can also do things like integrate.
g[x_] = Integrate[f[x], x]
(-(1/4))*(-400 + x^2)*UnitStep[-20 + x] +
(-50 + x^2/2)*UnitStep[-10 + x]
Plot[g[x], {x, 0, 30}];
The Mathematica Book, which has not really been updated properly to
reflect
the UnitStep function often leads users to using conditional
definitions.
Clear[f]
f[x_] /; x < 10 := 0;
f[x_] /; 10 <= x <= 20 := x;
f[x_] /; 20 < x := x/2;
This works for plotting...
Plot[f[x], {x, 0, 30}];
but won't integrate or perform other calculus functions.
Integrate[f[x], x]
Integrate[f[x], x]
So, the UnitStep method is far superior.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> -----Original Message-----
> From: Michael Popp [mailto:popp.michael at gmx.at]
To: mathgroup at smc.vnet.net
> Sent: Monday, May 27, 2002 1:17 AM
> Subject: [mg34700] [mg34650] [mg34595] [mg34556] Define a function
>
>
> Hello
>
> I want to define a function in Mathematica. But just f[x_]:=... does
not
> work, because I have to define different functions for different
ranges of
> the variable x.
> For example:
>
> x < 10: f(x) = 0
> 10 < x < 20: f(x) = x
> 20 < x: f(x) = x/2
>
> How to do this?
>
> Thanks, greetings
> Michael
>
>
>
>
Prev by Date:
**RE: new user question about creating a new cell**
Next by Date:
**RE: Image Processing questions (Help!)**
Previous by thread:
**RE: new user question about creating a new cell**
Next by thread:
**RE: RE: RE: Define a function**
| |