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

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
end
% 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.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end
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

3 Comments

The root is at the left boundary of the search interval - not easy to reach.
% Input Section
y = @(x)(sqrt(19.62.*x)).*tanh((sqrt(x*19.62)/8)*2.5);%input('Enter the equation equations: ');
a = 0;%input('Enter A value of the interval: ');
b = 2;%input('Enter B value of the interval: ');
e = 0.00005;%input('Entre the minimum tolerable error: ');
% Finding Functional Value
fa = y(a);
fb = y(b);
itermax = 50;
iter = 0;
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = y(c);
%fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e && iter < itermax
iter = iter + 1;
%fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc<= 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = y(c);
end
%fprintf('\nRoot is: %f\n', c);
iter
c
end
iter = 17
c = 7.6294e-06
thanks for the answer but the values are way off from what i have calculated by hand do you have an idea on why is that the case. figure attached is the desired values
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)

Let's look at your function.
syms x
f = sqrt(19.62*x);
fun = f*tanh(2.5*f/8);
fplot(fun)
yline(0)
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 = 
0
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

Categories

Products

Release

R2022b

Asked:

on 2 Dec 2022

Edited:

on 2 Dec 2022

Community Treasure Hunt

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

Start Hunting!