%computing values for rows in table of Problem 5, homework #1
   % this script file relies on the function file u.m, 
   % where u(x) := xsin(x);

%solution to problem 5, homework 1
% this script generates a table of approximate
% values for u'(1), where u(x) = x*sin(x)
% using the forward, central, and modified
% difference methods, along with the absolute
% error between the true and approximate solutions.
% these values are generated for various values of
% step size h.  the results should show that the error
% is O(h) for the forward, and approx O(h^2) for the
% other two.


%iterate through values of h, save tabular values in a matrix
for i=1:11
  h=.1/(2^(i-1));

  table(i,1) = h;

  %compute actual value of u'(1)
  Du = cos(1) + sin(1);


  %forward difference method at x=1, and error
    table(i,2) = 1/h*(u(1+h) - u(1));
    table(i,3) = Du-table(i,2);

  %central difference method at x=1, and error
    table(i,4) = 1/(2*h)*(u(1+h)-u(1-h));
    table(i,5) = Du - table(i,4);

  %modified difference method at x=1, and error
    table(i,6)= 1/(2*h)*(4*u(1+h)-u(1+2*h)-3*u(1));
    table(i,7) = Du - table(1,6);
  
end


%print table values in scientic notation, showing 16 digits
format long e;
table
