Re: One thing in two ways, with different result.
- To: mathgroup at smc.vnet.net
- Subject: [mg90118] Re: One thing in two ways, with different result.
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 29 Jun 2008 05:37:18 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g451vv$pqg$1@smc.vnet.net>
damayi wrote:
> Today I encountered a confused question, and I hope you can help me.
> I defined a function g[x] in the following and Plot it.
> g[x_] := x^3 /; x <= 0
> g[x_] := x /; 0 < x <= 1
> g[x_] := Sin[x] /; x > 1
>
> Then I defined another function f[x] that is the same as g[x] in my
> opinion, and Plot it.
> f[x_] := Piecewise[{{x^3, x <= 0}, {x, 0 < x <= 1}, {Sin[x], x > 1}}]
>
> You will find that Plot[g[x],{x,-2,3}] is different with Plot[f[x],
> {x,-2,3] when x is 1.0
>
> Since g[x] and f[x] are identify, why are these plot different ?
> By the way, my Mathematica is 6.0.2.
Depending on how you define your functions, Mathematica may or may not
look for and find discontinuities points. Defining a peacewise function
thanks to *Peacewise[]* is a good hint to Mathematica that it should
spend some extra time looking for discontinuities. So in the case of g
you can tell Plot to exclude the discontinuous point at one and get the
exact same plot as for f.
g[x_] := x^3 /; x <= 0
g[x_] := x /; 0 < x <= 1
g[x_] := Sin[x] /; x > 1
f[x_] := Piecewise[{{x^3, x <= 0}, {x, 0 < x <= 1}, {Sin[x], x > 1}}]
Plot[g[x], {x, -2, 3}, Exclusions -> 1]
Plot[f[x], {x, -2, 3}]
Regards,
-- Jean-Marc