## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# Mathematical Word Problems – Construction Tool

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.

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 datetime datatype in MATLAB.

### Contents

#### How does it work?

Given the birthdates for the elder and younger person, we want to return the date on which the elder is N times older than the younger one. Let's try it.

elderdate = datetime(1950, 1, 1);
youngerdate = datetime(1992, 7, 17);
doubledate = ntupleday(2, elderdate, youngerdate)


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.

#### Vectorizing

It was super easy to vectorize this, with respect to dates.

dd = datetime(1950, 1, 1:3:28)';
yd = datetime(1992, 7, 17:3:44)';
nd = ntupleday(2,dd,yd)

nd =
10×1 datetime array
31-Jan-2035
03-Feb-2035
06-Feb-2035
09-Feb-2035
12-Feb-2035
15-Feb-2035
18-Feb-2035
21-Feb-2035
24-Feb-2035
27-Feb-2035


Or for the multipliers

multiplier = 1.5:0.5:4.5;
newdates = ntupleday(multiplier, elderdate, youngerdate);
newdates'

ans =
7×1 datetime array
16-Aug-2077
31-Jan-2035
25-Nov-2020
24-Oct-2013
23-Jul-2009
21-Sep-2006
11-Sep-2004


What's the trend?

plot(newdates,multiplier, "-*")
ylabel("Age Multiplier")
xlabel("Date")


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.

#### And Then I Realized...

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.

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.

#### Seriously...

Do you take advantage of datetime in MATLAB? Especially if you have something novel you are doing with it, please share your thoughts here.

#### The Function ntupleday

function ndate = ntupleday(n, bdelder, bdyounger)
% The date on which the elder is n times as old as the younger
% NTUPLEDAY returns the date when the OLDER person is N times the age of
% the YOUNGER one.  Using this, parents can torture their children by
% making up a suite of word problems for the kids to solve.
%
% Example:
%   bdelder = datetime(1955,11,19);
%   bdyounger = datetime(1987,3,29);
%   doubleDate = ntupleday(2,bdelder, bdyounger)
ndate = bdyounger + (bdyounger-bdelder)./(n-1);
% Set the format of the output date to be the same as the first input date.
% If the input is of type "datetime".  Otherwise, don't worry about the
% format.
if isa(bdelder, "datetime")
% ndate.Format = 'dd-MMM-uuuu';
ndate.Format = bdelder.Format;
end
end %ntupleday

doubledate =
datetime
31-Jan-2035


Published with MATLAB® R2019a