Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

% ii) By numerical solution of ODE

% [MATLAB filename: ODE_45.m]


%============using ode45 ODE solver===========%

% dh/dt = - (100*r^2)*sqrt(2*g*h) / (2*R*h - h^2 )


%from given ODE, we can see that Water Level (h) is dependent on time (t)
%But it is also kind of implicit function which means after integrating, we can't write h in
%term of t only.
%So, write (after inserting R,r ang g=9.81), dh/dt = - 0.040*sqrt(19.62*h) / (2*R*h - h^2 )
% h' = -k * sqrt(y) / (8*h - h^2 ), where k = -0.040*sqrt(19.62)

clear , clc , close all ;


R=4; r = 0.020 ; % Given
tEnd = 270 ; %suppose time required is 290sec.
delt = 0.05 ;
tspan = [0 : delt : tEnd ]' ;
h0 = 6.50 ;
k = -100*(r^2)*sqrt(19.62) ;
[t, h] = ode45( @(t, h) (k*sqrt(h) / (2*R*h - h^2) ) , tspan, h0 ) ;
figure(1);
plot(t, h, 'LineWidth', 1.5);

title(' t vs h (using ode45) ');


xlabel(' Time (t) (seconds) ');
ylabel(' Water Level (h) in tank (meters) ');
grid on
%=======================END======================%

% ii) By numerical solution of ODE


% [MATLAB filename: Eulers_implicit.m]
%============User-defined=========
%========Euler's implicit method======

%============START=============%

%from eq.3, and replacing h with y...


%f = 2.65767*t + 80*y^(3/2) - 6*y^(5/2) - 679.44435 ;

clear , close all


clc ;
f = (@(y, t) 2.65767*t + 80*y^(3/2) - 6*y^(5/2) - 679.44435 ) ; %eq. (3) derived from given
ODE

%initial conditions
t0 = 0 ; y0 = 6.5 ;

tEnd = 260 ; %suppose it takes 270sec to drain


upto 0.5
delx = 0.50 ; % t step size
N = ( tEnd - t0 ) / delx ;

T = [ 0 : delx : tEnd ]' ; %creating time vector

Y = zeros (N+1, 1) ; %creating solution matrix Y


Y(1) = y0 ; %Initial condition

for i = 1:N
t = T(i) + delx;
y = fsolve ( @(y) y - Y(i) - delx*( f(y, t) ) , Y(i) ) ; %Euler's imlicit backward formula

clc ; %Clear fsolve messages.


%fsolve is very good tool for solving non-linear equations.

T(i+1) = t ; %Update values


Y(i+1) = y ;

end

plot(T, Y, 'LineWidth',1.5); %plot t and h


axis ([0 270 -1 8 ])
hold on;
title(' t vs h (using Euler implicit method)');
xlabel(' Time (t) (seconds) ');
ylabel(' Water Level (h) in tank (meters) ');
grid on

%============END=============%
%====INTERPOLATION of 2nd-Order POLYNOMIAL (using Newton's
Interpolation Method)=======
%[MATLAB filename: Interpolation.m]

% 1) First I constructed Quadratic Polynomial using 3 data points (x-y coordinates) from
% Euler Implicit Method or from ODE45 plots of given ODE. Although, both methods...
% produce almost plots but in our case, Euler Implicit Method is more accurate).
%
% 2) Then using this Polynomial’s graph, I selected 3 points on the graph to find Inverse
% Interpolating Polynomial.
%
% 3) Then used this to calculate root which is the REQUIRED TIME t

%To select proper data points, I chose Data Cursor in the Figure Toolbar from figure
window.

%==============START=================%

clear, clc ;
%Selected data points from the graph of Interpolted Polynomial (Figure 3 )
x0 = 250 ;
y0 = 0.333 ;

x = [ 237.5 221.5] ; % x1 and x2


y = [ 0.7413 1.155] ; % y1 and y2

f0 =y0 ; %b0

%==== Newton's Interpolation formula=======


i = 1 ; %index

f01 = ( y(i) - y0 ) / ( x(i) - x0 ) ; %eq.(b1)

f12 = ( y(i+1) - y(i) ) / ( x(i+1) - x(i) ) ; %put in eq.(b2)

