is there any mistake !!! method bisection to find root of equation

1 view (last 30 days)
function ys = fs(vp)
% pour les modes symetriques, pour plaque d'acier :
% vitesse longitudinale= 6144 m/s ,vitesse transversale =3095 m/s,
% module de Young E= 2e11Pa, coefficient de poisson 0.33,
% densité = 7850kg/m3,
f = 4000000;
d = 0.001;
w = 2*pi*f;
% d = 1.0000e-03; % epaisseur de la palaque 1 mm
vt = 3095;
vl = 6144;
k = w/vp;
kt = w/vt;
kl = w/vl;
s = sqrt ( k^2 - kt^2 );
q = sqrt ( k^2 - kl^2 );
ys = ((k^2 + s^2)^2)*cosh(q*d)*sinh(s*d)+ 4*(k^2)*q*s*sinh(q*d)*cosh(s*d) ;
function vp = bisection (f,a,b)
if f(a)*f(b) < 0
vp = (a+b)/2;
err = abs (f(vp));
while err > 1.0e-5
if f(a)*f(vp) <0
b = vp;
end
if f(a)*f(vp) >0
a = vp;
end
if f (vp) == 0
break
end
vp = (a+b)/2;
err = abs (f(vp));
end
else
fprintf ( ' pas de solution ');
end
  2 Comments
John D'Errico
John D'Errico on 6 Feb 2016
Edited: John D'Errico on 6 Feb 2016
There is no question here. What is your problem? Haave you tried to run it and it did not work? Or are we expected to completely test and debug your code, while figuring out what it is supposed to do?
Geoff Hayes
Geoff Hayes on 6 Feb 2016
salah - your implementation of the Bisection method seems reasonable though it isn't clear to me why the initial condition of
f(a)*f(b) < 0
is necessary. I can see you wanting to ensure that
b > a
so that the interval is correctly set but not the other. Also, when using while loops, it is good practice to implement a strategy to ensure that your code doesn't become stuck in an infinite loop. You do this by counting the number of iterations for the loop and once the maximum allowed has been reached, then you exit the loop. For example, your method could become
err = realmax;
iters = 0;
TOL = 1.0e-5;
MAXITERS = 1000;
while err > TOL && iters > MAXITERS
iters = iters + 1;
vp = (a+b)/2;
err = abs(f(vp));
if f(a)*f(vp) <0
b = vp;
elseif f(a)*f(vp) >0
a = vp;
elseif abs(f(vp)) < eps
break;
end
end
Note the use of the elseif in the above. Also, when using floating point precision, it a good idea to avoid comparing the float (or double) to zero as
f(vp) == 0
since this not likely to succeed (this is valid for integer comparisons but not when using doubles). Instead, we compare against some small number, eps, and assume that if this condition is met then the number is close enough to being zero.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!