how to solve tan(x)-x=0.4267?
Show older comments
May I ask how to solve tan(x)-x=0.4267 in matlab?
Answers (2)
John D'Errico
on 23 Mar 2015
To be honest, I'd use fzero, not fsolve. But simple iteration is entirely sufficient.
x0 = atan(x0 + 0.4267)
Put a loop around it.
4 Comments
Titus Edelhofer
on 23 Mar 2015
And since the derivative is smaller than one in e.g. [0..2], it converges for any start point from [0..2]. Nice.
Titus
dpb
on 23 Mar 2015
DOH! Headslap! Disconnect between intent and typed, fzero was what I really was thinking of for function, but wrote fsolve instead.
But, the iteration is gooder...
John D'Errico
on 23 Mar 2015
Edited: John D'Errico
on 23 Mar 2015
A Titus points out, I had to choose a form for the iteration scheme that will converge.
Had I instead chosen to write it as
x = tan(x) - 0.4267
this would not converge so well or so stably for any set of starting values.
x = -5:5;
xprev = inf(size(x));
iter = 0;
while any(abs(x - xprev) > 1.e-12)
iter = iter + 1;
xprev = x;
x = atan(x + 0.4267);
end
iter
iter =
32
x
x =
0.93864 0.93864 0.93864 0.93864 0.93864 0.93864 0.93864 0.93864 0.93864 0.93864 0.93864
So that worked pretty well, converging for essentially any value of x that I chose, and to 12 digits, after only 32 iterations. Some of those converged more rapidly, but I was being lazy when I wrote it.
x = -5:5;
xprev = inf(size(x));
iter = 0;
for iter = 1:100
iter = iter + 1;
xprev = x;
x = tan(x) - 0.4267;
end
iter =
101
x =
-2.6874 -2.0619 -0.79099 -3.0194 -0.8124 -0.10595 -1.1468 -1.4749 -2.2183 -0.71188 1.5208
So, after 100 iterations with the "wrong" form, essentially none of the start points have converged. Even after 1000 iterations of the latter loop, none of them converged.
The trick is to choose a form that will robustly converge. In general, fzero is usually simpler. Let the code do the thinking for you.
But, if you want more precision, better yer is just...
syms x
vpasolve(tan(x)-x==0.4267,1)
ans =
0.93864428424672204276667386408592
Titus Edelhofer
on 24 Mar 2015
Yes, you chose the right form. In fact, since the derivative of your iteration function is 1/((x+0.4267)^2 +1 ) is always between zero and one, your iteration will converge for any starting point.
dpb
on 23 Mar 2015
Start with
doc fsolve
3 Comments
SUHAS GUPTA THUNUGUNTLA
on 23 Mar 2015
Jan de Wilde
on 23 Mar 2015
Edited: Jan de Wilde
on 23 Mar 2015
rewrite the formula to tan(x)-x-0.4267 = 0 and solve with: fzero('tan(x)-x-0.4267',1)
Titus Edelhofer
on 24 Mar 2015
Now that the full solution is there, one more suggestion is to use anonymous functions to call fzero:
fzero(@(x) tan(x)-x-0.4267, 1)
Titus
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!