{"id":1357,"date":"2016-03-14T12:00:55","date_gmt":"2016-03-14T17:00:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=1357"},"modified":"2016-02-25T16:49:55","modified_gmt":"2016-02-25T21:49:55","slug":"investigating-the-classic-crossed-ladders-puzzle","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2016\/03\/14\/investigating-the-classic-crossed-ladders-puzzle\/","title":{"rendered":"Investigating the Classic Crossed Ladders Puzzle"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><!--introduction--><p>Today's blog post is a complete working MATLAB program investigating the <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2016\/02\/29\/the-classic-crossed-ladders-puzzle\">crossed ladders problem<\/a>. Download a copy of the program via the link at the end. Publish it again with the <tt>publish<\/tt> command or the <tt>publish<\/tt> editor tab.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#a780adda-b71a-46ec-a7f3-ad871293d349\">The model<\/a><\/li><li><a href=\"#78816805-b6f9-4066-8781-c58ae04aa0da\">ladders_app<\/a><\/li><li><a href=\"#857a72f7-89ac-43cc-946b-3af8ceb125c4\">letters<\/a><\/li><li><a href=\"#5c61f507-35ac-444d-935e-27f5a24cdc2e\">numbers<\/a><\/li><li><a href=\"#804b9dc3-26b4-41af-9aa9-b971063495de\">initialize_figure<\/a><\/li><li><a href=\"#5be45b54-ad4f-4a6a-8128-7a9fd1faf095\">motion<\/a><\/li><li><a href=\"#9a4e929f-af46-42c4-b362-40481d53e73f\">lines<\/a><\/li><li><a href=\"#8993b60d-27c6-4083-b52f-abce9a8e0bb3\">down<\/a><\/li><li><a href=\"#3080591d-6658-43e3-b5e1-37c34a0606b8\">up<\/a><\/li><li><a href=\"#2a097830-9075-43fe-84bd-c2a72c3f9817\">buttons<\/a><\/li><li><a href=\"#250cc61d-51e9-4f0d-a623-bb0bc7d4f706\">labels<\/a><\/li><li><a href=\"#79689da8-5803-4e5e-9e42-bc6acd675594\">letters_cb<\/a><\/li><li><a href=\"#1c0b039c-b97d-46ea-8c8e-90c9322160a7\">numbers_cb<\/a><\/li><li><a href=\"#f8b21058-5c84-4024-ae6a-5589012919b4\">mirror_cb<\/a><\/li><li><a href=\"#adb1b884-f9b0-43d1-a0f2-ff6af37951e2\">reset_cb<\/a><\/li><li><a href=\"#1f90c627-94c0-49a7-ac13-45e417ca1dc4\">read_mouse<\/a><\/li><\/ul><\/div><h4>The model<a name=\"a780adda-b71a-46ec-a7f3-ad871293d349\"><\/a><\/h4><p>Explore the interaction of eight variables satisfying five nonlinear equations.<\/p><p>The eight variables are:<\/p><div><ul><li>$a$, length of one ladder.<\/li><li>$b$, length of the other ladder.<\/li><li>$c$, height of the point where they cross.<\/li><li>$u$, width of one base triangle.<\/li><li>$v$, width of the other base triangle.<\/li><li>$w$, width of the alley.<\/li><li>$x$, height of the point where one ladder meets the wall.<\/li><li>$y$, height of the point where the other ladder meets the other wall.<\/li><\/ul><\/div><p>The five equations are:<\/p><p>$$ a^2 = x^2 + w^2 $$<\/p><p>$$ b^2 = y^2 + w^2 $$<\/p><p>$$ \\frac{x}{w} = \\frac{c}{v} $$<\/p><p>$$ \\frac{y}{w} = \\frac{c}{u} $$<\/p><p>$$ w = u + v $$<\/p><h4>ladders_app<a name=\"78816805-b6f9-4066-8781-c58ae04aa0da\"><\/a><\/h4><p>The initial values provide the smallest solution that is all integers. Are there any other integer solutions within the range of this app?<\/p><pre class=\"codeinput\"><span class=\"keyword\">function<\/span> ladders_app\r\n<\/pre><pre class=\"codeinput\">    <span class=\"comment\">% The Classic Crossed Ladders Puzzle.<\/span>\r\n    <span class=\"comment\">% See Cleve's Corner, Feb. 29, 2016.<\/span>\r\n    <span class=\"comment\">% &lt;https:\/\/blogs.mathworks.com\/cleve\/2016\/02\/29\/the-classic-crossed-ladders-puzzle&gt;<\/span>\r\n\r\n    <span class=\"comment\">% Starting values are a minimal sum integer solution.<\/span>\r\n    w0 = 56;\r\n    x0 = 105;\r\n    y0 = 42;\r\n    w = w0;\r\n    x = x0;\r\n    y = y0;\r\n    c = x*y\/(x+y);\r\n    u = c*w\/y;\r\n    v = c*w\/x;\r\n\r\n    <span class=\"comment\">% Starting toggles have both labels off.<\/span>\r\n    letters = false;\r\n    numbers = false;\r\n\r\n    initialize_figure;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/ladders_app_01.png\" alt=\"\"> <h4>letters<a name=\"857a72f7-89ac-43cc-946b-3af8ceb125c4\"><\/a><\/h4><p>Snapshot with <tt>letters = true<\/tt> and a wider valley.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/ladders_app_letters.png\" alt=\"\"> <\/p><h4>numbers<a name=\"5c61f507-35ac-444d-935e-27f5a24cdc2e\"><\/a><\/h4><p>Snapshot of the mirror image of the previous values with <tt>numbers = true<\/tt>. Four of the eight values are still integers.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/ladders_app_numbers.png\" alt=\"\"> <\/p><h4>initialize_figure<a name=\"804b9dc3-26b4-41af-9aa9-b971063495de\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> initialize_figure\r\n        <span class=\"comment\">% Size of the square view<\/span>\r\n        h = 160;\r\n        clf\r\n        shg\r\n        set(gcf,<span class=\"string\">'menubar'<\/span>,<span class=\"string\">'none'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'numbertitle'<\/span>,<span class=\"string\">'off'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'name'<\/span>,<span class=\"string\">'Crossed Ladders'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'windowbuttondownfcn'<\/span>,@down, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'windowbuttonupfcn'<\/span>,@up)\r\n        axis([-h\/8 7\/8*h -h\/8 7\/8*h])\r\n        axis <span class=\"string\">square<\/span>\r\n        lines\r\n        buttons\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">%initialize_figure<\/span>\r\n<\/pre><h4>motion<a name=\"5be45b54-ad4f-4a6a-8128-7a9fd1faf095\"><\/a><\/h4><p>Called repeatedly as the mouse moves.<\/p><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> motion(varargin)\r\n        <span class=\"comment\">% WindowsButtonMotionFunction<\/span>\r\n        [p,q] = read_mouse;\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        <span class=\"comment\">% Which control point?<\/span>\r\n        <span class=\"keyword\">if<\/span> q &lt; c\/2\r\n            <span class=\"comment\">% Drag the alley width horizonally.<\/span>\r\n            w = p;\r\n        <span class=\"keyword\">elseif<\/span> p &lt; u\/2\r\n            <span class=\"comment\">% Drag the ladder against the left wall vertically.<\/span>\r\n            x = q;\r\n        <span class=\"keyword\">elseif<\/span> p &gt; w-v\/2\r\n            <span class=\"comment\">% Drag both the width and the other ladder.<\/span>\r\n            w = p;\r\n            y = q;\r\n        <span class=\"keyword\">else<\/span>\r\n            <span class=\"comment\">% Drag the crossing point, creating lots of action.<\/span>\r\n            c = q;\r\n            u = p;\r\n            v = w-u;\r\n            x = c*w\/v;\r\n            y = c*w\/u;\r\n        <span class=\"keyword\">end<\/span>\r\n        c = x*y\/(x+y);\r\n        lines\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% motion<\/span>\r\n<\/pre><h4>lines<a name=\"9a4e929f-af46-42c4-b362-40481d53e73f\"><\/a><\/h4><p>Play dot-to-dot.<\/p><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> lines\r\n        cla\r\n        ms = 3*get(gcf,<span class=\"string\">'defaultlinemarkersize'<\/span>);  <span class=\"comment\">% Large dots<\/span>\r\n        u = c*w\/y;\r\n\r\n        <span class=\"comment\">% Five markers at the vertices.<\/span>\r\n        line([0 0],[0 0],<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'.'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,ms)\r\n        line([w w],[0 0],<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'.'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,ms,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'k'<\/span>)\r\n        line([0 0],[x x],<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'.'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,ms,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'k'<\/span>)\r\n        line([w w],[y y],<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'.'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,ms,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'k'<\/span>)\r\n        line([u u],[c c],<span class=\"string\">'Marker'<\/span>,<span class=\"string\">'.'<\/span>,<span class=\"string\">'MarkerSize'<\/span>,ms,<span class=\"string\">'Color'<\/span>,<span class=\"string\">'k'<\/span>)\r\n\r\n        <span class=\"comment\">% Connect the markers with six lines.<\/span>\r\n        line([0 w],[0 0])\r\n        line([0 0],[0 x])\r\n        line([w w],[0 y])\r\n        line([0 w],[x 0])\r\n        line([w 0],[y 0])\r\n        line([u u],[c 0],<span class=\"string\">'LineStyle'<\/span>,<span class=\"string\">'-.'<\/span>)\r\n        box <span class=\"string\">on<\/span>\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% lines<\/span>\r\n<\/pre><h4>down<a name=\"8993b60d-27c6-4083-b52f-abce9a8e0bb3\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> down(varargin)\r\n        <span class=\"comment\">% Called at the start of mouse movement.<\/span>\r\n        <span class=\"comment\">% Activate the motion function.<\/span>\r\n        cla\r\n        set(gcf,<span class=\"string\">'windowbuttonmotionfcn'<\/span>,@motion)\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% down<\/span>\r\n<\/pre><h4>up<a name=\"3080591d-6658-43e3-b5e1-37c34a0606b8\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> up(varargin)\r\n        <span class=\"comment\">% Called at the end of mouse movement.<\/span>\r\n        <span class=\"comment\">% Deactivate motion function.<\/span>\r\n        set(gcf,<span class=\"string\">'windowbuttonmotionfcn'<\/span>,[])\r\n        set(gcf,<span class=\"string\">'windowbuttondownfcn'<\/span>,@down)\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% up<\/span>\r\n<\/pre><h4>buttons<a name=\"2a097830-9075-43fe-84bd-c2a72c3f9817\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> buttons\r\n        <span class=\"comment\">% Two toggles and three pushbuttons.<\/span>\r\n        posit = [.88 .64 .10 .06];\r\n        delta = [  0 .08   0   0];\r\n\r\n        <span class=\"comment\">% letters<\/span>\r\n        uicontrol(<span class=\"string\">'style'<\/span>,<span class=\"string\">'toggle'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'units'<\/span>,<span class=\"string\">'normalized'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'position'<\/span>,posit, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'string'<\/span>,<span class=\"string\">'letters'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'value'<\/span>,letters, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'callback'<\/span>,@letters_cb);\r\n\r\n        <span class=\"comment\">% numbers<\/span>\r\n        uicontrol(<span class=\"string\">'style'<\/span>,<span class=\"string\">'toggle'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'units'<\/span>,<span class=\"string\">'normalized'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'position'<\/span>,posit-delta, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'string'<\/span>,<span class=\"string\">'numbers'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'value'<\/span>,numbers, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'callback'<\/span>,@numbers_cb);\r\n\r\n        <span class=\"comment\">% mirror<\/span>\r\n        uicontrol(<span class=\"string\">'style'<\/span>,<span class=\"string\">'pushbutton'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'units'<\/span>,<span class=\"string\">'normalized'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'position'<\/span>,posit-2*delta, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'string'<\/span>,<span class=\"string\">'mirrror'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'callback'<\/span>,@mirror_cb);\r\n\r\n        <span class=\"comment\">% reset<\/span>\r\n        uicontrol(<span class=\"string\">'style'<\/span>,<span class=\"string\">'pushbutton'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'units'<\/span>,<span class=\"string\">'normalized'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'position'<\/span>,posit-3*delta, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'string'<\/span>,<span class=\"string\">'reset'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'callback'<\/span>,@reset_cb);\r\n\r\n        <span class=\"comment\">% close<\/span>\r\n        uicontrol(<span class=\"string\">'style'<\/span>,<span class=\"string\">'pushbutton'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'units'<\/span>,<span class=\"string\">'normalized'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'position'<\/span>,posit-4*delta, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'string'<\/span>,<span class=\"string\">'close'<\/span>, <span class=\"keyword\">...<\/span>\r\n            <span class=\"string\">'callback'<\/span>,<span class=\"string\">'close(gcf)'<\/span>);\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% buttons<\/span>\r\n<\/pre><h4>labels<a name=\"250cc61d-51e9-4f0d-a623-bb0bc7d4f706\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> labels\r\n        <span class=\"comment\">% Label lines with either letters or numbers.<\/span>\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        lines\r\n        <span class=\"keyword\">if<\/span> letters\r\n            f = <span class=\"string\">'%s'<\/span>;\r\n            xs = <span class=\"string\">'x  '<\/span>;\r\n            ys = <span class=\"string\">'  y'<\/span>;\r\n            ws = <span class=\"string\">' w '<\/span>;\r\n            as = <span class=\"string\">'  a'<\/span>;\r\n            bs = <span class=\"string\">'b  '<\/span>;\r\n            us = <span class=\"string\">' u '<\/span>;\r\n            vs = <span class=\"string\">' v '<\/span>;\r\n            cs = <span class=\"string\">'c  '<\/span>;\r\n            top = <span class=\"string\">'Drag a black dot.'<\/span>;\r\n        <span class=\"keyword\">elseif<\/span> numbers\r\n            <span class=\"comment\">% This is the only place values of a and b are needed.<\/span>\r\n            a = sqrt(x^2 + w^2);\r\n            b = sqrt(y^2 + w^2);\r\n            z = [a b c u v w x y];\r\n            <span class=\"keyword\">if<\/span> all(abs(z-round(z)) &lt; .001)\r\n                f = <span class=\"string\">'%.0f'<\/span>;\r\n            <span class=\"keyword\">else<\/span>\r\n                f = <span class=\"string\">'%.1f'<\/span>;\r\n            <span class=\"keyword\">end<\/span>\r\n            <span class=\"keyword\">if<\/span> sum(z) &lt; 50 || sum(z) &gt; 5000\r\n                scream\r\n            <span class=\"keyword\">end<\/span>\r\n            xs = x;\r\n            ys = y;\r\n            ws = w;\r\n            as = a;\r\n            bs = b;\r\n            us = u;\r\n            vs = v;\r\n            cs = c;\r\n            top = [<span class=\"string\">'sum = '<\/span> sprintf(f,sum(z))];\r\n        <span class=\"keyword\">else<\/span>\r\n            top = <span class=\"string\">'Drag a black dot.'<\/span>;\r\n        <span class=\"keyword\">end<\/span>\r\n        <span class=\"keyword\">if<\/span> letters || numbers\r\n            text(0,x\/2,sprintf(f,xs),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'right'<\/span>)\r\n            text(w,y\/2,sprintf(f,ys),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'left'<\/span>)\r\n            text(w\/2,-3,sprintf(f,ws),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'center'<\/span>)\r\n            text(w\/2,x\/2,sprintf(f,as),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'left'<\/span>)\r\n            text(w\/2,y\/2,sprintf(f,bs),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'right'<\/span>)\r\n            text(u\/2,3,sprintf(f,us),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'center'<\/span>)\r\n            text(w-v\/2,3,sprintf(f,vs),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'center'<\/span>)\r\n            text(u,c\/2,sprintf(f,cs),<span class=\"string\">'horiz'<\/span>,<span class=\"string\">'right'<\/span>)\r\n        <span class=\"keyword\">end<\/span>\r\n        title(top)\r\n     <span class=\"keyword\">end<\/span> <span class=\"comment\">% labels<\/span>\r\n<\/pre><h4>letters_cb<a name=\"79689da8-5803-4e5e-9e42-bc6acd675594\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> letters_cb(varargin)\r\n        <span class=\"comment\">% Called when letters button is toggled.<\/span>\r\n        letters = get(findobj(gcf,<span class=\"string\">'string'<\/span>,<span class=\"string\">'letters'<\/span>),<span class=\"string\">'value'<\/span>);\r\n        <span class=\"comment\">% Make sure numbers is false.<\/span>\r\n        numbers = false;\r\n        set(findobj(gcf,<span class=\"string\">'string'<\/span>,<span class=\"string\">'numbers'<\/span>),<span class=\"string\">'value'<\/span>,numbers)\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% letter_cb<\/span>\r\n<\/pre><h4>numbers_cb<a name=\"1c0b039c-b97d-46ea-8c8e-90c9322160a7\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> numbers_cb(varargin)\r\n        <span class=\"comment\">% Called when numbers button is toggled.<\/span>\r\n        numbers = get(findobj(gcf,<span class=\"string\">'string'<\/span>,<span class=\"string\">'numbers'<\/span>),<span class=\"string\">'value'<\/span>);\r\n        <span class=\"comment\">% Make sure letters is false.<\/span>\r\n        letters = false;\r\n        set(findobj(gcf,<span class=\"string\">'string'<\/span>,<span class=\"string\">'letters'<\/span>),<span class=\"string\">'value'<\/span>,letters)\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% numbers_cb<\/span>\r\n<\/pre><h4>mirror_cb<a name=\"f8b21058-5c84-4024-ae6a-5589012919b4\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> mirror_cb(varargin)\r\n        <span class=\"comment\">% Called when mirror button is pushed.<\/span>\r\n        <span class=\"comment\">% Interchange x and y.<\/span>\r\n        t = x;\r\n        x = y;\r\n        y = t;\r\n        <span class=\"comment\">% c = x*y\/(x+y) is unchanged<\/span>\r\n        lines\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% mirror_cb<\/span>\r\n<\/pre><h4>reset_cb<a name=\"adb1b884-f9b0-43d1-a0f2-ff6af37951e2\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> reset_cb(varargin)\r\n        <span class=\"comment\">% Called when reset button is pushed.<\/span>\r\n        <span class=\"comment\">% Restore initial values.<\/span>\r\n        w = w0;\r\n        x = x0;\r\n        y = y0;\r\n        c = x*y\/(x+y);\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        lines\r\n        labels\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% reset_cb<\/span>\r\n<\/pre><h4>read_mouse<a name=\"1f90c627-94c0-49a7-ac13-45e417ca1dc4\"><\/a><\/h4><pre class=\"codeinput\">    <span class=\"keyword\">function<\/span> [p,q] = read_mouse\r\n        <span class=\"comment\">% Current horizontal and vertical coordinates of the mouse.<\/span>\r\n        pq = get(gca,<span class=\"string\">'currentpoint'<\/span>);\r\n        p = pq(1,1);\r\n        q = pq(1,2);\r\n    <span class=\"keyword\">end<\/span> <span class=\"comment\">% read_mouse<\/span>\r\n<\/pre><pre class=\"codeinput\"><span class=\"keyword\">end<\/span> <span class=\"comment\">% ladders_app<\/span>\r\n<\/pre><script language=\"JavaScript\"> <!-- \r\n    function grabCode_855fb105ddeb44c99ab6db9e0cbcaed2() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='855fb105ddeb44c99ab6db9e0cbcaed2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 855fb105ddeb44c99ab6db9e0cbcaed2';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_855fb105ddeb44c99ab6db9e0cbcaed2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\r\n855fb105ddeb44c99ab6db9e0cbcaed2 ##### SOURCE BEGIN #####\r\n%% Investigating the Classic Crossed Ladders Puzzle\r\n% Today's blog post is a complete working MATLAB program investigating the\r\n% <https:\/\/blogs.mathworks.com\/cleve\/2016\/02\/29\/the-classic-crossed-ladders-puzzle\r\n% crossed ladders problem>.\r\n% Download a copy of the program via the link at the end.\r\n% Publish it again with the |publish| command or the |publish| editor tab.\r\n\r\n%% The model\r\n% Explore the interaction of eight variables satisfying five\r\n% nonlinear equations.\r\n\r\n%%\r\n% The eight variables are:\r\n%\r\n% * $a$, length of one ladder.\r\n% * $b$, length of the other ladder.\r\n% * $c$, height of the point where they cross.\r\n% * $u$, width of one base triangle.\r\n% * $v$, width of the other base triangle.\r\n% * $w$, width of the alley.\r\n% * $x$, height of the point where one ladder meets the wall.\r\n% * $y$, height of the point where the other ladder meets the other wall.\r\n\r\n%% \r\n% The five equations are:\r\n%\r\n% $$ a^2 = x^2 + w^2 $$\r\n%\r\n% $$ b^2 = y^2 + w^2 $$\r\n%\r\n% $$ \\frac{x}{w} = \\frac{c}{v} $$\r\n%\r\n% $$ \\frac{y}{w} = \\frac{c}{u} $$\r\n%\r\n% $$ w = u + v $$\r\n\r\n%% ladders_app\r\n% The initial values provide the smallest solution that is all integers.\r\n% Are there any other integer solutions within the range of this app?\r\n\r\nfunction ladders_app\r\n    % The Classic Crossed Ladders Puzzle.\r\n    % See Cleve's Corner, Feb. 29, 2016.\r\n    % <https:\/\/blogs.mathworks.com\/cleve\/2016\/02\/29\/the-classic-crossed-ladders-puzzle>\r\n     \r\n    % Starting values are a minimal sum integer solution.\r\n    w0 = 56;\r\n    x0 = 105;\r\n    y0 = 42;\r\n    w = w0;\r\n    x = x0;\r\n    y = y0;\r\n    c = x*y\/(x+y);\r\n    u = c*w\/y;\r\n    v = c*w\/x;\r\n    \r\n    % Starting toggles have both labels off.\r\n    letters = false;\r\n    numbers = false;\r\n    \r\n    initialize_figure;\r\n    \r\n%% letters\r\n% Snapshot with |letters = true| and a wider valley.\r\n%\r\n% <<ladders_app_letters.png>>\r\n\r\n%% numbers\r\n% Snapshot of the mirror image of the previous values with |numbers = true|.\r\n% Four of the eight values are still integers. \r\n%\r\n% <<ladders_app_numbers.png>>\r\n    \r\n%% initialize_figure\r\n  \r\n    function initialize_figure      \r\n        % Size of the square view\r\n        h = 160;\r\n        clf\r\n        shg\r\n        set(gcf,'menubar','none', ...\r\n            'numbertitle','off', ...\r\n            'name','Crossed Ladders', ...\r\n            'windowbuttondownfcn',@down, ...\r\n            'windowbuttonupfcn',@up)\r\n        axis([-h\/8 7\/8*h -h\/8 7\/8*h])\r\n        axis square\r\n        lines\r\n        buttons\r\n        labels\r\n    end %initialize_figure\r\n\r\n%% motion\r\n% Called repeatedly as the mouse moves.\r\n    \r\n    function motion(varargin)\r\n        % WindowsButtonMotionFunction\r\n        [p,q] = read_mouse;\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        % Which control point?\r\n        if q < c\/2\r\n            % Drag the alley width horizonally.\r\n            w = p;\r\n        elseif p < u\/2\r\n            % Drag the ladder against the left wall vertically.\r\n            x = q;\r\n        elseif p > w-v\/2\r\n            % Drag both the width and the other ladder.\r\n            w = p;\r\n            y = q;\r\n        else\r\n            % Drag the crossing point, creating lots of action.\r\n            c = q;\r\n            u = p;\r\n            v = w-u;\r\n            x = c*w\/v;\r\n            y = c*w\/u;\r\n        end\r\n        c = x*y\/(x+y);\r\n        lines\r\n    end % motion\r\n        \r\n%% lines\r\n% Play dot-to-dot.\r\n    \r\n    function lines\r\n        cla\r\n        ms = 3*get(gcf,'defaultlinemarkersize');  % Large dots      \r\n        u = c*w\/y; \r\n\r\n        % Five markers at the vertices.\r\n        line([0 0],[0 0],'Marker','.','MarkerSize',ms)\r\n        line([w w],[0 0],'Marker','.','MarkerSize',ms,'Color','k')\r\n        line([0 0],[x x],'Marker','.','MarkerSize',ms,'Color','k')\r\n        line([w w],[y y],'Marker','.','MarkerSize',ms,'Color','k')\r\n        line([u u],[c c],'Marker','.','MarkerSize',ms,'Color','k')\r\n        \r\n        % Connect the markers with six lines.\r\n        line([0 w],[0 0])\r\n        line([0 0],[0 x])\r\n        line([w w],[0 y])\r\n        line([0 w],[x 0])\r\n        line([w 0],[y 0])\r\n        line([u u],[c 0],'LineStyle','-.')\r\n        box on\r\n    end % lines\r\n\r\n%% down\r\n    \r\n    function down(varargin)\r\n        % Called at the start of mouse movement.\r\n        % Activate the motion function.\r\n        cla\r\n        set(gcf,'windowbuttonmotionfcn',@motion)\r\n    end % down\r\n\r\n%% up\r\n    \r\n    function up(varargin)\r\n        % Called at the end of mouse movement.\r\n        % Deactivate motion function.\r\n        set(gcf,'windowbuttonmotionfcn',[])\r\n        set(gcf,'windowbuttondownfcn',@down)\r\n        labels\r\n    end % up\r\n\r\n%% buttons\r\n\r\n    function buttons\r\n        % Two toggles and three pushbuttons.\r\n        posit = [.88 .64 .10 .06];\r\n        delta = [  0 .08   0   0];\r\n        \r\n        % letters\r\n        uicontrol('style','toggle', ...\r\n            'units','normalized', ...\r\n            'position',posit, ...\r\n            'string','letters', ...\r\n            'value',letters, ...\r\n            'callback',@letters_cb);\r\n        \r\n        % numbers\r\n        uicontrol('style','toggle', ...\r\n            'units','normalized', ...\r\n            'position',posit-delta, ...\r\n            'string','numbers', ...        \r\n            'value',numbers, ...\r\n            'callback',@numbers_cb); \r\n        \r\n        % mirror\r\n        uicontrol('style','pushbutton', ...\r\n            'units','normalized', ...\r\n            'position',posit-2*delta, ...\r\n            'string','mirrror', ...        \r\n            'callback',@mirror_cb);\r\n        \r\n        % reset\r\n        uicontrol('style','pushbutton', ...\r\n            'units','normalized', ...\r\n            'position',posit-3*delta, ...\r\n            'string','reset', ...\r\n            'callback',@reset_cb);\r\n        \r\n        % close\r\n        uicontrol('style','pushbutton', ...\r\n            'units','normalized', ...\r\n            'position',posit-4*delta, ...\r\n            'string','close', ...\r\n            'callback','close(gcf)');        \r\n    end % buttons\r\n\r\n%% labels\r\n\r\n    function labels\r\n        % Label lines with either letters or numbers.\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        lines\r\n        if letters\r\n            f = '%s';\r\n            xs = 'x  ';\r\n            ys = '  y';\r\n            ws = ' w ';\r\n            as = '  a';\r\n            bs = 'b  ';\r\n            us = ' u ';\r\n            vs = ' v ';\r\n            cs = 'c  ';\r\n            top = 'Drag a black dot.';\r\n        elseif numbers\r\n            % This is the only place values of a and b are needed.\r\n            a = sqrt(x^2 + w^2);\r\n            b = sqrt(y^2 + w^2);\r\n            z = [a b c u v w x y];\r\n            if all(abs(z-round(z)) < .001)\r\n                f = '%.0f';\r\n            else\r\n                f = '%.1f';\r\n            end\r\n            if sum(z) < 50 || sum(z) > 5000\r\n                scream\r\n            end\r\n            xs = x;\r\n            ys = y;\r\n            ws = w;\r\n            as = a;\r\n            bs = b;\r\n            us = u;\r\n            vs = v;\r\n            cs = c;\r\n            top = ['sum = ' sprintf(f,sum(z))];\r\n        else\r\n            top = 'Drag a black dot.';\r\n        end\r\n        if letters || numbers\r\n            text(0,x\/2,sprintf(f,xs),'horiz','right')\r\n            text(w,y\/2,sprintf(f,ys),'horiz','left')\r\n            text(w\/2,-3,sprintf(f,ws),'horiz','center')\r\n            text(w\/2,x\/2,sprintf(f,as),'horiz','left')\r\n            text(w\/2,y\/2,sprintf(f,bs),'horiz','right')\r\n            text(u\/2,3,sprintf(f,us),'horiz','center')\r\n            text(w-v\/2,3,sprintf(f,vs),'horiz','center')\r\n            text(u,c\/2,sprintf(f,cs),'horiz','right')\r\n        end\r\n        title(top)\r\n     end % labels\r\n     \r\n%% letters_cb\r\n\r\n    function letters_cb(varargin)\r\n        % Called when letters button is toggled.\r\n        letters = get(findobj(gcf,'string','letters'),'value');\r\n        % Make sure numbers is false.\r\n        numbers = false;\r\n        set(findobj(gcf,'string','numbers'),'value',numbers)\r\n        labels\r\n    end % letter_cb\r\n    \r\n%% numbers_cb\r\n\r\n    function numbers_cb(varargin)\r\n        % Called when numbers button is toggled.\r\n        numbers = get(findobj(gcf,'string','numbers'),'value');\r\n        % Make sure letters is false.\r\n        letters = false;\r\n        set(findobj(gcf,'string','letters'),'value',letters)\r\n        labels\r\n    end % numbers_cb\r\n\r\n%% mirror_cb\r\n\r\n    function mirror_cb(varargin)\r\n        % Called when mirror button is pushed.\r\n        % Interchange x and y.\r\n        t = x;\r\n        x = y;\r\n        y = t;\r\n        % c = x*y\/(x+y) is unchanged\r\n        lines\r\n        labels\r\n    end % mirror_cb\r\n\r\n%% reset_cb\r\n\r\n    function reset_cb(varargin)\r\n        % Called when reset button is pushed.\r\n        % Restore initial values.\r\n        w = w0;\r\n        x = x0;\r\n        y = y0;\r\n        c = x*y\/(x+y);\r\n        u = c*w\/y;\r\n        v = c*w\/x;\r\n        lines\r\n        labels\r\n    end % reset_cb\r\n\r\n%% read_mouse\r\n\r\n    function [p,q] = read_mouse\r\n        % Current horizontal and vertical coordinates of the mouse.\r\n        pq = get(gca,'currentpoint');\r\n        p = pq(1,1);\r\n        q = pq(1,2);\r\n    end % read_mouse\r\n\r\nend % ladders_app\r\n##### SOURCE END ##### 855fb105ddeb44c99ab6db9e0cbcaed2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/cleve\/ladders_app_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Today's blog post is a complete working MATLAB program investigating the <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2016\/02\/29\/the-classic-crossed-ladders-puzzle\">crossed ladders problem<\/a>. Download a copy of the program via the link at the end. Publish it again with the <tt>publish<\/tt> command or the <tt>publish<\/tt> editor tab.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2016\/03\/14\/investigating-the-classic-crossed-ladders-puzzle\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,23],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1357"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=1357"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1357\/revisions"}],"predecessor-version":[{"id":1359,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/1357\/revisions\/1359"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=1357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=1357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=1357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}