You are on page 1of 11

CH E 374 – Assignment # 5 Fall 2007

Problem 1

The equation of motion of a simple pendulum, subject to damping and external torque, is given by:

d 2θ dθ
ml 2 2
+c + mgl sin θ = M (t )
dt dt
Where m = mass of the bob, l = length, c = damping factor, g = gravitational acceleration, M(t) =
external torque, θ = angular deflection and t = time.
Express this equation as a system of linear first order equations
Solution

Let
θ1 = θ
θ2 = θ ′
Therefore
⎡ θ1′ = θ 2 ⎤
⎡θ ⎤ ⎢ ⎥
θ = ⎢ 1⎥ θ′ =
⎣θ 2 ⎦ ⎢θ 2′ = θ ′′ = M (t ) − cθ 2 − 2mgl sin(θ1 ) ⎥
⎣ ml ⎦

Problem 2
{THIS WAS SUPPOSED TO BE DONE BY HAND - see page zzz}
Find the solution of the equation
y ′ = 2 − 3 x + 4 y; y (0) = 1
In the interval 0 ≤ x ≤ 1
(a) Using Euler’s method with a step size of 0.5
(b) Using Heun’s method with a step size of 0.5
(c) Using 4th Order Runge Kutta Method with a step size of 0.5.

%%%%%%
{MATLAB VERSION}

%% function to be solved
function F = p3fun(x,y)
F = 2 - 3*x + 4*y;
end

function [x,y] = solver ( f , txspan , y0, n )


% Solve y' = f(x,y) with initial conditions y(a) = y0
% f can be an inline function or m-file
% txspan interval of interest
% txspan(1) start point
% txspan(2) end point
% Calculate the interval size (h)
a = txspan(1)
b = txspan(2)
h = (b-a)/n
1
CH E 374 – Assignment # 5 Fall 2007

% calculate the initial values


x(1) = a;
y(1) = y0;

% calculate the subsequent values


% set the vector of x values from x = x+h to b
x = a : h : b;

%HEUN
for i= 1: n
K1 = feval(f, x(i), y(i));
K2 = feval(f, x(i)+0.75*h, y(i)+0.75*h*K1);
y(i+1) = y(i) + h * 1/3 * (K1 + 2*K2);
end

%EULER
for i= 1: n
y(i+1) = y(i) + h * feval(f, x(i), y(i));
end
%RK4
while x < xStop
i = i + 1;
h = min(h,xStop - x);
K1 = h*feval(dEqs,x,y); % h*slope at the start point
K2 = h*feval(dEqs,x + h/2,y + K1/2); % h*slope at the midpoint
K3 = h*feval(dEqs,x + h/2,y + K2/2); % h*slope at the midpoint
K4 = h*feval(dEqs,x+h,y + K3); % h*slope at the endpoint
% advance the solution
y = y + (K1 + 2*K2 + 2*K3 + K4)/6;
% increment the step
x = x + h;
% Store current soln.
xSol(i) = x;
ySol(i,:) = y;
end

end

Coord: 0.000000 0.500000 1.000000


(a) EULER: 1.000000 4.000000 12.250000
(b) HEUN : 1.000000 6.625000 33.250000
(c ) RK4 : 1.000000 9.250000 64.750000

2
CH E 374 – Assignment # 5 Fall 2007
Part B: { this part may be done in groups}

MATLAB

Problem 3 { This will be done in the seminar}


Find the solution of the equation using Matlab
y ′ = 2 − 3 x + 4 y; y (0) = 1
n the interval 0 ≤ x ≤ 1

Compare the following solutions


Using Euler’s method with step size of 0.1, 0.05 and 0.01
Using 4th order Runge Kutta Method with a stepsize of 0.05

(USING the same codes above with different step sizes.)

80

70

Euler0.1
60 Euler0.05
Euler0.01
50 RK4

40
y

30

20

