In: Advanced Math
Solve the following initial value problem over the interval from t = 0 to 2 where y(0) = 1 using the following methods.
dy/dt=y*t^2−1.1y
a) Analytical method
b) Euler's method with h=0.5 at t=2
c) Euler's method with h=0.25 at t=2
d) Midpoint method with h=0.5 at t=2
e) Fourth-order Runge-Kutta method with h=0.5 at t=2
f) Display all yor results obtained above on the same graph




%%Matlab code for Euler, Mid Point, RK2 and RK4 method
clear all
close all
%Function for which solution have to do
f=@(t,y) y.*t.^2-1.1.*y;
    h=0.5; % amount of intervals
    fprintf('\nSolution for Euler method using step
size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at
t=0.\n')
    %Euler method
   
%%%%%%%%%%%%%%%        
   
t=0;            
% initial t
   
y=1;            
% initial y
   
t_eval=2;        % at what point
we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t2(1)=t;
    y2(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t2(i+1)=t;
        y2(i+1)=y;
        fprintf('\t at t=%2.2f
value of y(%2.2f)=%f\n',t2(i+1),t2(i+1),y2(i+1))
    end
  
    %Euler method
    %%%%%%%%%%%%%%%
    h=0.25;
    fprintf('\nSolution for Euler method using step
size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at
t=0.\n')
   
t=0;            
% initial t
   
y=1;            
% initial y
   
t_eval=2;        % at what point
we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t3(1)=t;
    y3(1)=y;
    for i=1:n
    %Euler steps
       m=double(f(t,y));
       t=t+h;
       y=y+h*m;
       y3(i+1)=y;
       t3(i+1)=t;
       fprintf('\t at t=%2.2f value
of y(%2.2f)=%f\n',t3(i+1),t3(i+1),y3(i+1))
    end
  
    %RK4 method
    %%%%%%%%%%%%%%%
    h=0.5; % amount of intervals
    fprintf('\nSolution for RK4 method using step
size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at
t=0.\n')
   
t=0;            
% initial t
   
y=1;            
% initial y
   
t_eval=2;        % at what point
we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t4(1)=t;
    y4(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
      
k2=h*double(f((t+h/2),(y+k1/2)));
      
k3=h*double(f((t+h/2),(y+k2/2)));
      
k4=h*double(f((t+h),(y+k3)));
      
dx=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dx;
       t4(i+1)=t;
       y4(i+1)=y;
       fprintf('\t at t=%2.2f value
of y(%2.2f)=%f\n',t4(i+1),t4(i+1),y4(i+1))
    end
  
    %Midpoint method
    h=0.5;
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for Midpoint method using
step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at
t=0.\n')
   
t=0;            
% initial t
   
y=1;            
% initial y
   
t_eval=2;        % at what point
we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t5(1)=t;
    y5(1)=y;
    for i=1:n
    %Midpoint Steps
       k1=h*double(f(t,y));
      
k2=h*double(f((t+h),(y+k1)));
     
       dx=(1/2)*(k1+k2);
       t=t+h;
       y=y+dx;
       t5(i+1)=t;
       y5(i+1)=y;
       fprintf('\t at t=%2.2f value
of y(%2.2f)=%f\n',t5(i+1),t5(i+1),y5(i+1))
    end
  
     %%Exact solution
     syms y(t)
        eqn = diff(y,t) ==
y*t^2-1.1*y;
        cond = y(0) == 1;
        ySol(t) =
dsolve(eqn,cond);
        fprintf('Exact solution
for given ode is y(t)=')
        disp(ySol)
        yy_ext(t)=ySol;
        fprintf('Initial
condition y(0)=1 at t=0.\n')
        for
ii=1:length(t4)
           
y6(ii)=double(yy_ext(t4(ii)));
           
fprintf('\t at t=%2.2f value of
y(%2.2f)=%f\n',t4(ii),t4(ii),y6(ii))
        end
%%Plotting solution using Euler method
figure(1)
hold on
plot(t2,y2,'Linewidth',2)
plot(t3,y3,'Linewidth',2)
plot(t4,y4,'Linewidth',2)
plot(t5,y5,'linewidth',2)
plot(t4,y6,'linewidth',2)
xlabel('t')
ylabel('y(t)')
title('Solution plot y(t) vs. t')
legend('Euler Method','Euler Method','RK4 Method','Midpoint
Method','Exact solution','Location','northwest')
grid on
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%