You are on page 1of 32

CONTROLS LAB (SESSION 2) :

SUBMITTED BY

SOHINI ROY

18IE10026

QUESTION 2(PART A):


E = euler(@(t,y)2,0,5,0,0.1);
tic
T=E(:,1);
Y=E(:,2);
y=2*T;
toc
Elapsed time is 0.007956 seconds.
hold on
plot(T,y)
legend("h=0.1","2t");
xlabel("t");
ylabel("y");
title("Euler : h=0.1");
hold off

1
N=norm(Y-y)
N = 1.5424e-14

E = euler(@(t,y)2,0,5,0,0.01);
tic
T=E(:,1);
Y=E(:,2);
y=2*T;
toc
Elapsed time is 0.007750 seconds.
hold on
plot(T,y)
legend("h=0.01","2t");
xlabel("t");
ylabel("y");
title("Euler : h=0.01");
hold off

2
N=norm(Y-y)
N = 1.2345e-12

E = euler(@(t,y)2,0,5,0,0.001);
tic
T=E(:,1);
Y=E(:,2);
toc
Elapsed time is 0.005996 seconds.
y=2*T;
hold on
plot(T,y)
legend("h=0.001","2t");
xlabel("t");
ylabel("y");
title("Euler : h=0.001");
hold off

3
N=norm(Y-y)
N = 2.4000e-11

QUESTION 2(PART B):

E = euler(@(t,y)3*t^2,0,5,0,0.1);
tic
T=E(:,1);
Y=E(:,2);
toc
Elapsed time is 0.006364 seconds.
y=T.^3;
hold on
plot(T,y)
legend("h=0.1","3t^2");
xlabel("t");
ylabel("y");
title("Euler : h=0.1");
hold off

4
N=norm(Y-y)
N = 12.0550
acc = (1-(N/norm(y)))*100
acc = 96.5139

E = euler(@(t,y)3*t^2,0,5,0,0.01);
tic
T=E(:,1);
Y=E(:,2);
toc
Elapsed time is 0.005606 seconds.
y=T.^3;
hold on
plot(T,y)
legend("h=0.01","3t^2");
xlabel("t");
ylabel("y");
title("Euler : h=0.01");
hold off

5
N=norm(Y-y)
N = 3.7562
acc = (1-(N/norm(y)))*100
acc = 99.6457

E = euler(@(t,y)3*t^2,0,5,0,0.001);
tic
T=E(:,1);
Y=E(:,2);
toc
Elapsed time is 0.005995 seconds.
y=T.^3;
hold on
plot(T,y)
legend("h=0.001","3t^2");
xlabel("t");
ylabel("y");
title("Euler : h=0.001");
hold off

6
N=norm(Y-y)
N = 1.1861
acc = (1-(N/norm(y)))*100
acc = 99.9645

QUESTION 4 :

M2 =rk4method(@(t,y)3*(t^2),0,5,0,0.1);
tic
T=M2(:,1);
Y=M2(:,2);
toc
Elapsed time is 0.006051 seconds.
y=(T.^3);
hold on
plot(T,y)
legend("h=0.1","3t^2");
xlabel("t");
ylabel("y");
title("RK4 : h=0.1");

7
hold off

N=norm(Y-y)
N = 2.0510
acc = (1-(N/norm(y)))*100
acc = 99.4069

M2 =rk4method(@(t,y)3*(t^2),0,5,0,0.01);
tic
T=M2(:,1);
Y=M2(:,2);
toc
Elapsed time is 0.006543 seconds.
y=(T.^3);
hold on
plot(T,y)
legend("h=0.01","3t^2");
xlabel("t");
ylabel("y");
title("RK4 : h=0.01");
hold off

8
N=norm(Y-y)
N = 0.6273
acc = (1-(N/norm(y)))*100
acc = 99.9408

M2 =rk4method(@(t,y)3*(t^2),0,5,0,0.001);
tic
T=M2(:,1);
Y=M2(:,2);
toc
Elapsed time is 0.005964 seconds.
y=(T.^3);
hold on
plot(T,y)
legend("h=0.001","3t^2");
xlabel("t");
ylabel("y");
title("RK4 : h=0.001");
hold off

9
N=norm(Y-y)
N = 0.1977
acc = (1-(N/norm(y)))*100
acc = 99.9941

WHAT DO YOU OBSERVE AS h DECREASES ?

AS h DECREASES THE ERROR DECREASES AND ACCURACY INCREASES AND COMPUTATION TIME
INCREASES.

TABLE :

10
QUESTION 5:

PENDULUM (UNDAMPED) :
close all hidden
g=9.8;
l=2;
b=0.3;
m=1;
tspan = [0 20];
y0 = [2 0];
fn = @(t,y)[y(2);-(g/l)*sin(y(1))];

