Bisection method (figuring out theta)

2 ビュー (過去 30 日間)
Ahmed Alhawaj
Ahmed Alhawaj 2020 年 3 月 15 日
回答済み: Geoff Hayes 2020 年 3 月 17 日
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 Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by