function [y]=hw2_1_15(x, tol) % given x and tol as input, calculate y=ln(1+x) using Talyor series to within % tolerance tol oldy=x; % initial value of y (just the first term in the series) y=oldy-x^2/2; % add one more term so we can measure tolerance k=2; % we've used 2 terms so far % add another term while tolerance is NOT less than tol while (abs(y-oldy)>tol) k=k+1; % next term oldy=y; % remember the last value y then ... y=oldy + (-1)^(k+1) * x^k / k; % ... add another term % delete the percent sign below if you want to see the iterates of y % disp(y) end x tol disp('check the accuracy (these two results should be close)') disp('actual') disp(log(1+x)) disp('approximate') disp(y)