# Solve piecewise function

I have two piecewise function

equ1 = Piecewise[{{0.524324 + 0.0376478x, 0.639464 <= x <= 0.839322}}]
equ2 = Piecewise[{{-0.506432 + 1.48068x, 0.658914 <= x <= 0.77085}}]


Now, I am trying to solve equ1 = equ2.

Firstly I tried FindRoot:

FindRoot[equ1 == equ2, x]


But the output is x = 0. I can only get the correct answer by set search starting point 0.7. How can I direct get the answer without set starting point?

Secondly, I tried code Reduce:

Reduce[equ1 == equ2, x]


However, the error appear. The good news is Reduce do provide the correct answer for my equation. The error is:

Reduce::ratnz: Reduce was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.


Do I have other way to solve those two piecewise function?

• What's wrong with Solve[0.524324 + 0.0376478 x == -0.506432 + 1.48068 x, x]? or even FindRoot[equ1 == equ2, {x, .7}]? Oct 28, 2018 at 22:46
• Try to plot then you see where is root.{Plot[equ1 - equ2, {x, 1/2, 1}], FindRoot[equ1 == equ2, {x, 7/10}]}. Oct 28, 2018 at 22:48
• Because at later stages, I need to compara 100 equations to find out solution. It is not possible to set the starting value for each one of them. Oct 28, 2018 at 23:27
• Your Reduce command works for me. The message is just a warning, not an error. Oct 29, 2018 at 0:33

Either:

FindRoot[equ1 == equ2, {x, 0.7, 0.639464, 0.839322}]
(*  {x -> 0.714299}  *)


Or:

NSolve[equ1 == equ2 && 0.639464 <= x <= 0.839322, x]


NSolve::ratnz:.... [Unimportant warning.]

(*  {{x -> 0.714299}}  *)


Update: Here's a programmatic way to get the interval of support and a starting point for FindRoot, assuming that by a comment, the OP means that they have to do this a hundred times:

equAll = {equ1, equ2};
{funAll, domAll} = Transpose@ equAll[[All, 1, 1]];
support = Simplify[And @@ domAll];
endpoints = First@ RegionBounds@ ImplicitRegion[support, x];
midpoint = Mean@ endpoints;  (* can be used as a starting point *)


Alternatively, if you can define your piecewise functions to be undefined outside the interval of support, then Solve can be used directly:

pw1 = Piecewise[{{0.524324 + 0.0376478 x, 0.639464 <= x <= 0.839322}}, Undefined];
pw2 = Piecewise[{{-0.506432 + 1.48068 x, 0.658914 <= x <= 0.77085}}, Undefined];

Solve[pw1 == pw2, x]
(*  {{x -> 0.714299}}  *)

equ1 = Rationalize[
Piecewise[{{0.524324 + 0.0376478 x, 0.639464 <= x <= 0.839322}}]]
equ2 = Rationalize[
Piecewise[{{-0.506432 + 1.48068 x, 0.658914 <= x <= 0.77085}}]]
Reduce[equ1 == equ2, x]
x == 0.714299 || x < 79933/125000 || x > 419661/500000


If you have 100 equations and you want to find the unique solution to the whole set and each of your Piecewise are written in exactly the same form as the two you showed and the solution must lie in the intersection of all those inequalities then perhaps

equ1 = Piecewise[{{0.524324 + 0.0376478 x, 0.639464 <= x <= 0.839322}}];
equ2 = Piecewise[{{-0.506432 + 1.48068 x, 0.658914 <= x <= 0.77085}}];
funs = {equ1, equ2};
Join[{Equal @@ Map[#[[1, 1, 1]] &, funs]},
{Max[Map[#[[1, 1, 2, 1]] &, funs]] < x < Min[Map[#[[1, 1, 2, 3]] &, funs]]}]


will give you

{0.524324 + 0.0376478 x == -0.506432 + 1.48068 x, 0.658914 < x < 0.77085}


and

x /. Solve[
Join[{Equal @@ Map[#[[1, 1, 1]] &, funs]},
{Max[Map[#[[1, 1, 2, 1]] &, funs]] < x < Min[Map[#[[1, 1, 2, 3]] &, funs]]}]
, x][]


will give you

0.714299


You can make the list of funs longer and longer and still get the solution.

The Max part of that is extracting all your lower bounds and finding the largest one. The Min part of that is extracting all your upper bounds and finding the smallest one. The Equal part of that is extracting all your equations and setting them equal to each other. We assemble those pieces and give them to Solve.

You should check carefully to make certain that there will always be one unique solution to your set of equations.

You might click on Piecewise and then Details in the help pages and read very carefully all the information there. Because you have not given any other information about the value of your functions if x happens to be outside the bounds you have given then the value of that Piecewise function will be zero for x outside those bounds. That might surprise you.