# 5 - Incremental Search(incsearch.m function brackets =...

This preview shows pages 1–3. Sign up to view the full content.

1/25/2010 1 function [ brackets ] = incsearch ( f, min, max, points ) % INCSEARCH: locates roots by incremental search % Inputs: f = a function of one variable (need not be able to deal with vectors) % min = lower bound of range to be searched % max = upper bound of range to be searched % points = number of search steps % O b k (i 1) l b d f i h b k Incremental Search (incsearch.m) % Outputs: brackets(i, 1) = lower bound for ith bracket % brackets(i, 2) = upper bound for ith bracket % **** if no brackets found, brackets = [] **** nb = 0; brackets = []; % brackets is initially 0 by 0 x = linspace (min, max, points); flo = f(x(1)); for i = 2: points fhi = f(x(i)); if sign(flo) ~= sign(fhi) nb = nb + 1; brackets(nb, 1) = x(i 1); brackets(nb, 2) = x(i); end flo = fhi; end end f = @(x) 50 * sin(0.5 * x) x.^2 17 * x + 60; % from last problem brackets = incsearch ( f, 0, 20, 100 ); [n m] = size(brackets); % note vector on left hand side The script below finds and outputs all roots of f between 0 and 20. if n == 0 fprintf ('No roots found.\n'); else for k = 1 : n x = fzero(f, brackets(k, :)); % select whole row fprintf ('There is a root at x = %f\n', x); end end Output: There is a root at x = 1.583780 There is a root at x = 6.636039 There is a root at x = 12.825489 There is a root at x = 16.266248

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

View Full Document
1/25/2010 2 Bisection Search (basic idea) start with x LOW (less than root) and x HIGH (greater than root) while true pick x half way between x and x ROOT LOW HIGH if termination conditions satisfied stop endif if f( x MID ) has same sign as f(x LOW ) x LOW = x ROOT else x HIGH = x ROOT endif endwhile Possible Termination Conditions: 1/. The error in x ROOT has become acceptably small (i.e. we have got close enough to the actual root).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern