{"id":3402,"date":"2019-07-30T10:33:52","date_gmt":"2019-07-30T15:33:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=3402"},"modified":"2019-07-30T10:33:52","modified_gmt":"2019-07-30T15:33:52","slug":"mathematical-word-problems-construction-tool","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2019\/07\/30\/mathematical-word-problems-construction-tool\/","title":{"rendered":"Mathematical Word Problems &#8211; Construction Tool"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>Do your kids have to practice solving mathematical word problems?  Maybe they need to practice more during school breaks?  I've written a function that can turn you in to a machine for torturing your kids with some.<\/p><p>Actually, this started off as a tool to find the date on which I was X times older than a colleague. It's super simple to do this with the help of the <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html\">datetime<\/a><\/tt> datatype in MATLAB.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#c775f5fe-6747-4f46-8012-3f316c052990\">How does it work?<\/a><\/li><li><a href=\"#460c17aa-18b6-4097-9f48-2a305e6e33d1\">Vectorizing<\/a><\/li><li><a href=\"#43950e5f-4853-4e5a-84fa-3d2bf70e6fc3\">And Then I Realized...<\/a><\/li><li><a href=\"#e0ed0b99-9dab-48a8-9b4d-676831438503\">Improvements to Be Made<\/a><\/li><li><a href=\"#f745b186-1c8d-48e0-8729-371eb61c47b9\">Seriously...<\/a><\/li><li><a href=\"#1f3ec82c-7c1b-4a82-85ee-3193ade2d13a\">The Function ntupleday<\/a><\/li><\/ul><\/div><h4>How does it work?<a name=\"c775f5fe-6747-4f46-8012-3f316c052990\"><\/a><\/h4><p>Given the birthdates for the elder and younger person, we want to return the date on which the elder is <tt>N<\/tt> times older than the younger one. Let's try it.<\/p><pre class=\"codeinput\">elderdate = datetime(1950, 1, 1);\r\nyoungerdate = datetime(1992, 7, 17);\r\ndoubledate = ntupleday(2, elderdate, youngerdate)\r\n<\/pre><p>From there, it's just a matter of creating the relationships between various people, basically a set of linear constraints, and you are ready to embellish the stories as a new torture device for your kids. problems with for your kids to solve.<\/p><h4>Vectorizing<a name=\"460c17aa-18b6-4097-9f48-2a305e6e33d1\"><\/a><\/h4><p>It was super easy to vectorize this, with respect to dates.<\/p><pre class=\"codeinput\">dd = datetime(1950, 1, 1:3:28)';\r\nyd = datetime(1992, 7, 17:3:44)';\r\nnd = ntupleday(2,dd,yd)\r\n<\/pre><pre class=\"codeoutput\">nd = \r\n  10&times;1 datetime array\r\n   31-Jan-2035\r\n   03-Feb-2035\r\n   06-Feb-2035\r\n   09-Feb-2035\r\n   12-Feb-2035\r\n   15-Feb-2035\r\n   18-Feb-2035\r\n   21-Feb-2035\r\n   24-Feb-2035\r\n   27-Feb-2035\r\n<\/pre><p>Or for the multipliers<\/p><pre class=\"codeinput\">multiplier = 1.5:0.5:4.5;\r\nnewdates = ntupleday(multiplier, elderdate, youngerdate);\r\nnewdates'\r\n<\/pre><pre class=\"codeoutput\">ans = \r\n  7&times;1 datetime array\r\n   16-Aug-2077\r\n   31-Jan-2035\r\n   25-Nov-2020\r\n   24-Oct-2013\r\n   23-Jul-2009\r\n   21-Sep-2006\r\n   11-Sep-2004\r\n<\/pre><p>What's the trend?<\/p><pre class=\"codeinput\">plot(newdates,multiplier, <span class=\"string\">\"-*\"<\/span>)\r\nylabel(<span class=\"string\">\"Age Multiplier\"<\/span>)\r\nxlabel(<span class=\"string\">\"Date\"<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/ntupledate_01.png\" alt=\"\"> <p>Clearly, we can't expect the younger person to actually catch up in age to the older one - just likely that the difference gets less significant over time.<\/p><h4>And Then I Realized...<a name=\"43950e5f-4853-4e5a-84fa-3d2bf70e6fc3\"><\/a><\/h4><p>I then realized there was nothing so special with dates except the format.  So this can also be used for entities with other units, if you need to work that in.<\/p><h4>Improvements to Be Made<a name=\"e0ed0b99-9dab-48a8-9b4d-676831438503\"><\/a><\/h4><p>It would be great to add the full functionality for building the problems.  I leave that as an exercise for you.  E.g., what functions should I add if I want to be able to construct a problem given one date, say the elder, and have it spit out what the birthdate would if the child was 3 years and 2 months younger than half dad's birthdate.<\/p><h4>Seriously...<a name=\"f745b186-1c8d-48e0-8729-371eb61c47b9\"><\/a><\/h4><p>Do you take advantage of <tt>datetime<\/tt> in MATLAB?  Especially if you have something novel you are doing with it, please share your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=3402#respond\">here<\/a>.<\/p><h4>The Function ntupleday<a name=\"1f3ec82c-7c1b-4a82-85ee-3193ade2d13a\"><\/a><\/h4><pre class=\"codeinput\"><span class=\"keyword\">function<\/span> ndate = ntupleday(n, bdelder, bdyounger)\r\n<span class=\"comment\">% The date on which the elder is n times as old as the younger<\/span>\r\n<span class=\"comment\">% NTUPLEDAY returns the date when the OLDER person is N times the age of<\/span>\r\n<span class=\"comment\">% the YOUNGER one.  Using this, parents can torture their children by<\/span>\r\n<span class=\"comment\">% making up a suite of word problems for the kids to solve.<\/span>\r\n<span class=\"comment\">%<\/span>\r\n<span class=\"comment\">% Example:<\/span>\r\n<span class=\"comment\">%   bdelder = datetime(1955,11,19);<\/span>\r\n<span class=\"comment\">%   bdyounger = datetime(1987,3,29);<\/span>\r\n<span class=\"comment\">%   doubleDate = ntupleday(2,bdelder, bdyounger)<\/span>\r\nndate = bdyounger + (bdyounger-bdelder).\/(n-1);\r\n<span class=\"comment\">% Set the format of the output date to be the same as the first input date.<\/span>\r\n<span class=\"comment\">% If the input is of type \"datetime\".  Otherwise, don't worry about the<\/span>\r\n<span class=\"comment\">% format.<\/span>\r\n<span class=\"keyword\">if<\/span> isa(bdelder, <span class=\"string\">\"datetime\"<\/span>)\r\n   <span class=\"comment\">% ndate.Format = 'dd-MMM-uuuu';<\/span>\r\n   ndate.Format = bdelder.Format;\r\n<span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span> <span class=\"comment\">%ntupleday<\/span>\r\n<\/pre><pre class=\"codeoutput\">doubledate = \r\n  datetime\r\n   31-Jan-2035\r\n<\/pre><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b4d9aeb24a3343ffafc4687906f3f164() {\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='b4d9aeb24a3343ffafc4687906f3f164 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b4d9aeb24a3343ffafc4687906f3f164';\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 2019 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_b4d9aeb24a3343ffafc4687906f3f164()\"><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; R2019a<br><\/p><\/div><!--\r\nb4d9aeb24a3343ffafc4687906f3f164 ##### SOURCE BEGIN #####\r\n%% Mathematical Word Problems - Construction Tool\r\n% Do your kids have to practice solving mathematical word problems?  Maybe\r\n% they need to practice more during school breaks?  I've written a function\r\n% that can turn you in to a machine for torturing your kids with some.\r\n%\r\n% Actually, this started off as a tool to find the date on which I was X\r\n% times older than a colleague. It's super simple to do this with the help\r\n% of the |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/datetime.html\r\n% datetime>| datatype in MATLAB.\r\n%% How does it work?\r\n% Given the birthdates for the elder and younger person, we want to return\r\n% the date on which the elder is |N| times older than the younger one.\r\n% Let's try it.\r\nelderdate = datetime(1950, 1, 1);\r\nyoungerdate = datetime(1992, 7, 17);\r\ndoubledate = ntupleday(2, elderdate, youngerdate)\r\n%% \r\n% From there, it's just a matter of creating the relationships between\r\n% various people, basically a set of linear constraints, and you are ready\r\n% to embellish the stories as a new torture device for your kids.\r\n% problems with for your kids to solve.\r\n%% Vectorizing\r\n% It was super easy to vectorize this, with respect to dates.\r\ndd = datetime(1950, 1, 1:3:28)';\r\nyd = datetime(1992, 7, 17:3:44)';\r\nnd = ntupleday(2,dd,yd)\r\n%%\r\n% Or for the multipliers\r\nmultiplier = 1.5:0.5:4.5;\r\nnewdates = ntupleday(multiplier, elderdate, youngerdate);\r\nnewdates'\r\n%%\r\n% What's the trend?\r\nplot(newdates,multiplier, \"-*\")\r\nylabel(\"Age Multiplier\")\r\nxlabel(\"Date\")\r\n%%\r\n% Clearly, we can't expect the younger person to actually catch up in age\r\n% to the older one - just likely that the difference gets less significant\r\n% over time.\r\n%% And Then I Realized...\r\n% I then realized there was nothing so special with dates except the\r\n% format.  So this can also be used for entities with other units, if you\r\n% need to work that in.  \r\n%% Improvements to Be Made\r\n% It would be great to add the full functionality for building the\r\n% problems.  I leave that as an exercise for you.  E.g., what functions\r\n% should I add if I want to be able to construct a problem given one date,\r\n% say the elder, and have it spit out what the birthdate would if the child\r\n% was 3 years and 2 months younger than half dad's birthdate.\r\n%% Seriously...\r\n% Do you take advantage of |datetime| in MATLAB?  Especially if you have\r\n% something novel you are doing with it, please share your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=3402#respond here>.\r\n%% The Function ntupleday\r\nfunction ndate = ntupleday(n, bdelder, bdyounger)\r\n% The date on which the elder is n times as old as the younger\r\n% NTUPLEDAY returns the date when the OLDER person is N times the age of\r\n% the YOUNGER one.  Using this, parents can torture their children by\r\n% making up a suite of word problems for the kids to solve.\r\n%\r\n% Example:\r\n%   bdelder = datetime(1955,11,19);\r\n%   bdyounger = datetime(1987,3,29);\r\n%   doubleDate = ntupleday(2,bdelder, bdyounger)\r\nndate = bdyounger + (bdyounger-bdelder).\/(n-1);\r\n% Set the format of the output date to be the same as the first input date.\r\n% If the input is of type \"datetime\".  Otherwise, don't worry about the\r\n% format.\r\nif isa(bdelder, \"datetime\")\r\n   % ndate.Format = 'dd-MMM-uuuu';\r\n   ndate.Format = bdelder.Format;\r\nend\r\nend %ntupleday\r\n##### SOURCE END ##### b4d9aeb24a3343ffafc4687906f3f164\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2019\/ntupledate_01.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Do your kids have to practice solving mathematical word problems?  Maybe they need to practice more during school breaks?  I've written a function that can turn you in to a machine for torturing your kids with some.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2019\/07\/30\/mathematical-word-problems-construction-tool\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3402"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=3402"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3402\/revisions"}],"predecessor-version":[{"id":3408,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/3402\/revisions\/3408"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=3402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=3402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=3402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}