%ODE45
[t,y] = ode45(fn,tspan,y0);
subplot(3,2,[1,2])
plot(t,y(:,1))
title("ode45");

%RK4
h = [0.1,0.01];
[t_n,y_n] = vectorised_rk4method(fn,0,20,y0,h(1));
subplot(3,2,3)
plot(t_n,y_n(:,1))
title("RK4: h=0.1");

[t_n,y_n] = vectorised_rk4method(fn,0,20,y0,h(2));
subplot(3,2,4)
plot(t_n,y_n(:,1))
title("RK4: h=0.01");

%Euler
h = [0.1,0.01];
[t_n,y_n] = vectorised_euler(fn,0,20,y0,h(1));
subplot(3,2,5)

11
plot(t_n,y_n(:,1))
title("EULER: h=0.1");

[t_n,y_n] =vectorised_euler(fn,0,20,y0,h(2));
subplot(3,2,6)
plot(t_n,y_n(:,1))
title("EULER: h=0.01");

PENDULUM (DAMPED) :
close all hidden
g=9.8;
l=2;
b=0.3;
m=1;
y0 = [2 0];
fn = @(t,y)[y(2);-(b/m)*y(2)-((m*g)/(l*(m-2*b)))*sin(y(1))];

%ODE45
[t,y] = ode45(fn,tspan,y0);
subplot(3,2,[1,2])
plot(t,y(:,1))
title("ode45");

%RK4
h = [0.1,0.01];

12
[t_n,y_n] = vectorised_rk4method(fn,0,20,y0,h(1));
subplot(3,2,3)
plot(t_n,y_n(:,1))
title("RK4: h=0.1");

[t_n,y_n] = vectorised_rk4method(fn,0,20,y0,h(2));
subplot(3,2,4)
plot(t_n,y_n(:,1))
title("RK4: h=0.01");

%Euler
h = [0.1,0.01];
[t_n,y_n] = vectorised_euler(fn,0,20,y0,h(1));
subplot(3,2,5)
plot(t_n,y_n(:,1))
title("EULER: h=0.1");

[t_n,y_n] =vectorised_euler(fn,0,20,y0,h(2));
subplot(3,2,6)
plot(t_n,y_n(:,1))
title("EULER: h=0.01");

QUESTION 6 :
close all hidden
fn = @(t_n,y_n) 1-0.5*y_n;

13
h=0.001;
y_0=0;
M2 = rk4method(fn,0,20,y_0,h);
t_n = M2(:,1);
y_n = M2(:,2);
hold on
plot(t_n,y_n,'g-');
y_0 = 3;
M2 = rk4method(fn,0,20,y_0,h);
t_n = M2(:,1);
y_n = M2(:,2);
plot(t_n,y_n,'b-');
xlim([0 20])
title('RL TIME RESPONSE');
ylabel("Current");
xlabel("Time");
legend("I(0)=0 A","I(0) = 3 A");
hold off

QUESTION 7 :

UNDAMPED (b = 0) :
x = -1:0.05:1;
[y1,y2] = meshgrid(x);
fn = @(t,y)[y(2);-(g/l)*sin(y(1))];
y1dot = y2;

