title: Issues with diff(f,x,n) in MacLaurin series
body: <p>We had an assignment asking us to estimate <code>exp(2)</code> using  <em>MacLaurin series</em> and I've taken it a bit further trying to develop and function where you can input an equation, the value of <code>x</code> you wish to estimate and to how many significant figures you want to have it.</p>
  
  <p>It works well for certain equations, i.e. <code>exp(x)</code>, <code>sin(x)</code>, even <code>exp(x) + 2x</code>, but the second I throw in an higher power on <code>x</code>, say <code>x^2</code> it returns <code>0</code>. </p>
  
  <p>For example, when I call the function as: <code>MacLaurin(x^2,2,1)</code> , it returns <code>0</code> which is not a proper answer. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>% Created by: DarkRiot43
  % Student #: 
  % Date: Sept 12, 2016
  % Course: Numerical Analysis, MTH 510
  
  function [  ] = MacLaurin( func , valueOfx, sigfigs )
  %MACLAURIN Summary of this function goes here
  %   Detailed explanation goes here
  %       inputs: func: a function
  %               sigfigs: the number of significant figures you would like
  %               to have the answer evaluated to.
  clc;
  syms f(x) x ;
  f(x) = func;
  presentApprox = 0;
  previousApprox = 0;
  n = 0;
  % Find the criterion for stopping the iteration as an double not a percent.
  Es = (0.5*10^(2-round(sigfigs)))/100;   
  
  Ea =Es+1; % Ensures Ea is larger than Es to begin
  
  while Ea &gt;= Es
      %differentiate the n'th derivative of the equation f(x) w.r.t. x
      beenDiff = diff(f(x),n); 
      presentApprox = previousApprox + (vpa(subs(beenDiff,x,0))* (((valueOfx)^n)/factorial(n))); %MacLaurin series structure used to evaluate.
      n = n+1; %Counter incrementation
    % Call to function    approxError to determine approximate relative error.
      Ea = approxError(presentApprox,previousApprox); 
      previousApprox = presentApprox; 
      f(x) = beenDiff;      
      % can be used to ensure proper iterating counting of the program
      disp(n); 
  
      disp(f(x)); %shows the n'th derivative function
  
      %check to see if in the approxError function there would have been a
      %by zero error. if so I edit the value (999 was all i could think of?
      % was thinking of using something else but didn't know what to use
      if Ea == 999
          break;
      end
  end
  else
      fprintf('\nStopping criterion used:\t');
      disp(Es*100)
  
  
      fprintf('The estimate generated using MacLaurin Series of ');
      disp(func);
      fprintf('using the value x = %.3f is: \n\n',valueOfx);
      fprintf('%7f',presentApprox);
      fprintf('\nThis was done using %d iterations', n);
  
      if Ea == 0
          fprintf('\nCould not reach the requested stopping criterion.');
  
      end
  
  
  end 
  
  function [Ea] = approxError(presentApprox,previousApprox) 
  %Approximate relative error Function
  % inputs:
  %   presentApprox: type double
  %   previousApprox: type double
  % returns: 
  %   Ea: type double
  %       
  %   Uses two values to determine the approximate relative error w.r.t
  %       eachother
  
      if presentApprox ~= 0
  
          Ea = (presentApprox - previousApprox)/(presentApprox);
  
      elseif presentApprox == 0
  
          disp('Cannot determine the value with more precision as it would   involve dividing by zero!');
          Ea = 999;
  
      end 
  end
  </code></pre>
  
  <p>Please ask if I have been unclear.</p>