classdef Taylor % this class models a degree-3 Taylor series with remainder, % a1 + a2 * t + a3 * t^2 + O(t^3) properties coeffs %length-3 vector of coefficients end methods function obj = Taylor(v) % so-called "constructor method". % This construct an object of this type starting from a given vector obj.coeffs = v; end function c = mtimes(a, b) % this is the function that gets called when Matlab encounters % a product that involves an object of type Taylor c = Taylor([a.coeffs(1)*b.coeffs(1), ... a.coeffs(2)*b.coeffs(1) + a.coeffs(1)*b.coeffs(2), ... a.coeffs(3)*b.coeffs(1) + a.coeffs(2)*b.coeffs(2) + a.coeffs(1)*b.coeffs(3) ... ]); end function c = plus(a, b) % this is the function that gets called when Matlab encounters % a sum that involves an object of type Taylor % we add special cases for Taylor + number and number + Taylor if isa(b, 'double') b = Taylor([b 0 0]); end if isa(a, 'double') a = Taylor([a 0 0]); end c = Taylor(a.coeffs + b.coeffs); end end end