# How do I find the intersection points of two piecewise functions? [closed]

I've got a piecewise function that is defined as follows:

f[a_, b_, c_, d_] :=  d + a /; a < b;
f[a_, b_, c_, d_] :=  d + b + (a - b)/10 /; a > b && b + (a - b)/10 < c;
f[a_, b_, c_, d_] :=  d + c /; b + (a - b)/10 >= c;


There is nothing fancy or complicated here. This function basically has three linear regions; the only difference between these regions is the slope of the line.

I want to solve for the intersection point of two different cases of this function, for example:

Solve[f[x, 6.55, 6.55, 5.32] == f[x, 5.5, 6.5, 5.52], x]


But I get this error:

Solve::inex: Solve was unable to solve the system with inexact coefficients
or the system obtained by direct rationalization of inexact numbers present
in the system. Since many of the methods used by Solve require exact input,
providing Solve with an exact version of the system may help. >>


I'm not sure what it means by "inexact coefficients" of "an exact version of the system".

I also tried NSolve, but this returns the original expression unchanged.

## closed as off-topic by Yves Klett, dr.blochwave, user9660, Kuba♦, ÖskåDec 14 '15 at 17:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, dr.blochwave, Community, Kuba, Öskå
If this question can be reworded to fit the rules in the help center, please edit the question.

• "inexact coefficients" refers to your use of numbers like 5.5; try replacing those with exact versions like 11/2. – J. M. is away Dec 14 '15 at 8:02

You can define your piecewise function a few different ways, here are two others,

f2[a_, b_, c_, d_] :=
Which[a < b, d + a, a > b && b + (a - b)/10 < c, d + b + (a - b)/10,
b + (a - b)/10 >= c, d + c];

f3[a_, b_, c_, d_] :=
Piecewise[{{d + a, a < b}, {d + b + (a - b)/10,
a > b && b + (a - b)/10 < c}, {d + c, b + (a - b)/10 >= c}}];


They all give the same plot and show that there should be 2 intersection points,

Plot[{#[x, 6.55, 6.55, 5.32], #[x, 5.5, 6.5, 5.52]}, {x, 0,
20}] & /@ {f, f2, f3}


Using the Which or Piecewise commands, you can use Solve or NSolve easily enough,

Solve[#[x, 6.55, 6.55, 5.32] == #[x, 5.5, 6.5, 5.52],
x] & /@ {f, f2, f3}
(* {Solve[f[x, 6.55, 6.55, 5.32] == f[x, 5.5, 6.5, 5.52],
x], {{x -> 5.72222}, {x -> 14.}}, {{x -> 5.72222}, {x -> 14.}}} *)

NSolve[#[x, 6.55, 6.55, 5.32] == #[x, 5.5, 6.5, 5.52],
x] & /@ {f, f2, f3}
(* {NSolve[f[x, 6.55, 6.55, 5.32] == f[x, 5.5, 6.5, 5.52],
x], {{x -> 5.72222}, {x -> 14.}}, {{x -> 5.72222}, {x -> 14.}}} *)


But FindRoot will only work if you choose a starting point that

FindRoot[#[x, 6.55, 6.55, 5.32] == #[x, 5.5, 6.5, 5.52], {x,
4}] & /@ {f, f2, f3}
FindRoot[#[x, 6.55, 6.55, 5.32] == #[x, 5.5, 6.5, 5.52], {x,
7}] & /@ {f, f2, f3}
FindRoot[#[x, 6.55, 6.55, 5.32] == #[x, 5.5, 6.5, 5.52], {x,
5.6}] & /@ {f, f2, f3}


During evaluation of In[16]:= FindRoot::jsing: Encountered a singular Jacobian at the point {x} = {4.}. Try perturbing the initial point(s).

During evaluation of In[16]:= FindRoot::jsing: Encountered a singular Jacobian at the point {x} = {4.}. Try perturbing the initial point(s).

During evaluation of In[16]:= FindRoot::jsing: Encountered a singular Jacobian at the point {x} = {4.}. Try perturbing the initial point(s).

During evaluation of In[16]:= General::stop: Further output of FindRoot::jsing will be suppressed during this calculation. >>

(* {{x -> 4.}, {x -> 4.}, {x -> 4.}} *)
(* {{x -> 14.}, {x -> 14.}, {x -> 14.}} *)
(* {{x -> 5.72222}, {x -> 5.72222}, {x -> 5.72222}} *)

f[x_, b_, c_, d_] :=
Piecewise[{{d + x, x < b}, {d + b + (x - b)/10,
x > b && b + (x - b)/10 < c}, {d + c, b + (x - b)/10 >= c}}]

x /. Solve[
Rationalize[f[x, 6.55, 6.55, 5.32] == f[x, 5.5, 6.5, 5.52]], x]


{103/18, 14}