function fx = maclaurin2(x,Ea_desired) %MACLAURIN approximates log(1+x/1-x) using the MacLaurin series expansion % % (this section is skipped to avoid giving away part of the solution) % % Inputs: % X: a scalar with -1 < X < 1 % EA_DESIRED: a scalar with the desired approximate error % % Output: % FX: a scalar containing the MacLaurin approximation % % Assumptions: % - the input is inside the interval (-1,1) % - the desired approximate error is strictly positive % check whether the input is valid if length(x)>1 error('function only works for scalars'); end if abs(x)>=1 error('abs(x) must be smaller than 1'); end max_iter = 1000; fx = 2*x; x_power = x; for i=2:max_iter x_power = x_power*x*x; new_term = 2/(2*i-1)*x_power; fx = fx+new_term; % check whether we have reached the desired accuracy if (abs(new_term) < abs(Ea_desired)) return; end end % this should only occur if the maximum number of iterations has % been exceeded warning('the approximation does not satisfy the desired accuracy') return;