Bisection method (figuring out theta)

3 views (last 30 days)
Ahmed Alhawaj
Ahmed Alhawaj on 15 Mar 2020
Answered: Geoff Hayes on 17 Mar 2020
clc; clear;
%% Initialize
w = 30; h = 30; c = 3.5; % In inches
%% Bisection
a= 49.04; b = 50.5;
f = @(theta) w*sind(theta) - h*cosd(theta) - c;
err = 0.477; ii = 0;
if f(a)*f(b)>0
disp('Change initial values')
elseif abs(f(a))<ferr
display('Your initial guess is correct')
elseif abs(f(b))<ferr
display('Your second guess is correct')
xmid= (a + b)/2;
err_bisect = abs(f(xmid));
while err_bisect > err
if (f(a))*f(xmid)<0
b = xmid;
a = xmid;
xmid = (a + b)/2;
err_bisect =abs(f(xmid));
ii = ii + 1;
fprintf("After %d iterations, final value using Bisection method is %f and error is %e \n", ii, xmid, err_bisect)
how do i get the code to calculate the iteration needed to come up with the value of theta??

Answers (1)

Geoff Hayes
Geoff Hayes on 17 Mar 2020
Ahmed - theta is your xmid and I think that you are calculating it correctly (see for details). One issue may be with your tolerance err
err = 0.477;
Why is this such a large value? If I leave your code as is, then the condition for
while err_bisect > err
is never true so the bisection method doesn't do anything. Try reducing err to somethig much smaller (perhaps 0000477) and then the code will iterate 12 times and give you an answer of 49.732039 which f(49.732039) ~= 0. Same with
Why such a large number? Shouldn't this be close to zero too?


Community Treasure Hunt

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

Start Hunting!