Bisection method (figuring out theta)
2 views (last 30 days)
Show older comments
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;
ferr=0.176;
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')
else
xmid= (a + b)/2;
err_bisect = abs(f(xmid));
while err_bisect > err
if (f(a))*f(xmid)<0
b = xmid;
else
a = xmid;
end
xmid = (a + b)/2;
err_bisect =abs(f(xmid));
ii = ii + 1;
end
end
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??
0 Comments
Answers (1)
Geoff Hayes
on 17 Mar 2020
Ahmed - theta is your xmid and I think that you are calculating it correctly (see https://en.wikipedia.org/wiki/Bisection_method 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
ferr=0.176;
Why such a large number? Shouldn't this be close to zero too?
0 Comments
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!