f012 = ( f12 - f01 ) / ( x(i+1) - x0) ; %eq.(b2)

P = @(t) f0 + f01 *(t - x0) + f012 *(t - x0)*(t - x(i)) ;

t0 = 120 ; %after visulizing graph, plotting from


200sec and 270
tEnd = 270 ; %suppose it takes 270sec to drain
upto 0.5
delx = 0.25 ; % t step size
N = ( tEnd - t0 ) / delx ;
T = [ t0 : delx : tEnd ]' ; %creating time vector
Y = zeros(N, 1);
Y(1) = P(t0) ; %Starting ponit.
for k = 1:N

t = T(k) + delx;

Y(k+1) = P(t) ; %put t into polynomial P.

T(k+1) = t ;

end

figure(2);
plot(T, Y, 'LineWidth', 1.5);
title(' interpolated Polynomial graph t versus h)');
xlabel(' Time (t) (seconds) ');
ylabel(' Water Level (h) in tank (meters) ');
grid on

%==============END=================%

%=======REVERSE INTERPOLATION========
% Root finding of the Polynomial %
%===============START=================
%[Matlab filename: reverse_interpolation.m ]

clear, clc ;
%Selected data points from the graph of Interpolted Polynomial (Figure 5 )
y0 = 0.1297 ;
x0 = 255.5 ;
y = [0.3772 0.6886] ; % y1 and y2
x = [248.8 239.3] ; % x1 and x2

f0 =y0 ; %b0

%=========Newton's Interpolation formula==============


i = 1 ; %index

b0 = ( y(i) - y0 ) / ( x(i) - x0 ) ; %eq.(b1)

b1 = ( y(i+1) - y(i) ) / ( x(i+1) - x(i) ) ; %put in eq.(b2)

b2 = ( b1 - b0 ) / ( x(i+1) - x0) ; %eq.(b2)

%Constructing Polynomial in terms of t.


P = @(t) -0.5 + f0 + b0 *(t - x0) + b2 *(t - x0)*(t - x(i)) ; %Note: P = f0 + b0 *(t - x0) + b2 *(t -
x0)*(t - x(i)) == 0.5 (which is our desired water level.)

%Now using fzero solver, it'll give the root which will be required time,

xo = [130 270] ; %function must change signs for provided


interval otherwise fzero doesn't work.
Req_t = fzero(P, xo) ; %where P is function and xo is vector for which
function P changes signs.

fprintf(' \n Required time ( t ) = %.2f seconds\n' , Req_t);

%==============END================

%============PART B(1) using fzero=============%


%[Matlab filename: reverse_interpolation.m ]
% fzero uses bisection method and interpolation techniques to calculate root
%of a function where it crosses zero.
%If the interval for which function changes signs, is unknown, we can plot
%the function and then the chose the interval

%========================================%

%Using Eq.3 from part (a)


%f = 2.65767*t + 80*h^(3/2) - 6*h^(5/2) - 679.44435 ;

%===============START===================%
clear; clc
%given time (in seconds) at which h is to be found.
t = 60;
fun = (@(h) 2.65767*t + 80*h.^(3/2) - 6*h.^(5/2) - 679.44435 ) ; %eq. (3) derived from given
ODE

fplot(fun, [0 8]); %plot function to find interval


grid on
hold on
title(' Please select the curve point above and below zero line. ')
xlabel(' h (meters) ');
[x1, y1 ] = ginput(1) ; %get selected coordinates from mouse click.
plot(x1, y1, 'og'); %mark green point on the graph
[x2, y2 ] = ginput(1) ; %get selected coordinates from mouse click.
plot(x2, y2, 'or'); %mark red point on the graph

intervalX = [x1 x2];


pause(1); hold off; close all %close figure after 1sec.
options = optimset('Display','iter'); % To show iterations and function values

[x ,fval ] = fzero(fun,intervalX,options) ;

fprintf(' \n Water Level ( h ) at t=60 is h = %.2f m \n\n' , x);


%===============END=================%

%[ MATLAB filename: partC.m ]

% r = sqrt ( 1/(100*sqrt(19.62)*t )*(( (14/3)*R^(5/2) -4/3*R^(3/2)) - (2/5*(2*R)^(5/2)) - 23.515 ) )


