Maple - Find all intersecting points

960 Views Asked by At

If I want to find all points of intersection, and not just one point, between the following two functions: $ sin(x)^{2}$ and $e^{-x}cos(x) $

$ eqn := sin(x)^2 = e^{-x}cos(x); $

$ fsolve(eqn, x); $

The output in Maple gives me one point of intersection, while I would like to see ALL points of intersection between the two functions.

2

There are 2 best solutions below

0
On

Iterate fsolve on intervals of the form $[k\pi,(k+1)\pi]$, with $k\in\mathbb{Z}$, as follows:

f := proc (x) options operator, arrow; sin(x)^2 end proc
g := proc (x) options operator, arrow; exp(-x)*cos(x) end proc
eq := f(x) = g(x);
for k from -5 to 5 do fsolve(eq, x = k*Pi .. (k+1)*Pi) end do

output (depending on $k$):

...
-14.13716622
-10.99559106
-7.853593280
-4.721292059
-1.317039836
0.6791427621
6.239013543
6.325488468
12.56450143
12.56823632
18.84947522
...

Note that the above results are NOT proof that these are the $\mathbf{only}$ roots in the indicated intervals. The latter is an entirely different question.

0
On
restart:

eqn := sin(x)^2 = exp(-x)*cos(x):

ans1 := Student:-Calculus1:-Roots(eqn, x=-20..20, numeric);

    [-17.27875963, -14.13716622, -10.99559106, -7.853593280, -4.721292059, 

     -1.317039836, 0.6791427621, 6.239013543, 6.325488468, 12.56450143, 

     12.56823632, 18.84947522, 18.84963662]

nops( ans1 );

                                 13

findroots:=proc(expr,a,b,{guard::posint:=5,maxtries::posint:=50})
local F,x,sols,i,res,start,t;
   x:=indets(expr,name) minus {constants};
   if nops(x)>1 then error "too many indeterminates"; end if;
   F:=subs(__F=unapply(expr,x[1]),__G=guard,proc(t)
      Digits:=Digits+__G;
      __F(t);
   end proc);
   sols,i,start:=table([]),0,a;
   to maxtries do
      i:=i+1;
      res:=RootFinding:-NextZero(F,start,
                                 'maxdistance'=b-start);
      if type(res,numeric) then
         sols[i]:=fnormal(res);
         if sols[i]=sols[i-1] then
            start:=sols[i]+1.0*10^(-Digits);
            i:=i-1;
         else
            start:=sols[i];
         end if;
      else
         break;
      end if;
   end do;
   op({entries(sols,'nolist')});
end proc:

ans2 := [ findroots( (rhs-lhs)(eqn), -20, 20 ) ];

   [-17.27875963, -14.13716622, -10.99559107, -7.853593281, -4.721292060, 

    -1.317039836, 0.6791427620, 6.239013542, 6.325488467, 12.56450142, 

    12.56823631, 18.84947521, 18.84963661]

nops( ans2 );

                                 13