% This is a modified version of getexp.m, illustrating: % * formatted output (via "fprintf") % * multiple output parameters % * techniques for controlling loops %--------------------------------------------------------- function [y,n] = getexp_new(x) %--------------------------------- % Use Taylor series to evaluate % exp(x). Continue adding terms % until there is no change in the % answer. %--------------------------------- % Usage: [y,n] = getexp(x) %--------------------------------- y = 1; term = 1; n = 1; term = term*x/n; while ( (y+term)~=y ) & (...); y = y + term; n = n + 1; % if (...); break; end; term = term*x/n; end; fprintf('x = %8.4f y = %10.4e error = %8.1e\n',x,y,y-exp(x))