function maxminpractice
fplot(@(x) -fcn(x),[-2.5 2.5])
[xloc,f]=fminbnd(@(x) fcn(x),-1,1)
disp('Press any key to continue')
pause
fplot(@(x) distance(x),[0 1000])
[time,mindist]=fminbnd(@(x) distance(x),900,1000)
[time,maxnegdist]=fminbnd(@(x) negdistance(x),700,800)
disp('Press any key to continue')
pause
x=0:0.1:1000;
y=distance(x);
[ymax,imax,ymin,imin] = extrema(y);
plot(x,y,x(imax),ymax,'ko',x(imin),ymin,'ro')
fprintf('Max using extrema.m is %f\n',max(ymax))
fprintf('Min using extrema.m is %f\n',min(ymin))
%
function f=fcn(x)
f=-(1-x-5*x.^2+x.^4);
%
function d=distance(t)
xm=-11.9084+57.9117*cos(2*pi*t/87.97);
ym=56.6741*sin(2*pi*t/87.97);
xe=-2.4987+149.6041*cos(2*pi*t/365.25);
ye=149.5832*sin(2*pi*t/365.25);
d=sqrt((xm-xe).^2+(ym-ye).^2);
%
function d=negdistance(t)
d=-distance(t);