% Find min of q(x)=x^2+bx+c given b,c and random L,R. Plot the function.

close all; % clear all previous figures

b= 2; c= -1.5; % coefficients of q(x)
L= rand*10 - 8; % left end of interval, random in (-8,2)
R= L + 1 + rand*10; % right end of interval, random in (-7,13)

xc = -b/2; % critical value

if (L<=xc && xc<=R)
qMin= -b*b/4+c; % q(xc)
xMin= xc;
else
qL= L*L + b*L + c; % q(L)
qR= R*R + b*R + c; % q(R)
if (qL < qR)
qMin= qL;
xMin= L;
else
qMin= qR;
xMin= R;
end
end

disp('Quadratic function q(x) = x^2 + bx + c')
fprintf(' b=%.2f c=%.2f\n', b,c)
fprintf('The minimum value in [%.2f,%.2f] is %.4f\n\n', L,R,qMin)

% Plot the graph (just for fun)
% Note: you DO NOT need to learn plotting as shown below for
% now, but feel free to experiment with the commands.
% You'll learn the details on plotting later.
xvector= linspace(L, R);
qvector= xvector.^2 + b*xvector + c;
plot(xvector,qvector, xMin,qMin,'o', ...
'Linewidth',2, 'MarkerSize',12)
title(sprintf('q(x) from x=%.1f to x=%.1f', L, R), 'Fontsize',14)
shg