Bisection method (figuring out theta)

2 次查看(过去 30 天)
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??

回答(1 个)

Geoff Hayes
Geoff Hayes 2020-3-17
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?

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by