% Script quadMin
% Minimum and Maximum of a quadratic q(x) = x^2 + bx + c
% [Use nested IFs]

close all

disp('Min and max of quadratic x^2 + bx + c over [L,R]')
b = input('Enter b: ');
c = input('Enter c: ');
L = input('Enter L: ');
R = input('Enter R (L<=R): ');

xc = -b/2; % critical point
if L<=xc && xc<=R
% critical point is inside the interval
xMin= xc;
if (xc-L)>(R-xc)
xMax= L;
else
xMax= R;
end
else
% Minimum of q is at one of the endpoints
if xc < L
xMin= L;
xMax= R;
else
xMin= R;
xMax= L;
end
end
qMin= xMin^2 + b*xMin + c;
qMax= xMax^2 + b*xMax + c;

fprintf('Min value is %.4f\n', qMin)
fprintf('Max value is %.4f\n', qMax)

% Plot a graph
xvector= linspace(L, R, 100);
qvector= xvector.^2 + b*xvector + c;
plot(xvector,qvector, xMin,qMin,'o', xMax,qMax,'*')
title('q(x) from x=L to x=R')