# data[].attributes.attachment_link.readable_text
function dydt = ODEfunctions(t,y)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here

dydt = -0.06*sqrt(y);


end


# data[].attributes.attachment_link.readable_text


clear all
clc

%% Setup the input parameters

t0 = 0;
y0 = 3;

tfinal = 65;
h = 0.01;

N = (tfinal-t0)/h;

%% Initiliaze the solution values of y and t

y = zeros(N+1,1);
y(1) = 3; 
t = t0:h:tfinal;

% %% Perform Euler's method on estimating the water level in tank (m)
% 
% for i =1:N
%     
%     if (y(i) < 0)
%         i_final=i;
%         break
%     else
%     y(i+1) = y(i) + h*ODEfunctions(t(i),y(i));
%     end
% end

% %% Perform Heuns's method on estimating the water level in tank (m)
% 
% for i =1:N
%     
%     if (y(i) < 0)
%         i_final=i;
%         break
%     else
%         k1 = ODEfunctions(t(i),y(i));
%         k2 = ODEfunctions(t(i)+ h,y(i)+k1*h);
%         
%     y(i+1) = y(i) + h/2*(k1+k2);
%     end
% end

%% Perform RK 4th order method on estimating the water level in tank (m)

for i =1:N
    
    if (y(i) < 0)
        i_final=i;
        break
    else
        k1 = ODEfunctions(t(i),y(i));
        k2 = ODEfunctions(t(i)+ h/2,y(i)+k1*h/2);
        k3 = ODEfunctions(t(i)+ h/2,y(i)+k2*h/2);
        k4 = ODEfunctions(t(i)+ h,y(i)+k3*h);
        
    y(i+1) = y(i) + 1/6*(k1+2*k2+2*k3+k4)*h;
    end
end

t_actual = t0 +(i_final-1)*h;

yexact = ((2*sqrt(3)-0.06*t)/2).^2;



plot(t,y,'r', t, yexact,'b')
title(['The time to drain the water tank is =', num2str(t_actual),'minutes'])
legend(['Estimated solution by RK3 Method with h =', num2str(h)],'Exact solution')









# data[].attributes.headline
Lab Sheet 8 : Ordinary Differential Equation

# data[].attributes.subject
Lab Sheet 8 : Ordinary Differential Equation

# data[].attributes.headline
Lecture 3: Runge-Kutta 4th Order with MATLAB

# data[].attributes.subject
Lecture 3: Runge-Kutta 4th Order with MATLAB

# data[].attributes.attachment_link.authors[].name
MATLAB Programming for Numerical Computation

# data[].attributes.headline
Lecture 2 : Runge-Kutta 2nd Order with MATLAB

# data[].attributes.subject
Lecture 2 : Runge-Kutta 2nd Order with MATLAB

# data[].attributes.attachment_link.authors[].name
MATLAB Programming for Numerical Computation

# data[].attributes.headline
Lecture 1 : Introduction to Euler's Method with MATLAB

# data[].attributes.subject
Lecture 1 : Introduction to Euler's Method with MATLAB

# data[].attributes.attachment_link.title
MATLAB Programming Tutorial #33 Intro to ODE & Euler's Method