10

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Thus a better solution is obtained by using higher order method or using much smaller steps in a lower
order method (Euler).

3
CH E 374 – Assignment # 5 Fall 2007
Problem 4: Non-Isothermal Plug flow Reactor

A Plug flow reactor is used to oxidize SO2 to SO3. The following reactor equations have been
developed (With radial and axial dispersion neglected, Young & Finlayson, 1973):

dX
= −50ℜ
dz
(1)
dT
= −4.1(T − Tsurr ) + 1.02 × 104 ℜ
dz
Where the reaction rate is:
X [1 − 0.167(1 − X ) ] − 2.2(1 − X ) / K eq
1/ 2

ℜ= (2)
[ k1 + k2 (1 − X )]
2

and
ln k1 = −14.96 + 11070 / T
ln k2 = −1.331 + 2331/ T (3)
ln K eq = −11.02 + 11570 / T
With parameters: Tsurr = 673.2, T (0) = 673.2, X (0) = 1 . Where X is the concentration of SO2 divided by
the inlet concentration, 1-X is the fractional conversion, and T is the temperature in K.

(a) write a function (an m-file) to evaluate the right hand side of the above differential equations
(b) Test the m-file with y(1) = 0.2 and y(2) = 573.2
(c) Write a driver program to solve the function in (a). The driver code should set the initial values
and the total interval of integration [0 1].The driver should also make plots, one for the
fractional conversion as a function of dimensionless axial distance and another for the
temperature variation along the axial distance.

(a) matlab program to evaluate rhs


% function to evaluate the rhs
function F=myode1(z,y)
%
Tsurr=673.2; % surrounding temperature
X=y(1); % set the X variable
T=y(2); % set the T variable

% rate constants
k1=exp(-14.96+(11070/T));
k2=exp(-1.331+(2331/T));
% equilibrium constant
Keq=exp(-11.02+(11570/T));
% reaction rate
R=((X*(1-0.167*(1-X))^(0.5))-(2.2*(1-X)/Keq))/((k1+k2*(1-X))^(2));

F(1)=-50*R;
F(2)=-4.1*(T-Tsurr)+(1.02*10^4)*R;

F=F'; % make sure F is a column


4
CH E 374 – Assignment # 5 Fall 2007

End

test
>> y0 = [1 573.2];
>> myode1(0.2,y0)

ans =

-0.0083
411.6903

(c ) Main driver program

%Driver Program to Solve function in (a)


%set initial values and total interval of integration to [0,1]
%
% set the initial conditions
y0=[1 673.2];

% set the span for the independent vector


txspan=[0 1];

% call the ODE solver


[z,y]=ode45(@myode1,txspan,y0);

%PLOT fractional conversion vs. axial distance


plot(z,(1-y(:,1)),'*-')
xlabel('Length');
ylabel('Fractional Conversion');
%PLOT temperature variation vs axial distance
figure
plot(z,y(:,2),'+-')
xlabel('Length');
ylabel('Temperature Variation');

5
CH E 374 – Assignment # 5 Fall 2007

750

740

730
Temperature Variation

720

710

700

690

680

670
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Length

0.8

0.7

0.6
Fractional Conversion

0.5

0.4

0.3

0.2

0.1

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Length

6
CH E 374 – Assignment # 5 Fall 2007
Problem # 5

The velocity distribution of a fluid in a straight uniform pipe is given by the Poisson equation:

∂ 2U ∂ 2U
+ = −4
∂x 2 ∂y 2
Where U is the axial dimensionless velocity. The following boundary conditions apply:

U ( x, y = 0) = 0
U ( x, y = H ) = 0
U ( x = 0, y ) = 1.5 y ( H − y)
Find the velocity distribution for a channel with L=10 and H=1 using centered finite difference
approximation. Use Δx = Δy = 1/10 . Produce a plot of y as a function of U for x = 5 and a surface plot
of the velocity distribution in the channel.

