bisection method, iteration does not stop although hand calculation were diffrent

5 views (last 30 days)
while running this scrept it keeps loopin on the same iteration for some reason
while doing the bisection by hand error reached below zero after 5 iterations
% Setting x as symbolic variable
syms q w e r t y u i o p l k j h g f d s a z x c v b n m ;
% Input Section
y = input('Enter the equation equations: ');
a = input('Enter A value of the interval: ');
b = input('Enter B value of the interval: ');
e = input('Entre the minimum tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
c = (a+b)/2;
fc = eval(subs(y,x,c));
while abs(fc)>e
if fa*fc< 0
b =c;
a =c;
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\nRoot is: %f\n', c);
my inputs are
>> bisection2
Enter the equation equations: (sqrt(19.62.*x))*tanh((sqrt(x*19.62)/8)*2.5)
Enter A value of the interval: 0
Enter B value of the interval: 2
Entre the minimum tolerable error: 0.00005
the thing afterwards goes on forever unless i stop the script
this the function i am trying to the find the root of
Torsten on 2 Dec 2022
Edited: Torsten on 2 Dec 2022
Then you must have used a different function in your hand calculation.
f(0), e.g. is obviously 0 for the function given, but in your list, f(0) = -5.
Maybe your function is
y = @(x)(sqrt(19.62.*x)).*tanh((sqrt(x*19.62)/8)*2.5) - 5;

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 2 Dec 2022
Let's look at your function.
syms x
f = sqrt(19.62*x);
fun = f*tanh(2.5*f/8);
It looks like you have a zero crossing at x = 0 but you never check for that in your code. You only check that the value of the function at the midpoint of your interval is 0. Let's check numerically what we observed graphically.
value = subs(fun, x, 0)
value = 
By the way, stop using eval on symbolic expressions. If you need to convert the result of the subs call into a double precision value, call double on it.
d = double(value)
d = 0




Community Treasure Hunt

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

Start Hunting!