Bisection method for transcendental equations. I:e tan(x)= x
Afficher commentaires plus anciens
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.
Réponses (1)
Alan Stevens
le 9 Août 2020
If I were you I would change the "while" condition to
while abs(x1-x2) > 0.0001
1 commentaire
Alan Stevens
le 9 Août 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!)
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!