function placetd_better_hw2
% this version uses one file
% this version does not need global to share data
% this version suppresses the displayed output from fsolve
% this version is a function not a script
% this version employs a two second pause to see the part(a) output
%
%listing of main (output) variables
% where x(1) = w
% x(2) = H1
% x(3) = H2
% x(4) = a
% x(5) = b
%
clear, clc, format short g, format compact
opts = optimset('Display', ['off']);

function [f] = find_width(x)
% This is the system of equations to be solved in Lab2 - Spring 2012
% The method employed will be fsolve
f(1) = x(1)^2+x(2)^2-L1^2;
f(2) = x(2)/x(1)-h/x(5);
f(3) = x(1)-x(4)-x(5);
f(4) = x(3)/x(1)-h/x(4);
f(5) = x(1)^2+x(3)^2-L2^2;
end

% part (a) width for a single ladder length
L1 = 12;
L2 = 10;
h = 3;
% starting values
xg(1)=min([L1,L2])/2;
xg(2)=L1/2;
xg(3)=L2/2;
xg(4)=xg(1)/2;
xg(5)=xg(1)/2;
soln = fsolve(@find_width,xg,opts);
disp('part (a): w = ')
disp(soln(1))
pause(2)

% part (b) now preparing data for plot
% these are just used to establish reasonable starting values
steps = 11;
ladder = linspace(10,15,steps);
for k = 1 : steps
L1 = ladder(k);
xg(1)=min([L1,L2])/2;
xg(2)=L1/2;
xg(3)=L2/2;
xg(4)=xg(1)/2;
xg(5)=xg(1)/2;
soln = fsolve(@find_width,xg,opts);
disp('part (b) w = ')
disp(soln(1))
% save values for plotting
xplot(k)=L1;
yplot(k)=soln(1);
end
plot(xplot,yplot)
xlabel('Length L1 (ft)')
ylabel('Alley width w (ft)')
title('placetd\_hw2\_SOLN')
end