14
y2dot = -(9.8/2)*sin(y1);
quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("y_2");
hold on
[ts,ys] = ode45(fn,[0,20],[0;0.5]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Undamped : b=0");
legend("Trajectory", "Phase Potrait");
hold off

DAMPED (b = 0.3) :
x = -1:0.05:1;
[y1,y2] = meshgrid(x);
fn = @(t,y)[y(2);-(b/m)*y(2)-((m*g)/(l*(m-2*b)))*sin(y(1))];
y1dot = y2;
y2dot = -(0.3/1)*y1dot -(9.8/0.8)*sin(y1);
quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("y_2");
hold on
[ts,ys] = ode45(fn,[0,20],[0;0.5]);
plot(ys(:,1),ys(:,2))

15
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Damped : b=0.3");
legend("Trajectory", "Phase Potrait");
hold off

QUESTION 8 :

PART A :
x = -1:0.05:1;
x0 = [-0.5;-0.3];
t_i = [0 10];
A = [-1 1;-1 -1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point

16
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

17
PART B :
x = -1:0.05:1;
x0 = [-0.3;-0.3];
t_i = [0 10];
A = [0 1;-1 0];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

18
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

19
PART C [ INTERVAL[0,5]:
x = -1:0.05:1;
x0 = [-0.05;-0.05];
t_i = [0 5];
A = [1 1;-1 1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

20
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

21
PART C (INTERVAL[0,10] :
x = -1:0.05:1;
x0 = [-0.05;0.05];
t_i = [0 10];
A = [1 1;-1 1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

22
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

23
PART D(1) :
x = -1:0.05:1;
x0 = [0.1;-0.1];
t_i = [0 10];
A = [1 3;1 -1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

24
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

25
PART D(2):
x = -1:0.05:1;
x0 = [0.11;-0.1];
t_i = [0 4];
A = [1 3;1 -1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

26
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

27
PART D(3):
x = -1:0.05:1;
x0 = [0.1;-0.11];
t_i = [0 10];
A = [1 3;1 -1];
[x1,x2] = meshgrid(x);
fn = @(t,x) A*x;

x1dot = A(1,1)*x1+A(1,2)*x2;
x2dot = A(2,1)*x1+A(2,2)*x2;

quiver(y1,y2,y1dot,y2dot);
xlabel("y_1");
ylabel("x_2");
hold on
[ts,ys] = ode45(fn,[0,10],[-0.5;-0.3]);
plot(ys(:,1),ys(:,2))
plot(ys(1,1),ys(1,2),'bo') % starting point
plot(ys(end,1),ys(end,2),'bo') % ending point
title("Trajectory and Phase Potrait");
legend("Trajectory", "Phase Potrait");
hold off

28
[t,x] = ode45(fn,t_i,x0);
plot(t,x(:,1),t,x(:,2));
hold on
xlabel("Time");
ylabel("Trajectory");
title("Trajectory vs. Time");
legend("x1","x2");
hold off

29
Q8. NOW FIND THE EIGEN VALUES OF EACH OF THE A MATRICES AND DISCUSS HOW THEY ARE
RELATED TO THE SYSTEM TRAJECTORY
A1 = [-1 1;-1 -1];
eig(A1)
ans = 2×1 complex
-1.0000 + 1.0000i
-1.0000 - 1.0000i

A2 = [0 1;-1 0];
eig(A2)
ans = 2×1 complex
0.0000 + 1.0000i
0.0000 - 1.0000i

A3 = [1 1;-1 1];
eig(A3)
ans = 2×1 complex
1.0000 + 1.0000i
1.0000 - 1.0000i

A4 = [1 3;1 -1];
eig(A4)

30
ans = 2×1
2
-2

ANS :

1. HENCE WE OBSERVE THAT IF EIGEN IS PURELY REAL THEN THE SYSTEM BEHAVIOUR IS
OVERDAMPED.
2. IF THE EIGEN VALUE IS PURELY IMMAGINARY THEN TEH SYSTEM BEHAVIOUR IS SINUSOIDAL.
3. IF THE EIGEN VALUE CONTAINS BOTH IMAGINARY AND REAL PARTS THEN THE SYSTEM
BEHAVIOUR IS UNDERDAMPED.

QUESTION 1 (EULER FUNCTION):


function E = euler(f,t0,tend,y0,h)
N=(tend-t0)/h;
Y=zeros(N+1,1);
T= [t0:h:tend]';
Y(1)=y0;
for j=1:N
Y(j+1)=Y(j)+h*f(T(j),Y(j));
end
plot(T,Y)
E=[T Y];
end

QUESTION 5 (VECTORISED EULER FUNCTION) :


function [T,Y] = vectorised_euler(f,t0,tend,y0,h)
N=(tend-t0)/h;
T= [t0:h:tend]';
Y(1,:)=y0;
for j=1:N
Y(j+1,:)=Y(j,:)+h*f(T(j),Y(j,:)).';
end
end

QUESTION 3 (RK4 FUNCTION) :


function M2=rk4method(F,t0,tf,y0,h)
N=(tf-t0)/h;
y=zeros(N+1,1);
y(1)=y0;
t=zeros(N+1,1);
t(1)=t0;
for i=2:1:N+1
s1 = F(t(i-1),y(i-1));
s2 = F(t(i-1)+h/2,y(i-1)+h*s1/2);

31
s3 = F(t(i-1)+h/2,y(i-1)+h*s2/2);
s4 = F(t(i-1)+h/2,y(i-1)+h*s3/2);
y(i) = y(i-1)+h*(s1+2*s2+2*s3+s4)/6;
t(i)=t(i-1)+h;
end
plot(t,y)
M2=[t y];
end

QUESTION 5 (VECTORISED RK4 FUNCTION) :


function [t,y]=vectorised_rk4method(F,t0,tf,y0,h)
y(1,:)=y0;
t(1)=t0;
for i=2:tf/h+1
s1 = (F(t(i-1),y(i-1,:))).';
s2 = (F(t(i-1)+h/2,y(i-1,:)+h*s1/2)).';
s3 = (F(t(i-1)+h/2,y(i-1,:)+h*s2/2)).';
s4 = (F(t(i-1)+h/2,y(i-1,:)+h*s3/2)).';
y(i,:) = y(i-1,:)+h*(s1+2*s2+2*s3+s4)/6;
t(i)=t(i-1)+h;
end
end

32

You might also like