;
%============START===========%
clc ; clear ;
t = 120.0 ;
N = 100 ;
R=3; %R = [3 5] given.
Rend = 5 ;
delx = (Rend - R)/N ;
R = linspace(3, 5, N);
rmatrix = zeros(1, N) ;

for i=1:N
%r as a function of tank radius for desired flow rate obtained using
%t0 and tf and related hi and hf
r = sqrt ( (5*(2-4*sqrt(2))*(R(i))^(5/2) - 3*(2-8*sqrt(2) )*(R(i))^(5/2) ) /(t*100*66.4417) ) ;
rmatrix(i) = real(r) ;
end

plot( rmatrix,R, 'LineWidth', 2) %plot R vs r


%axis( [3 5 0.001 0.02]); % same limits as of R and r
given in equation
title( ' Possible combinations/pairs of orifice radius r and spherical tank radius R');
xlabel('Orifice radius r (meters)');
ylabel( 'Sphere Tank Radius R (meters)');
grid on
hold on

%============END===========%

%===========PART C====================%

%[ MATLAB filename: partC.m ]


% r = sqrt ( 1/(100*sqrt(19.62)*t )*(( (14/3)*R^(5/2) -4/3*R^(3/2)) - (2/5*(2*R)^(5/2)) - 23.515 ) )
;

%============START===========%
clc ; clear ;
t = 120.0 ;
N = 100 ;
R=3; %R = [3 5] given.
Rend = 5 ;
delx = (Rend - R)/N ;
R = linspace(3, 5, N);
rmatrix = zeros(1, N) ;

for i=1:N
%r as a function of tank radius for desired flow rate obtained using
%t0 and tf and related hi and hf
r = sqrt ( (5*(2-4*sqrt(2))*(R(i))^(5/2) - 3*(2-8*sqrt(2) )*(R(i))^(5/2) ) /(t*100*66.4417) ) ;
rmatrix(i) = real(r) ;

end

plot( rmatrix,R, 'LineWidth', 2) %plot R vs r


%axis( [3 5 0.001 0.02]); % same limits as of R and r
given in equation
title( ' Possible combinations/pairs of orifice radius r and spherical tank radius R');
xlabel('Orifice radius r (meters)');
ylabel( 'Sphere Tank Radius R (meters)');
grid on
hold on

%============END===========%

%============PART B(2) Numerical Solution==========%


%========Using Euler's implicit method========%
%[MATLAB filename: partB2.m]
%f = 2.65767*t + 80*h^(3/2) - 6*h^(5/2) - 679.44435 ;
%replacing h with y...
%f = 2.65767*t + 80*y^(3/2) - 6*y^(5/2) - 679.44435 ;

%============START==============%

clear , close all


clc ;
f = (@(y, t) 2.65767*t + 80*y^(3/2) - 6*y^(5/2) - 679.44435 ) ; %eq. (3) derived from given
ODE

%initial conditions
t0 = 0 ; y0 = 6.5 ;

tEnd = 100 ; %our end time is 60sec.


delx = 1 ; % t step size
N = ( tEnd - t0 ) / delx ;

T = [ 0 : delx : tEnd ]' ; %creating time vector

Y = zeros (N+1, 1) ; %creating solution matrix Y


Y(1) = y0 ; %Initial condition

for i = 1:N
t = T(i) + delx;
y = fsolve ( @(y) y - Y(i) - delx*( f(y, t) ) , Y(i) ) ; %Euler's imlicit backward formula

clc ; %Clear fsolve output messages.


%fsolve is very good tool for solving non-linear equations.

T(i+1) = t ;

if t == 60.0
Req_h = y ;
Time_t = t;
plot(t, y, 'or', 'MarkerSize', 6, 'LineWidth', 2); %mark red
point on the graph
end

Y(i+1) = y ;
end
hold on;
plot(T, Y, 'LineWidth',1.5); %plot t and h

title(' t vs h (using Euler implicit method)');


xlabel(' Time (t) (seconds) ');
ylabel(' Water Level (h) in tank (meters) ');
grid on
%print water level at 60sec.
fprintf(' \nOUTPUT: for t=%.2f, h=%.2f \n' ,Time_t, Req_h);
%============END=============%

You might also like