The Natick software company MathWorks has paid nearly $18 million for a 115,000-square-foot office building next to one of its two campuses in town.