# Find simultaneous roots of Sin[x] and Cos[x] using FindRoot, If, and For

I am a beginner of Mathematica, I happened to come across the following exercise which may seem trivial to the experts of this field but is hard for me to do at this level. I have the equation Sin[x]=1/Sqrt where x lies between 0 and 10Pi, I want to find the the roots of this equation which also satisfy Cos[x]=1/Sqrt. I am allowed to use FindRoot, If statement and For loop for the problem. I apologize for the mistake in the original question which I edited by writing 1/Sqrt in place of Zero.

Can I proceed like

For i in the range 0 to 10Pi in increments of 0.1, FindRoot[Sin[x]=Cos[x],{x,i}] and to check whether this is an actual root, I put it back in equation Sin[x]=Cos[x] using If statement and ask it to print the root for which Sin[x]=Cos[x]?

As I said, I am just learning Mathematica, so please bear with me if you find this improper.

• I would appreciate other ways of solving this problem, if any, with or without the use of 'If', 'For' and 'FindRoot' commands. – Jee Mar 7 '17 at 18:17
• You may have to look a long time to find a real number x where both Sin[x] and Cos[x] are zero. Why not plot the two functions and see... – bill s Mar 7 '17 at 18:17
• Seems a little silly, but Solve[Sin[x] == 0 && Cos[x] == 0 && 0 < x < 10 Pi, x]. The answer, of course, is the empty set {}. – Mark McClure Mar 7 '17 at 18:18
• plot the two functions using Plot[{ Sin[x], Cos[x] }, {x, 0, 10 Pi} ]. Do you see any points in which they are both zero? – glS Mar 7 '17 at 18:19
• Yes, you are right. The exercise might had some mistake in it. I can see some points other than zero, where these curves do intersect. Could you please tell give me some idea as to how to locate those points? – Jee Mar 7 '17 at 18:27

Here's a way to implement your idea:

dx = 10 Pi / 100;
sols = Table[
Quiet[
Check[
FindRoot[
Sin[x] == Cos[x],
{x, x0, x0, x0 + dx}],
Nothing,
FindRoot::reged],
FindRoot::reged],
{x0, 0, 10 Pi - dx, dx}
]


You can convert the Table to For if you absolutely need to. See also AppendTo for building a list of solutions inside For[]. Alternatively, you could build up the solutions with

sols = {sols, newsol};


inside the For loop; after the loop, use sols = List /@ Flatten[sols] to get it in standard multiple-solution form. I will say that doing this task with For is more like using Mathematica to learn C or Java, and not like learning Mathematica. See Why should I avoid the For loop in Mathematica? for more discussion of For.

I picked a dx that divides the interval evenly, instead of 0.1. I also used Check instead of If; see Check, Quiet, FindRoot::reged. If you really want If, you might do something like the following instead of the Quiet[..] statement:

res = FindRoot[
Sin[x] == Cos[x],
{x, x0, x0, x0 + dx}];
If[Sin[x] == Cos[x] /. res,
res,
Nothing]


But again, Quiet and Check are the Mathematica tools designed for this sort of approach.

Check:

Plot[{Sin[x], Cos[x]}, {x, 0, 10 Pi},
GridLines -> {x /. sols, None}] • Thank you very much Michael. It really worked for me. Just one query. I was going through Mathematica Documentation Center where I found FindRoot function with the form FindRoot[f, {x,xo}] which specifies the initial point for the variable x, while as in your answer you have put an additional argument " x0 + 10 Pi/100". Could you please explain what this extra term does?. – Jee Mar 8 '17 at 9:54
• @Jee There are three forms: {x, x0} which will use an unconstrained, damped, Newton's method (if the function is differentiable); {x, x0, x1} which will search without derivatives (e.g. secant method); and {x, x0, x1, x2} which still start at x0, constrain x to be within x1 <= x <= x2, and give the reged message if it tries to go outside the interval. My code constrains each search to be over the interval from x0 to x0 + dx (I forgot to change the last x0 + 10 Pi/100 to x0 + dx. (Using dx makes it easier to adjust the width of the intervals used; it can be made larger.) – Michael E2 Mar 8 '17 at 12:02

You can see that there are no solutions to Sin[x]=Cos[x]=0 by plotting:

Plot[{Sin[x], Cos[x]}, {x, 0, 10}] You can find the point (near 1) where Sin[x]=Cos[x] using:

FindRoot[Sin[x] == Cos[x], {x, 1}]
{x -> 0.785398}

• Thank you very much. Just one question. If I am not sure where the root lies, I have often found that FindRoot gives solutions (depending on initial point) which are not satisfying LHS=RHS. In that case, I would like to substitute the root obtained from FindRoot back into equation LHS=RHS and check for different initial values. Could you please help me to understand how to do this check for the actual root. – Jee Mar 7 '17 at 18:54

Since your comment indicated that you are also interested in solutions other than using FindRoot

sol = Solve[{Sin[x] == Cos[x], 0 <= x <= 10 Pi}, x] // FullSimplify //
SortBy[#, N] &

(*  {{x -> π/4}, {x -> (5 π)/4}, {x -> (9 π)/4}, {x -> (
13 π)/4}, {x -> (17 π)/4}, {x -> (21 π)/4}, {x -> (
25 π)/4}, {x -> (29 π)/4}, {x -> (33 π)/4}, {x -> (
37 π)/4}}  *)


Verifying

And @@ (Sin[x] == Cos[x] /. sol)

(*  True  *)


The approximate numeric values are

sol // N

(*  {{x -> 0.785398}, {x -> 3.92699}, {x -> 7.06858}, {x ->
10.2102}, {x -> 13.3518}, {x -> 16.4934}, {x -> 19.635}, {x ->
22.7765}, {x -> 25.9181}, {x -> 29.0597}}  *)


You could also use NSolve or Reduce