Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Any body can help me solve the equation with piecewise function

428 views
Skip to first unread message

love...@gmail.com

unread,
Mar 31, 2008, 3:07:37 AM3/31/08
to
Hi,

I have a problem to solve, but it seems not easy because of the piecewise function involved. I am wondering how we should deal with piecewise function when solving an equation or doing optimization.

To give you an simple example, the piecewise function is f(x)= 3x if x<0; f(x)= 0 if x>=0. And I want to solve the equation f(x)+6=0.

I actually define the piecewise function at first using "Which" and then try to use "NSolve" to solve it. However, NSolve doesn't work through.

Anybody know how should I solve this kind of problem generally in Mathematica? Of course, for this simple example, we can divide it into two cases and then compare the solutions after solving the two cases. Yet my problem is more general and involves more than 3 piecewise functions, thus a lot of possible cases to deal with. So any command or function to deal with this kind of general problem?

Thanks in advance.

Bill Rowe

unread,
Apr 1, 2008, 4:20:57 AM4/1/08
to
On 3/31/08 at 2:04 AM, love...@gmail.com wrote:

>I have a problem to solve, but it seems not easy because of the
>piecewise function involved. I am wondering how we should deal with
>piecewise function when solving an equation or doing optimization.

>To give you an simple example, the piecewise function is f(x)= 3x if
>x<0; f(x)= 0 if x>=0. And I want to solve the equation f(x)+6=0.

>I actually define the piecewise function at first using "Which" and
>then try to use "NSolve" to solve it. However, NSolve doesn't work
>through.

>Anybody know how should I solve this kind of problem generally in
>Mathematica?

How about using Reduce? For example,

In[1]:= f[x_] = Piecewise[{{3 x, x < 0}, {0, x >= 0}}];

In[2]:= Reduce[f[x] + 6 == 0, x]

Out[2]= x == -2

Jean-Marc Gulliet

unread,
Apr 2, 2008, 4:03:29 AM4/2/08
to
love...@gmail.com wrote:

First, note that there exists several "obvious" ways to define a
piecewise function in Mathematica such as *Piecewise*, *Which*,
*Switch*, *If*, etc., and some less obvious ways such as *Boole*,
*UnitStep*, *Clip*, *InterpolatingFunction* to name a few. Which one to
choose depends on the version of Mathematica you are using, the
familiarity you have with them, what you try to achieve, and also
evaluation speed. Therefore, it is hard to tell what in general one
should use without taking in account the problem to be solved and its
context.

Second, note that some solvers are designed to deal specifically with
piecewise and piecewise-related function (*Reduce*, *Simplify*,
*Minimize*, to name a few) whereas some others are not (*Solve* for
instance).

Third, note that there exists two classes of solvers --- symbolic
(Solve, Reduce, DSolve, Minimize. ...) and numeric (NSolve, NSolve,
NMinimize, ...) ---, each type using different algorithms and yielding
exact or approximate solutions, respectively.

Finally, here is an example using the function you gave in your post
(the output from Mathematica have been commented out (**)).

Clear[f]


f[x_] := Piecewise[{{3 x, x < 0}, {0, x >= 0}}]

Reduce[f[x] + 6 == 0, x] (* x == -2 *)

f[x] + 6 == 0 /. ToRules@% (* True *)

Solve[f[x] + 6 == 0, x]

(*

During evaluation of In[4]:= Solve::eqf: x<0 is not a well-formed
equation. >>

Solve[6 + \[Piecewise] {
{3 x, x < 0}
} == 0, x]

*)

Simplify[f[x] + 6 == 0] (* 2 + x == 0 *)

Solve[%, x] (* {{x -> -2}} *)

Plot[f[x] + 6, {x, -10, 10}] (* ... discarded plot ... *)


Regards,
--
Jean-Marc

--
Jean-Marc

0 new messages