## Thursday, August 17, 2006

The few people that read this blog may remember that my wife and I are engaged in the "landscaping project from hell," part of which has been to replace our deck. My wife asked me to calculate the places to drill the holes to mount the posts between the rails such that all the gaps between the posts are the same and no more than 3 and 15/16 inches (per county building code). Below is the Matlab program I wrote to make this calculations, given the length of the rail (rl), the thickness of the posts (pw), and the maximum space between the posts (ms):

clear;close all;
format rat;
% Note: all units are in inches
pw = 1 + 5/8; % post width
ms = 3+15/16; % maximum space
rl = 59.5; % actual rail length
ps = pw+ms; % minimum post spacing
erl = rl + pw; % effective rail length, since rail thickness not present on each end
np = ceil(erl/ps); % minimum number of posts+1, rounded up to nearest whole number
aps = erl/np; % actual post spacing determined by whole number of posts
i = (aps-pw/2):aps:rl; % This is a vector containing the floating point measurements
x = round(i*16)/16; % Rounded to nearest 16th of an inch
y = floor(x); % The whole number
z = (x - y); % The decimal. Should appear as a fraction
total = [y' z'] % Output

These calculations can be done by hand by interpreting the code as algebraic expressions. Note that "ceil()" means to round the number UP to the nearest whole number. The Matlab vector means that the first whole should be drilled through the rail aps-pw/2 inches from one end, and each successive hole should be drilled aps inches along. The last four lines are used to round the decimal inches to the nearest 16th of an inch, and to print the results.