This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: while err > tol % calculate guess for root xr = (xu+xl)/2 % calculate approximate percent error if not first iteration if i ~= 1 err = abs((xr-xrold)/xr)*100 end % determine which point to replace - xu or xl % pick point with same function sign as xnew fxr = EvaluateFunction(xr) fxu = EvaluateFunction(xu) fxl = EvaluateFunction(xl) if fxr*fxl < 0 % fxr and fxu have the same sign xu = xr else xl = xr end Sheet1 Page 2 % save current xr value as old xr value xrold = xr % increment counter i = i+1 end % 4. Verify that the final error is less than tol fprintf('Final error is %6.4g and tol is %6.4g\n', err, tol) function fx = EvaluateFunction(x) global g cd t v % x is mass m fx = sqrt(g*x/cd).*tanh(sqrt(g*cd./x)*t)-v...
View Full Document
- Spring '09
- Global variable, Referential transparency