me 1020 engineering programming with matlab chapter 9c in...

17
ME 1020 Engineering Programming with MATLAB Chapter 9c In-Class Assignment: 9.30, 9.34, 9.26, 9.37, 9.38, 9.42 Topics Covered: Higher-Order Differential Equations o Euler Method o MATLAB ODE Solver ode45 o ode45/Matrix Method Matrix Methods for Linear Equations o Free Response o Impulse Response o Step Response o Arbitrary Input Response Problem 9.30: Solve using the Euler Method. This is a second-order ordinary differential equation. Rewrite the equation by solving for the second derivative. ̈ =− 18 3 ̇ − 102 3 + 10/3 = −6̇ − 34 + 10/3 Let 1 = and 2 = ̇ Taking the derivative of the first equation gives ̇ 1 =̇ = 2 or ̇ 1 = 2 Taking the derivative of the second equation gives

Upload: doankhuong

Post on 16-May-2019

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

ME 1020 Engineering Programming with MATLAB

Chapter 9c In-Class Assignment: 9.30, 9.34, 9.26, 9.37, 9.38, 9.42

Topics Covered:

Higher-Order Differential Equations

o Euler Method

o MATLAB ODE Solver ode45

o ode45/Matrix Method

Matrix Methods for Linear Equations

o Free Response

o Impulse Response

o Step Response

o Arbitrary Input Response

Problem 9.30:

Solve using the Euler Method.

This is a second-order ordinary differential equation. Rewrite the equation by solving for the second

derivative.

�̈� = −18

3�̇� −

102

3𝑦 + 10/3 = −6�̇� − 34𝑦 + 10/3

Let 𝑥1 = 𝑦 and 𝑥2 = �̇�

Taking the derivative of the first equation gives

�̇�1 = �̇� = 𝑥2 or �̇�1 = 𝑥2

Taking the derivative of the second equation gives

Page 2: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

�̇�2 = �̈� = −6𝑥2 − 34𝑥1 + 10/3

The original second-order ordinary differential equation is now converted into two first-order ordinary

differential equations that are coupled.

�̇�1 = 𝑥2, �̇�2 = −6𝑥2 − 34𝑥1 + 10/3, 𝑥1(0) = 0, 𝑥2(0) = 0

Use the Euler method for this problem. The system of equations can be discretized as follows:

𝑥1,𝑘+1 = 𝑥1,𝑘 + ∆𝑡 ∙ 𝑥2,𝑘

𝑥2,𝑘+1 = 𝑥2,𝑘 + ∆𝑡[−6𝑥2,𝑘 − 34𝑥1,𝑘 + 10/3]

In the problem statement, the initial conditions are 𝑦(0) = 𝑥1(0) = 0 and �̇�(0) = 𝑥2(0) = 0. Let ∆𝑡 = 0.01.

For 𝑘 = 1:

𝑥1,2 = 𝑥1,1 + ∆𝑡 ∙ 𝑥2,1 = (0.0) + (0.01)(0.0) = 0.0

𝑥2,2 = 𝑥2,1 + ∆𝑡[−6𝑥2,1 − 34𝑥1,1 + 10/3]

𝑥2,2 = (0.0) + (0.01)[−(6)(0.0) − (34)(0.0) + 10/3] = 0.03̅

For 𝑘 = 2:

𝑥1,3 = 𝑥1,2 + ∆𝑡 ∙ 𝑥2,2 = (0.0) + (0.01)(0.03̅) = 0.0003̅

𝑥2,3 = 𝑥2,2 + ∆𝑡[−6𝑥2,2 − 34𝑥1,2 + 10/3]

𝑥2,3 = (0.03̅) + (0.01)[−(6)(0.03̅) − (34)(0.0) + 10/3] = 0.0646̅

For 𝑘 = 3:

𝑥1,4 = 𝑥1,3 + ∆𝑡 ∙ 𝑥2,3 = (0.0003̅) + (0.01)(0.0646̅) = 0.0098

𝑥2,4 = 𝑥2,3 + ∆𝑡[−6𝑥2,3 − 34𝑥1,3 + 10/3]

