function hw2solutions
clear all
v=20:20:160;
drag=[10 50 109 180 300 420 565 771];
numpts=max(size(v));
zin=0.1;
zout=fminsearch(@(z) sumoferrs1(z,v,drag), zin)
xplot=v(1):(v(end)-v(1))/(10*numpts):v(end);
yplot=curve1(xplot,zout);
plot(v,drag,'+',xplot,yplot)
xlabel('V (km/h)')
ylabel('FD (N)')
disp('press any key to continue')
pause

v=20:20:160;
drag=[10 50 109 180 300 420 565 771];
numpts=max(size(v));
zin(1)=0.1;
zin(2)=2;
zout=fminsearch(@(z) sumoferrs2(z,v,drag), zin)
xplot=v(1):(v(end)-v(1))/(10*numpts):v(end);
yplot=curve2(xplot,zout);
plot(v,drag,'+',xplot,yplot)
xlabel('V (km/h)')
ylabel('FD (N)')
disp('press any key to continue')
pause

dates=1900:10:2000;
time=dates-1900;
pop=[75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212 226.505 249.633 281.422];
coefs=polyfit(time,pop,8);
tplot=0:1:120;
ymodel=polyval(coefs,tplot);
plot(time,pop,'o',tplot,ymodel)
xlabel('time (years since 1900)')
ylabel('population (millions)')
zeropop=1900+floor(fzero(@(t) polyval(coefs,t),100));
fprintf('US Pop will apparently be 0 in the year %i\n',zeropop)
disp('press any key to continue')
pause

r_one=0.1:0.0001:10;
y=surfarea(r_one);
plot(r_one,y)
xlabel('R_1')
ylabel('Surface Area (cm^2)')
[x, f]=fminbnd(@(r_one) surfarea(r_one), 0, 6);
fprintf('Minimum surface area is %f cm^2 at a radius of %f cm\n',f,x)

function f=curve2(v,z)
rho=1.2;
cda=z(1);
n=z(2);
f=1/2*rho*cda*v.^n; 

function f=curve1(v,cda)
rho=1.2;
f=1/2*rho*cda*v.^2; 

function f=sumoferrs2(z, x, y)
f=sum((curve2(x,z)-y).^2);

function f=sumoferrs1(z, x, y)
f=sum((curve1(x,z)-y).^2);

function sa=surfarea(r_one)
r_two=r_one*1.3;
volume=240;
h=3*volume/pi./(r_one.^2+r_one.*r_two+r_two.^2);
sa=pi*r_one.^2+pi*(r_one+r_two).*sqrt((r_two-r_one).^2+h.^2);

