%Taylor series Method of higher order for Solving Initial Value Problems
% Input: interval [a,b], initial value x0, step size h
% Output: time steps t, solution y
% Exercise 10.1.3: 
%   Solve x'=x, with IC 
%           x(0)=1
%   on [0,10]
% and compare with the exact solution x(t)=exp(t).
% Use step 1/100 and derivatives up to the 10th.
% Example usage: x=taylor_ex10_1_3([0 10],1,0.01);

function [x,y,t] = taylor_ex10_1_3(int,x0,h)
a = int(1); b = int(2);
x = x0;
t = a;
%plot(t,x,'*','LineWidth',6);
%plot(t,y,'o','LineWidth',3);
n = round((int(2)-int(1))/h);
format long
for k = 1 : n
    xprime = x;
    xsecond = x;
    xtertius  = x;
    xquatro = x; xpentium = x; xsix = x; xsept = x; xoct = x; xneuf = x; xdix = x;
    x = x + h *(xprime + .5*h*(xsecond + (1/3)*h*(xtertius + .25*h*(xquatro ...
        + 1/5*h*( xpentium + 1/6*h*(xsix + 1/7*h*( xsept + 1/8*h*( xoct + 1/9*h*( xneuf + 1/10*h*xdix ))) ))))));
    t = a + k*h;
    y = exp(t);   % exact solution
       
hold all
plot(t,x,'*','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','g',...
                'MarkerSize',1);
plot(t,y,'o','LineWidth',1,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',1);
            
end
text(b,x,'\leftarrow x: 10th order Taylor',...
     'HorizontalAlignment','left') 
text(b,y,'\leftarrow y: exact solution',...
     'HorizontalAlignment','left')
title('Exercise 10.1.3: solution of ODE xprime=x, the exponential e^t')
xlabel('time interval')
end