𝑥2,4 = (0.0646̅) + (0.01)[−(6)(0.0646̅) − (34)(0.0003̅) + 10/3] = 0.094006̅

Page 3: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

% Problem 9.30

clear

clc

disp('Problem 9.30: Scott Thomas')

Na = 3000;

delta_ta = 0.001;

x1a = zeros(1,Na);

Page 4: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

x2a = zeros(1,Na);

ta = zeros(1,Na);

x1a(1) = 0.0;

x2a(1) = 0.0;

for k = 1:Na

x1a(k+1) = x1a(k) + x2a(k)*delta_ta;

x2a(k+1) = x2a(k) + (-6*x2a(k) - 34*x1a(k) + 10/3)*delta_ta;

ta(k+1) = ta(k) + delta_ta;

end

Nb = 3000;

delta_tb = 0.001;

x1b = zeros(1,Nb);

x2b = zeros(1,Nb);

tb = zeros(1,Nb);

x1b(1) = 0.0;

x2b(1) = 10.0;

for k = 1:Nb

x1b(k+1) = x1b(k) + x2b(k)*delta_tb;

x2b(k+1) = x2b(k) + (-6*x2b(k) - 34*x1b(k) + 10/3)*delta_tb;

tb(k+1) = tb(k) + delta_tb;

end

plot(ta, x1a, tb, x1b)

xlabel('Time t'), ylabel('Function y(t)')

title('Problem 9.30: Scott Thomas')

legend('Part a: dy/dt(0) = 0', 'Part b: dy/dt(0) = 10','Location', 'NorthEast')

%axis([0 50 5 25])

Problem 9.30: Scott Thomas

Page 5: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary
Page 6: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

Use the ode45 Solver for this problem.

𝐿�̈� + 𝑔 sin 𝜃 = 𝑎(𝑡) cos 𝜃

�̈� = − (𝑔

𝐿) sin 𝜃 + (

𝑎(𝑡)

𝐿) cos 𝜃

Let

𝑥1 = 𝜃, 𝑥2 = �̇�

Take the derivative of the first equation:

�̇�1 = �̇� = 𝑥2 → �̇�1 = 𝑥2

Take the derivative of the second equation:

�̇�2 = �̈� = − (𝑔

𝐿) sin 𝜃 + (

𝑎(𝑡)

𝐿) cos 𝜃

�̇�2 = − (𝑔

𝐿) sin 𝑥1 + (

𝑎(𝑡)

𝐿) cos 𝑥1

Page 7: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

function xdot = f934ab( t,x )

% Problem 9.34a and 9.34b: a = 5 m/s^2

a = 5;% m/s;

g = 9.81; % m/s^2

L = 1.0;% m

xdot(1) = x(2);

xdot(2) = -g/L*sin(x(1)) + a/L*cos(x(1));

xdot = [xdot(1) ; xdot(2)];

end

function xdot = f934c( t,x )

% Problem 9.34a and 9.34b: a = 0.5t m/s^2

a = 0.5*t;% m/s;

g = 9.81; % m/s^2

L = 1.0;% m

xdot(1) = x(2);

xdot(2) = -g/L*sin(x(1)) + a/L*cos(x(1));

xdot = [xdot(1) ; xdot(2)];

end

Page 8: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

% Problem 9.34

clear

clc

disp('Problem 9.34: Scott Thomas')

[ta,xa] = ode45(@f934ab, [0 10], [0.5,0]);

[tb,xb] = ode45(@f934ab, [0 10], [3.0,0]);

[tc,xc] = ode45(@f934c, [0 10], [3.0,0]);

figure

plot(ta, xa(:,1)), xlabel('Time (s)'), ylabel('Position (rad)')

title('Problem 9.34a: Scott Thomas')

text(0.2,0.445,'a = 5 m/s^2, \theta (0) = 0.5 rad')

figure

plot(tb, xb(:,1), tc, xc(:,1)), xlabel('Time (s)'), ylabel('Position (rad)')

title('Problem 9.34b and 9.34c: Scott Thomas')

legend('a = 5 m/s^2, \theta(0) = 3 rad','a = 0.5t m/s^2, \theta(0) = 3 rad', 'Location', 'SouthEast')

Problem 9.34: Scott Thomas

