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