% template.m
% This is a template for electronic submission of homework
% assignments for the course MATH 3Q03; use of this template is 
% obligatory
% Email the complete file to the instructor before the
% due date; before submission make sure that the code executes
% correctly in MATLAB

% Written by Bartosz Protas, September 2016

function hw4
close all, clear all


% Student information
  
disp('  ------------   Student Information  ------------  ')  

% Please enter your information here
Name = 'Johny';
Surname = 'Good';
ID = 123456789;

fprintf('  Student:  %s %s (ID: %d) \n', Name, Surname, ID);



disp('  ------------------------------------------------  ')  
disp('  ')
disp('  ')
disp('  ')
disp('  ')


disp('  ----------   Solution to Question #1  ----------  ')  
disp('  ')
% Insert here your solution to Question #1
% (if you define your own functions, they may appear 
%  at the end of the file)

d = domain(-1,1);
x = chebfun('x'); f = 1./(1+25*x.^2);


% part (a) 
figure(1)
df = f;
for i=1:5
  df = diff(df);
  semilogy(abs(df)), hold on
  Mn(i) = norm(df,inf);
end;
xlabel('x'), ylabel('|f^{(n)}(x)|');

% part (b)
figure(2)
semilogy(Mn,'-x')
xlabel('n'), ylabel('M_n');
disp('  Since log(E_max) grows proportionally to n, we have E_max ~ exp(n).');





disp('  ')
disp('  ------------------------------------------------  ')  
disp('  ')
disp('  ')
disp('  ')
disp('  ')


disp('  ----------   Solution to Question #2  ----------  ')  
disp('  ')
% Insert here your solution to Question #2
% (if you define your own functions, they may appear 
%  at the end of the file)


figure(3)
alpha = 5.0;

while ( alpha > 0 )
  fa = 1./(1+alpha^2*x.^2);
  ee = []; nn = 2:2:50;
  for np = nn
    s = linspace(-1,1,np); pa = interp1(s,fa,d);
    ee = [ee norm(fa-pa,inf)];
  end
  semilogy(nn,ee,'.-'), hold on, grid on, 

  alpha = alpha - 0.5;
end;
xlabel('n'), ylabel('E_{max}')

disp('    ');
disp('  (a) Convergence is restored for sufficiently small alpha ...');
disp('    ');
disp('  (b) For large alpha (alpha >= 2.0) there are only truncation errors.');
disp(['      For small alpha and large n round-off errors increasing' ...
      ' with n become evident.']);


disp('  ')
disp('  ------------------------------------------------  ')  
disp('  ')
disp('  ')
disp('  ')
disp('  ')

disp('  ')
disp('  ------------------------------------------------  ')  
disp('  ')
disp('  ')
disp('  ')
disp('  ')


disp('  ----------   Solution to Question #3  ----------  ')  
disp('  ')
% Insert here your solution to Question #3
% (if you define your own functions, they may appear 
%  at the end of the file)

T = (1:length(Mn));
S = Mn;

% Constructing and solving normal equations;
% The approximating function: S = a exp( b T )
a11 = sum(T.^2); a12 = sum(T); a21 = sum(T); a22 = sum(ones(1,length(T)));
A = [ a11,a12; a21,a22]; 
b1 = sum(T.*log(S)); b2 = sum(log(S)); b = [ b1; b2 ]; 
c = A \ b  % 
a = exp(c(2));
b = c(1) ;
fprintf('  Least-squares exponential fit: %.4f*exp(%.4f*n) \n',a, b);


% plotting the fit on figure(2)
t = T(1) : 0.01 : T(end);
s = a*exp(b*t);
figure(2);
hold on;
plot(t,s,'--');
legend('M_n data','least-squares fit ae^{bn}');

E = sum((log(S)-c(1)*T-c(2)).^2);
fprintf('  Total square error of linear regression fit E=%f \n',E);


% Please copy as many headers as necessary
