% diff_arith.m % Demonstrates how a simple differentiation arithmetic % can be implemented in Matlab, and applies it to the % computation of (x-3)^2/(2x-5). function diff_arith format compact x = var(2) c3 = const(3) c2 = const(2) c5 = const(5) numer = pow( sub(x,c3), 2 ) denom = sub( mul(c2,x), c5 ) y = div( numer, denom ) end function r = add(u,v) r = u + v; end function r = sub(u,v) r = u - v; end function r = mul(u,v) r(1) = u(1)*v(1); r(2) = u(1)*v(2) + u(2)*v(1); end function r = div(u,v) r(1) = u(1)/v(1); r(2) = ( u(2)*v(1) - u(1)*v(2) ) / v(1)^2; end function r = pow(u,n) r(1) = u(1)^n; r(2) = n*u(1)^(n-1)*u(2); end function r = const(u) r = [u,0]; end function r = var(u) r = [u,1]; end