Bisection method (figuring out theta)

1 Ansicht (letzte 30 Tage)
Ahmed Alhawaj
Ahmed Alhawaj am 15 Mär. 2020
Beantwortet: Geoff Hayes am 17 Mär. 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;
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??

Antworten (1)

Geoff Hayes
Geoff Hayes am 17 Mär. 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?

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by