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

assignment4-octave-template

June 1, 2017

1 CEIC3000 Assignment 4
In [1]: % Name: Le Ngoc Minh Dinh
% zID: z5054498

The analytic parts of the assignment can also be done within this template. Add additional
cells using the "+" toolbar button and change their type to "Markdown" using the dropdown list
on the toolbar. Double click on the Markdown cells to edit their contents; "run" the cell to turn it
into prettily formatted text; Help Markdown for information about some of the extra things you
can do in typesetting maths or formatting text in markdown.
If you answer the analytic parts outside this Jupyter notebook, please delete the (ir)relevant
cells to make marking easier (including this one!).

In [2]: pkg load odepkg

1.1 Question 1 template


1.1.1 Part (d)
In [3]: % Parameters
k = 0.8; % drag coefficient
tspan = linspace(0,10,1000);

In [4]: % Original ODE


dxdt = @(t, x) [x(2);
-sin(x(1)) - k*x(2)];
% Jacobian matrix A at equilibrium points
A = @(xe) [0, 1;
-cos(xe(1)), -k];
% Linearised ODE
dxdtl = @(t, x, A, xe) A * (x - xe);

In [5]: % At (0,0)
xe1 = [0;0];
A1 = A(xe1);
[t1,x1] = ode45(@(t,x) dxdtl(t,x,A1,xe1), tspan, [-pi/16,1]);
[t2,x2] = ode45(@(t,x) dxdtl(t,x,A1,xe1), tspan, [-pi/16,4]);
[t3,x3] = ode45(@(t,x) dxdtl(t,x,A1,xe1), tspan, [-pi/16,9]);

1
% Plotting
plot(x1(:,1), x1(:,2), x2(:,1), x2(:,2), x3(:,1), x3(:,2))
title('Trajectory behaviour at (0,0)')
xlabel('State x_1')
ylabel('State x_2')
legend('(-\pi/16,1)','(-\pi/16,4)','(-\pi/16,9)','Location','west')
xlim([-10 10])
ylim([-10 10])
axis('square')

Out[5]:

In [6]: % At (pi,0)
xe2 = [pi;0];
A2 = A(xe2);
[t4,x4] = ode45(@(t,x) dxdtl(t,x,A2,xe2), tspan, [-pi/16,1]);
[t5,x5] = ode45(@(t,x) dxdtl(t,x,A2,xe2), tspan, [-pi/16,4]);
[t6,x6] = ode45(@(t,x) dxdtl(t,x,A2,xe2), tspan, [-pi/16,9]);

% Identify eigenvalues and eigenvectors

2
[V,D] = eig(A2);

% Plotting
figure
hold on
plot(x4(:,1), x4(:,2), x5(:,1), x5(:,2), x6(:,1), x6(:,2))
title('Trajectory behaviour at (\pi,0)')
xlabel('State x_1')
ylabel('State x_2')
legend('(-\pi/16,1)','(-\pi/16,4)','(-\pi/16,9)','Location','west')
xlim([-10 10])
ylim([-10 10])
axis('square')

% Adding separatrices
x1r = linspace(-15,15,15);
plot(x1r + pi, V(2,1)/V(1,1) * x1r, 'r--')
plot(x1r + pi, V(2,2)/V(1,2) * x1r, 'r--')

Out[6]:

3
1.1.2 Part (e)
Hint: select a set of initial conditions that is along the top and bottom of your region of interest
(and exceeding the ROI on the left and right), such as x0 = [nπ/4, ±4] for n ∈ [−8, 20].

In [8]: figure
hold on
title('Rigid pendulum phase portrait')
xlabel('State x_1')
ylabel('State x_2')
xlim([-pi 4*pi])
ylim([-4 4])

n_traces = 100;
x0s = zeros(n_traces,2);
x0s(1:2:end-1,1) = linspace(-2*pi,5*pi,n_traces/2);
x0s(2:2:end,1) = x0s(1:2:end-1,1);
x0s(1:2:end-1,2) = -4;
x0s(2:2:end,2) = 4;

for i = 1:n_traces
x0 = x0s(i,:);
[t,x] = ode45(dxdt, tspan, x0);
plot(x(:,1),x(:,2),'b-')
end

% Adding trajectory arrows


grid1 = linspace(-2*pi,5*pi,20);
grid2 = linspace(-4,4,20);
[X,Y] = meshgrid(grid1,grid2);
U = zeros(size(X));
V = zeros(size(Y));

for j = 1:numel(X)
f = dxdt(tspan, [X(j);Y(j)]);
U(j) = f(1);
V(j) = f(2);
end
quiver(X,Y,U,V,'r')

hold off

Out[8]:

4
1.2 Question 2 template
1.2.1 Part (b)
Hint: draw trajectories starting on the left and right sides of the figure

In [9]: % Parameters
F = 0.50; % reciprocal of Damkohler number
K = 0.10; % K = k2/k1

In [10]: x1i1 = 1.90; % incoming feed concentration of A


dxdt = @(t,x) [F * (x1i1 - x(1)) - x(1)*x(2)^2;
-(K + F) * x(2) + x(1)*x(2)^2];
tspan = linspace(0,10,1000);

figure
hold on
title('CSTR model phase portrait with inlet concentration of 1.90')
xlabel('C_A')
ylabel('C_B')
xlim([0 3])

5
ylim([0 3])
axis('square')

n_traces = 100;
x0s = zeros(n_traces,2);
x0s(2:2:end,1) = 3;
x0s(1:2:end-1,2) = linspace(0,3,n_traces/2);
x0s(2:2:end,2) = x0s(1:2:end-1,2);

for i = 1:n_traces
x0 = x0s(i,:);
[t,x] = ode45(dxdt, tspan, x0);
plot(x(:,1),x(:,2),'b-')
end

% Adding trajectory arrows


grid3 = linspace(0,3,20);
[a, b] = meshgrid(grid3,grid3);
c = zeros(size(a));
d = zeros(size(b));

for j = 1:numel(a);
f = dxdt(tspan, [a(j);b(j)]);
c(j) = f(1);
d(j) = f(2);
end
quiver(a,b,c,d,'r')

hold off

Out[10]:

6
From the plotted phase portrait, the incoming feed concentration x1i should be less than 1.90
and initial concentration of x2 should be less than approximately 0.80 to achieve stable equilib-
rium.

1.2.2 Part (c)


In [11]: x1i2 = 1.50;
dxdt = @(t,x) [F * (x1i2 - x(1)) - x(1)*x(2)^2;
-(K + F) * x(2) + x(1)*x(2)^2];

figure
hold on
title('CSTR model phase portrait with inlet concentration of 1.50')
xlabel('C_A')
ylabel('C_B')
xlim([0 3])
ylim([0 3])
axis('square')

n_traces = 100;
x0s = zeros(n_traces,2);
x0s(2:2:end,1) = 3;

7
x0s(1:2:end-1,2) = linspace(0,3,n_traces/2);
x0s(2:2:end,2) = x0s(1:2:end-1,2);

for i = 1:n_traces
x0 = x0s(i,:);
[t,x] = ode45(dxdt, tspan, x0);
plot(x(:,1),x(:,2),'b-')
end

% Adding trajectory arrows


for j = 1:numel(a);
f = dxdt(tspan, [a(j);b(j)]);
c(j) = f(1);
d(j) = f(2);
end
quiver(a,b,c,d,'r')

hold off

Out[11]:

8
From the plotted phase portrait, when the inlet concentration changes to 1.50, there is no saddle
point and CB approaches the equilibrium point, hence the manager’s proposal is acceptable in this
situation.

You might also like