Page 9: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary
Page 10: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

Problem 9.26:

Use the ode45 Solver/Matrix Method to solve this problem.

Problem setup:

𝑚�̈� + 𝑐�̇� + 𝑘𝑦 = 0

This is a second-order ordinary differential equation. Rewrite the equation by solving for the second

derivative.

�̈� = −𝑐

𝑚�̇� −

𝑘

𝑚𝑦

Let

𝑥1 = 𝑦 and 𝑥2 = �̇�

Taking the derivative of the first equation gives

�̇�1 = �̇� = 𝑥2 or �̇�1 = 𝑥2

Taking the derivative of the second equation gives

�̇�2 = �̈� = − (𝑐

𝑚) 𝑥2 − (

𝑘

𝑚) 𝑥1

The original second-order ordinary differential equation is now converted into two first-order ordinary

differential equations that are coupled.

�̇�1 = 𝑥2, �̇�2 = − (𝑐

𝑚) 𝑥2 − (

𝑘

𝑚) 𝑥1, 𝑥1(0) = 10, 𝑥2(0) = 5

These equations can be placed into Matrix Form:

[�̇�1

�̇�2] = [

0 1

−𝑘

𝑚−

𝑐

𝑚

] ∙ [𝑥1

𝑥2]

or

Page 11: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

�̇� = 𝐀𝐱

where

𝐀 = [0 1

−𝑘

𝑚−

𝑐

𝑚

] 𝐱 = [𝑥1

𝑥2]

function xdot = f926a( ~,x )

% Problem 9.26a:

m = 3;

c = 18;

k = 102;

A = [0 1; -k/m -c/m];

xdot = A*x;

end

function xdot = f926b( ~,x )

% Problem 9.26b:

m = 3;

c = 39;

k = 120;

A = [0 1; -k/m -c/m];

xdot = A*x;

end

% Problem 9.26

clear

clc

disp('Problem 9.26: Scott Thomas')

[ta,xa] = ode45(@f926a, [0 2], [10,5]);

[tb,xb] = ode45(@f926b, [0 2], [10,5]);

plot(ta, xa(:,1),tb, xb(:,1))

ylabel('Position (m)')

title('Problem 9.26: Scott Thomas')

legend('m = 3, c = 18, k = 102','m = 3, c = 39, k = 120')

Problem 9.26: Scott Thomas

Page 12: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary
Page 13: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

Problem 9.37:

% Problem 9.37

clc

clear

disp('Problem 9.37: Scott Thomas')

A = [0, 1; -7/10, -3/10]

B = [0; 1/10]

C = [1, 0]

D = 0

sys_ss = ss(A,B,C,D);

impulse(sys_ss)

Problem 9.37: Scott Thomas

A =

0 1.0000

-0.7000 -0.3000

B =

0

0.1000

C =

1 0

D =

0

Page 14: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary
Page 15: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

Problem 9.38:

% Problem 9.38

clc

clear

disp('Problem 9.38: Scott Thomas')

sys1 = tf([7,1], [10,6,2])

step(sys1)

Problem 9.38: Scott Thomas

sys1 =

7 s + 1

----------------

10 s^2 + 6 s + 2

Continuous-time transfer function.

Page 16: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary

Problem 9.42:

% Problem 9.42

clc

clear

disp('Problem 9.42: Scott Thomas')

RC = 0.2;

sys_tf = tf(1, [RC 1]);

x0 = 0;

N = 1000;

t = linspace(0,2,N);

for k = 1:N

if t(k) < 0.4

u(k) = 10;

else

u(k) = 0;

end

end

subplot(2,1,1)

plot(t,u), xlabel('Time (seconds)'), ylabel('Forcing Function u(t)')

title('Problem 9.42: Scott Thomas')

axis([0 2, -1 11])

subplot(2,1,2)

lsim(sys_tf,u,t,x0)

Problem 9.42: Scott Thomas

Page 17: ME 1020 Engineering Programming with MATLAB Chapter 9c In ...cecs.wright.edu/~sthomas/matlabinclassassignmentchap09c.pdḟ2= ̈=−6 2−34 1+10/3 The original second-order ordinary