Effacer les filtres
Effacer les filtres

How to solve trigonometric equation x*cos(x)+sin(x)=0 with function fsolve?

33 vues (au cours des 30 derniers jours)
I tried to solve it bit it says that function need more input arguments, and no matter what initial value I set, the result is always 0.

Réponse acceptée

Riccardo Scorretti
Riccardo Scorretti le 1 Avr 2022
Hi Attho,
since your function depends on one varialbe only, you can solve more easily with fzero:
fun = @(x) x.*cos(x)+sin(x);
x0 = fzero(fun, 1)
x0 = 1.0649e-22
The exact solution is x = 0, so it worked. By the way, you can use fsolve as well, but in this case it is not the best option (= too much complex for such a simple equation):
x0 = fsolve(fun, 1)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x0 = 1.1102e-16
In both cases, you must provide at least an initial trial (in this case x = 1, which I have chosen arbitrarily). Unfortunately, there is no guarentee that the method (indeed, any method) will converge if the initial trial is too far from a solution of the equation.

Plus de réponses (2)

Tala le 1 Avr 2022
Modifié(e) : Tala le 1 Avr 2022
trigonometric equations have many solutions. the intersection of blue and black lines are your answers.
f = @(x) x.*cos(x)+sin(x);
x = -10:0.1:10;
hold on

John D'Errico
John D'Errico le 1 Avr 2022
Modifié(e) : John D'Errico le 1 Avr 2022
This is just a variation of a problem I've seen given many times. It is one of the simplest equations I can think of that has no analytical solution. Think of it as due to the presence of x inside and out of the trig functions.
First, a simple re-write of the problem. We can divide by cos(x), because (n+1/2)*pi is never a solution, and that is where cos(x) is equal to zero. But now we have the problem
-x = tan(x)
First, wheredo the solutions lie? We can see that by plotting the two sides of that equation.
fplot(@(x) -x,[-20,20],'r');
hold on
legend('LHS: y = -x','RHS: y = tan(x)')
Every place where the red and blue lines cross is a solution. Do you see there will be infinitely many solutions? In fact, the solutions will be roughly separated by increments of pi. However, there is no analytical solution to be found, even in this form.
syms X
solve(-X == tan(X),'returnconditions',true)
Warning: Unable to find explicit solution. For options, see help.
ans = struct with fields:
X: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
Tools like fzero, or vpasolve can solve it numerically of course. (fzero is a better choice always than fsolve, because fzero is designed to handle single variable problems very well, whereas fsolve is targeted at multidimensional problems.)
So how can we find the n'th positive solution numerically? This is where fzero is perfect, because we can provide an interval for the solution. And we know an interval where the nth root will lie. That is, if we call the zeroth root, the simple solution at x==0, then the nth root must lie in the interval pi*[n-1/2,n+1/2].
tanfunroot = @(n) fzero(@(x) tan(x) + x,pi*[n - 1/2 + eps(n),n + 1/2 - eps(n)]);
Note the way I have carefully tweaked the ends of the interval, to insure that fzero will have no problems.
ans = 0
ans = 2.0288
ans = 4.9132
It will even generate the negative roots, though you might have seen that if X is a root of the problem, then -X is also a root.
ans = -2.0288
Are these solutions to the original problem? Of course they are.
X = tanfunroot(2);
X*cos(X) + sin(X)
ans = 0
Finally, to aid your intuition that successive roots will be separated by approximately pi, try this:
format long
tanfunroot(100) - tanfunroot(99)
ans =
Pretty close to pi. That is generally true, except for the roots nearest to zero. And the farther away from zero you go, the increment will becomes closer and closer to pi.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by