%%%%%%%%%%%%%%%%%%%%%
Number of nodal points in the x-direction, nx
dx =dy=0.1
nx= 10/0.1 +1 = 101
Number of nodal points in the x-direction, ny
ny = 1/0.1 + 1 = 11

total number of nodes = nx*ny = 101*11 = 1111

Boundary conditions:
U (i, j = 0) = 0
U (i, j = ny ) = 0
U (i = 0, j ) = 1.5( j * dy )( H − ( j * dy ))
U (i = nx, j ) = U (i = nx − 1, j )

7
CH E 374 – Assignment # 5 Fall 2007
% ************************************************
% PROBLEM 5
% Centered in space
%d2U_dx2=(U_(i+1,j )-2 U_(i,j) +U_(i-1,j ))/ dx^2
%d2U_dy2=(U_( i ,j+1) -2 U_(i,j) +U_(i ,j-1))/dy^2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L = 10;
H = 1;
dx = 1/10; dx2 = dx^2;
dy = 1/10; dy2 = dy^2;
ys = [0:dy:H];
nx = L/dx+1;
ny = H/dy+1;
vecD = -2.0 / dx2 -2.0 / dy2;
vecLx = 1.0 / dx2;
vecLy = 1.0 / dy2;

n = nx*ny;
A = eye( n );
b = -4*ones( n,1 );

% BCs
U_yH = 0;
U_y0 = 0;
U_x0 = 1.5;

% Equations for inner nodes


% -----------------------------------
for j = nx+1:n-nx
for i=j:j+1
A( i,i ) = vecD;
A( i, i-1 ) = vecLx;
A( i, i+1 ) = vecLx;
A( i, i + nx )= vecLy;
A( i, i -nx )= vecLy;
end
end
% Outlet disp(’Outlet’)
for j = 2*nx:nx:n-nx
for i=j:j+1
A( i,i ) = vecD;
A( i, i-1 ) = 2*vecLx;
A( i, i + nx )= vecLy;
A( i, i -nx )= vecLy;
end
end
% Inlet
disp('Inlet')
j=1;
8
CH E 374 – Assignment # 5 Fall 2007
for i=nx+1:nx:n-nx+1
A(i,:) =0.0;A(i, i) =1.0;
j = j+1;
b( i ) = U_x0*( ys(j)*(H-ys(j) ) );
end
disp('Lower') % Upper and lower walls
for i=1:nx
A(i,:) = 0.0;
A(i, i) =1;
b( i ) = U_y0;
end
idx = n-nx+1; disp('Upper')
for i= idx:n
A(i,:) = 0.0;
A(i, i) =1;
b( i ) = U_yH;
end

% Obtain the solution using \ operator


% -----------------------------------
U = A \b ;

% Plot the solution


% ------------------------------------
UU = []; % holding vector
n = nx*ny;

for i = 1:nx:n
UU = [ UU; U(i:i+nx-1)' ];
end

fprintf('Solution rearranged with space profile \n')


disp(UU)

figure;
surf( UU )
view( -10, 30)
title( 'Velocity distribution' )
xlabel('x')
ylabel('y')
zlabel('U')
figure;
plot( UU( :,5/dx), ys, 'o-b' )
title('Velocity profile at x=5')
xlabel(' U '); ylabel(' y'); grid on
figure;
plot( UU, ys, 'o-b' )

9
CH E 374 – Assignment # 5 Fall 2007

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
-0.1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9

Velocity profile at x=5


1

0.9

0.8

0.7

0.6

0.5
y

0.4

0.3

0.2

0.1

0
-0.05 0 0.05 0.1 0.15 0.2 0.25 0.3
U

10
CH E 374 – Assignment # 5 Fall 2007

Velocity distribution
1

0.8

0.6

0.4
U

0.2

-0.2
15

10

100 120
y 0 60 80
20 40
0
x

11

You might also like