Bisection script running endlessly

조회 수: 6 (최근 30일)
Abdulla Al-Mohannadi
Abdulla Al-Mohannadi 2015년 9월 7일
답변: Walter Roberson 2015년 9월 7일
I'm trying to develop a script to obtain the root of a function using the bisection method. This is what I have:
function [ TC ] = bisect(xl, xu, es, osf)
format long
xl = xl + 273.15;
xu = xu + 273.15
xr = (xl + xu)/2;
ea = inf;
ii = 1;
while ea > es;
xr_vec = zeros(1,ii)
if fTa(xl, osf)*fTa(xr, osf) < 0
xu = xr
ii = (ii-1) + 1;
xr = (xl + xu)/2;
xr_vec(ii) = xr;
if ii > 1;
xr = (xl + xu)/2;
xr_vec(ii) = xr;
ea = (xr_vec(ii) - xr_vec(ii-1))/(xr_vec(ii))*100;
end
else xl = xr
ii = (ii-1)+1;
xr = (xl + xu)/2;
xr_vec(ii) = xr;
if ii > 1;
xr = (xl + xu)/2;
xr_vec(ii) = xr;
ea = (xr_vec(ii) - xr_vec(ii-1))/(xr_vec(ii))*100;
end
end
end
TC = xr - 273.15;
end
function f = fTa(Ta, osf)
f = -139.34411 + (1.575701e5)/(Ta) - (6.642308e7)/(Ta^2) + (1.243800e10)/(Ta^3) - (8.621949e11)/(Ta^4) - log(osf);
end
Command window input: bisect(0, 0.4, 0.05, 8)
  • TC = temperature in Celsius
  • TA = temperature in Kelvin
  • osf = oxygen concentration (just another input variable)
  • xl = specified lower guess
  • xu = specified upper guess
  • xr = average of xl and xu
  • ea = the relative % approximate error
  • es = specified error boundary
I'm putting all of the xr values in a vector so that ea can be calculated easily. The fTa function at the bottom is being called in the bisect function.
Every time I run it, I keep getting an endless loop with recurring numbers, and I'm not sure where it's going wrong.
I appreciate any help, Thank you

채택된 답변

Walter Roberson
Walter Roberson 2015년 9월 7일
In two places you have
ii = (ii-1) + 1;
That leaves ii unchanged at 1, since (1-1)+1 is 1. Your code for ii > 1 will never be invoked.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by