Clear Filters
Clear Filters

Bisection script running endlessly

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 7 Sep 2015
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.

More Answers (0)

Categories

Find more on MATLAB 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!