Question
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
function [x,Emaxs] = crude (f, xL, xU, Edes, display)% CUDE Finds a minimum by performing a bisection like search
% Inputs: f = a function of one variable
% xL = lower bound of region containing minimum
% xU = upper bound of region containing minimum
% Edes = function stops when x is guaranteed within Edes of minimum
% display = display option (0 = no display (default), 1 = display)
% Outputs: x - estimate of minimum
% Emaxs - value of Emax as a function of iteration number k;
if nargin < 5; display = 0; end
if display
fprintf ...
(' N xL xA xB xU Emax\n');
end
functionCount = 0;
for k = 0 : 1000
Emax = (xU - xL) / 2;
Emaxs(k+1)=Emax;
xA = xL + 0.49 * (xU - xL); fxA = f(xA);
xB = xL + 0.51 * (xU - xL); fxB = f(xB);
functionCount = functionCount + 2;
if display
fprintf ('%5d %12.6f %12.6f %12.6f %12.6f %12.6f\n', ...
k, xL, xA, xB, xU, Emax);
end
if Emax <= Edes
x = (xL + xU) / 2; % take midpoint as final answer
if display
% the last two function evaluations weren't actually needed
% (the results weren't used) and could be eliminated
fprintf ('Tolerance achieved after %d function evaluations.\n', ...
functionCount - 2);
end...