Find all roots to an equation?

Hello. I’m spending my friday night trying to learn Mathematica, and so far it’s been going decent. This is only my second day using the program, so bare with me.

I’m currently on an exercise where I’m supposed to plot these two functions in the same graph:

cef38e864a03b6a34c3162d6ee7b7c92.png

For that I used

Plot[{Abs[3 - t^2] + Abs[t - 1] - t^2, 3*Sin[t]}, {t, -3.8, 4.6}]

Which gave me this
:

Now, what I’m wondering is, how do I find all of the roots to my equation ( h(t) = g(t) )? My e-book suggests using FindRoot, but I don’t know how/where. Thanks for your help.

FindRoot[Abs[3 - t^2] + Abs[t - 1] - t^2 == 3Sin[t], {t, #}] & /@ {-3, 1, 3}( Starting values from Plot)
(
{{t → -3.77452}, {t → 0.762901}, {t → 3.35745}} *)

Or:

NSolve[{RealAbs[3 - t^2] + RealAbs[t - 1] - t^2 == 3*Sin[t], -4 < t < 4}, t, Reals](*Why Abs[] dosen't work I don't know !.*)
(* {{t -> -3.77452}, {t -> 0.762901}, {t -> 3.35745}} *)
NSolve[{Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], -4 < t < 4}, t, Complexes]
(* {{t -> -3.77452}, {t -> 0.762901}, {t -> 3.35745}} *)

Solve[{Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], -4 < t < 4}, t, Reals]
(*{{t -> Root[{-4 - 3 Sin[#1] + #1 &, 3.3574487606850113852}]}, {t -> 
   Root[{2 + 3 Sin[#1] + #1 &, -3.7745180124835511931}]}, {t -> 
   Root[{-4 + 3 Sin[#1] + #1 + 2 #1^2 &, 0.76290087955924483126}]}}*)
%//N
(* {{t -> 3.35745}, {t -> -3.77452}, {t -> 0.762901}} *)

Reduce[{Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], -4 < t < 4}, t, Reals]
(* t == Root[{-4 - 3 Sin[#1] + #1 &, 3.3574487606850113852}] || 
 t == Root[{2 + 3 Sin[#1] + #1 &, -3.7745180124835511931}] || 
 t == Root[{-4 + 3 Sin[#1] + #1 + 2 #1^2 &, 0.76290087955924483126}] *)
 %//N
 (* t == 3.35745 || t == -3.77452 || t == 0.762901*)

 FindInstance[{Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], -4 < t < 4}, t, Reals, 3] // N
  (* {{t -> 3.35745}, {t -> -3.77452}, {t -> 0.762901}} *)

Ah okay. The first one is definitely the one I’ll be using then. You used -3,1,3 for the interval, why was that?

Read first:

?FindRoot

Use it, that will be easier for you:

{FindRoot[Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], {t, -3}],
FindRoot[Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], {t, 1}],
FindRoot[Abs[3 - t^2] + Abs[t - 1] - t^2 == 3*Sin[t], {t, 3}]}

This link with code by users J.M. and Thales Fernandes should be extremely useful in finding all roots: FindAllCrossings and FindRoots

There is a wonderful package written by @Ted Ersek from the Naval Air Warfare Center. It works really well for getting all roots numerically over a given range. The package is in the Wolfram Demo Library and its called RootSearch. We have used it successfully in several applications. In fact, it would be nice if the functionality were to eventually make it into MMA.

Regards,

Neil

There’s an undocumented function System`TRootsDump`GuessRealRoots that finds pretty good initial points for FindRoot to polish up:

Reduce`AnalyticRootIsolation; (* initialization *)
With[{f = Function[t, -t^2 + Abs[-1 + t] + Abs[3 - t^2] - 3 Sin[t]]},
 FindRoot[f, {#}] & /@ System`TRootsDump`GuessRealRoots[f, {-4, 4}]
 ]
(*  {{0.762901}, {3.35745}, {-3.77452}}  *)

There’s an undocumented function System`TRootsDump`GuessRealRoots that finds pretty good initial points for FindRoot to polish up:

Reduce`AnalyticRootIsolation; (* initialization *)
With[{f = Function[t, -t^2 + Abs[-1 + t] + Abs[3 - t^2] - 3 Sin[t]]},
 FindRoot[f, {#}] & /@ System`TRootsDump`GuessRealRoots[f, {-4, 4}]
 ]
(*  {{0.762901}, {3.35745}, {-3.77452}}  *)

Here’s another way to get the starting points:

rootapprox = Plot[(Abs[3 - t^2] + Abs[t - 1] - t^2) - (3*Sin[t]), {t, -3.8, 4.6},
   MeshFunctions -> {#2 &}, Mesh -> {{0}}, MeshStyle -> {Red}, PlotStyle -> None];
Cases[Normal@rootapprox, Point[{x_, _}] :> x, Infinity]
(*  {0.761897, 3.35785, -3.77457}  *)

In my problem, SystemTRootsDumpGuessRealRoots works 10 to 300 times slower than Ted Ersek’s RootSearch.