How to solve trigonometric equation x*cos(x)+sin(x)=0 with function fsolve?
조회 수: 25 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Riccardo Scorretti
2022년 4월 1일
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)
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)
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.
추가 답변 (2개)
John D'Errico
2022년 4월 1일
편집: John D'Errico
2022년 4월 1일
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
fplot(@tan,[-20,20],'b')
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)
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.
tanfunroot(0)
tanfunroot(1)
tanfunroot(2)
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.
tanfunroot(-1)
Are these solutions to the original problem? Of course they are.
X = tanfunroot(2);
X*cos(X) + sin(X)
Finally, to aid your intuition that successive roots will be separated by approximately pi, try this:
format long
tanfunroot(100) - tanfunroot(99)
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.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!