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:
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}]}
jbaldwin
(Jim Baldwin)
November 3, 2018, 1:03am
5
This link with code by users J.M. and Thales Fernandes should be extremely useful in finding all roots: FindAllCrossings and FindRoots
neils
(Neil Singer)
November 6, 2018, 2:27am
6
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
mroge02
(Michael Rogers)
November 9, 2018, 7:47pm
7
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}} *)
mroge02
(Michael Rogers)
November 9, 2018, 7:55pm
8
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} *)
kia999
(Igor Kotelnikov)
February 7, 2022, 1:39pm
9
In my problem, SystemTRootsDumpGuessRealRoots works 10 to 300 times slower than Ted Ersek’s RootSearch.