% Engg. Maths for Adv. Studies - Autumn 2019 IIT Dharwad
% Module Numerical Methods Assignment 01
% Sample Solution using Octave - Question 3
% Author - Samarth Raut
% Last modified - 17th Nov. 2019
%**********************************************************

clear;

A=[ 1 2 0 0 0 0 0 0;
    1 4 1 0 0 0 0 0;
    0 1 4 1 0 0 0 0;
    0 0 1 4 1 0 0 0;
    0 0 0 1 4 1 0 0;
    0 0 0 0 1 4 1 0;
    0 0 0 0 0 1 4 1;
    0 0 0 0 0 0 2 1 ];
    
dataTable = ...
[0  0.0     0.12507;
 1  2.0    -0.12828;
 2  4.0    -0.10022;
 3  6.0     0.66500;
 4  8.0     0.66500;
 5 10.0    -0.10022;
 6 12.0    -0.12828;
 7 14.0     0.12507]

% sin function
%dataTable = ...
%[ 1  0.00000   0.00000
%  2 0.44880   0.43388
%  3 0.89760   0.78183
%  4 1.34640   0.97493
%  5 1.79520   0.97493
%  6 2.24399   0.78183
%  7 2.69279   0.43388
%  8 3.14159   0.00000]
 
 h = dataTable(2,2)-dataTable(1,2); % assumption of equispaced points
 v=dataTable(:,3);
 n=size(v,1);
 f = zeros( n,1);
 f(1) = ((-(5/2)*v(1) + 2.0*v(2) + 0.5*v(3)))/h ; 
 f(n) = (((5/2)*v(n) - 2.0*v(n-1) - 0.5*v(n-2)))/h ; 
 for i=2:(n-1)
        f(i) = (3.0*(v(i+1)-v(i-1)))/h;
 end
 f
 df = A\f
 
 %Visualization
 t = dataTable(:,2);
 deltaT=0.4
 t1= t+deltaT;
 v1= v+df*deltaT;
 [t,v,t1,v1]
 
 plot(t,v, '*--');
 hold on;
 plot( [t(1); t1(1)], [v(1); v1(1)], ...
       [t(2); t1(2)], [v(2); v1(2)],...
       [t(3); t1(3)], [v(3); v1(3)],...
       [t(4); t1(4)], [v(4); v1(4)],...
       [t(5); t1(5)], [v(5); v1(5)],...
       [t(6); t1(6)], [v(6); v1(6)],...
       [t(7); t1(7)], [v(7); v1(7)],...
       [t(8); t1(8)], [v(8); v1(8)])
 hold off;
 xlabel("time")
 ylabel("vel")
 title("lines show slope using Pade scheme")