% Frank Fisher
% solve nonlinear system of equations in Matlab
% The Ladder Problem

clear
clc

%These are the four simultaneous equations obtained for the ladder problem,
%written to equal 'zero' on the right hand side, and with the vector V
%representing the unknown variables that we want to solve.

% Solution is based on the information available on the MathWorks website
% "Is it possible to solve simultaneous non-linear equations with MATLAB?"
% http://www.mathworks.com/support/solutions/en/data/1-15NRJ/index.html

% V = [V1, V2, V3, V4], where
% V1 = angle of ladder 1 with respect to the left wall
% V2 = angle of ladder 2 with respect to the right wall
% V3 = 'x1' - distance from left wall to location where ladders cross
% V4 = 'x2' - distance from left wall to location where ladders cross
% NOTE: distance between the walls is x1 + x2, i.e. V(3) + V(4)

F = @(V) [cos(V(1))-(V(3)+V(4))/40; tan(V(1))-10/V(3); cos(V(2))-(V(3)+V(4))/60; tan(V(2))-10/V(4)];

InitialGuess = [0.2; 0.2; 25; 25];              %need to provide reasonable guesses
Options = optimset('Display','iter');
XY = fsolve(F, InitialGuess, Options);

disp(' ');          %add a blank line for formatting purposes

disp('The angles of the ladders (in degrees) are...');
disp(XY(1)*180/3.14159);
disp(XY(2)*180/3.14159);

disp('The total distance between the buildings is... ');
disp(XY(3)+XY(4))