bisection - while err > tol % calculate guess...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 function [xr,err] = bisection % define global variables for function whose root is sought global g cd t v % 1. Plot the function to guess where the root is located cd = 0.25 g = 9.81 v = 36 t = 4 m = linspace(50,200)' err = sqrt(g*m/cd).*tanh(sqrt(g*cd./m)*t)-v plot(m,err) grid xlabel('mass') ylabel('error') pause % 2. Based on the graph, enter upper and lower values of mass m % that bracket the zero crossing xu = input('Enter value of xupper: ') % use 200 xl = input('Enter value of xlower: ') % use 100 % 3. Use bisection bracketing rootfinding method to find the root xr % where the function crosses zero % Note that the bisection iterative equation is simply xr = (xu+xl)/2 tol = input('Enter convergence tolerance tol: ') % use 1e-4 err = 2*tol % start with err > tol i = 1 % initialize counter
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the 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

Page1 / 2

bisection - while err > tol % calculate guess...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online