Bisection method for transcendental equations. I:e tan(x)= x
17 views (last 30 days)
Show older comments
clear all
clc
f = @(x) tan(x) - x;
x1 = p;
x2 = (3*pi)/2;
if f(x1)*f(x2) < 0
fprintf('No routes');
end
if f(x1) == 0;
fprintf('pi/2 is one of the roots')
return
elseif f(x2) == 0;
fprintf('3*pi/2 is one of the roots')
return
end
while abs(f(x1)) < 0.0001
xmid = (x1+x2)/2; %bisection
if f(x1)*f(xmid) < 0
x2 = xmid;
else
x1 = xmid;
end
end
fprintf('The root is: %f%d\n',x1)
syms y
s = vpasolve(tan(y) == y,y,[pi/2,(3*pi)/2])
I feel like my loop is incorrect and the output I am getting is incorrect, Ive looked at other bisection method examples here on mathworks but I can get it to work for my specific case.
0 Comments
Answers (1)
Alan Stevens
on 9 Aug 2020
If I were you I would change the "while" condition to
while abs(x1-x2) > 0.0001
1 Comment
Alan Stevens
on 9 Aug 2020
Also, where you have
if f(x1)*f(x2) < 0
fprintf('No routes');
end
you should have
if f(x1)*f(x2) > 0
fprintf('No roots');
end
(The important part being the > sign!)
See Also
Categories
Find more on Loops and Conditional Statements 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!