MATLAB Solutions - Part 2
MATLAB Solutions - Part 2
MATLAB Solutions - Part 2
Solutions
So You Think You Can
MATLAB
HANS-PETTER HALVORSEN, 2013.08.19
Faculty of Technology, Postboks 203, Kjlnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
Table of Contents
1
Introduction .................................................................................................................................... 3
Optimization ................................................................................................................................. 27
10
1 Introduction
No Tasks
Bacteria Population
birth rate=bx
death rate = px2
Then the total rate of change of bacteria population is:
plot(t,y)
Task 2:
where
,where
In this case we want to pass a and b as parameters, to make it easy to be able to change values for
these parameters
We set initial condition
and
tspan=[0 25];
x0=1;
a=-1/5;
b=1;
param=[a b];
[t,y]=ode45(@mysimplediff, tspan, x0,[], param);
plot(t,y)
By doing this, it is very easy to changes values for the parameters a and b.
Note! We need to use the 5.argument in the ODE solver function for this. The 4.argument is for
special options and is normally set to [], i.e., no options.
The result from the simulation is:
Task 3:
ODE Solvers
Use the ode23 function to solve and plot the results of the following differential equation in the
interval
:
MATLAB Course, Part II - Solutions
[End of Task]
Solution:
We start by rewriting the differential equation:
This gives:
function dw = diff_task3(t,w)
dw = -(1.2 + sin(10*t))*w;
Task 4:
Use the ode23/ode45 function to solve and plot the results of the following differential equation in
the interval
:
(
Note! Higher order differential equations must be reformulated into a system of first order
differential equations.
is alone on the left side.
[End of Task]
Solution:
First we rewrite like this:
In order to solve it using the ode functions in MATLAB it has to be a set with 1.order odes.
So we set:
This gives:
and:
tspan=[0 5];
x0=[0; 1];
[t,x]=ode23(@diff_secondorder, tspan, x0);
plot(t,x)
legend('x1','x2')
This gives:
we can use
plot(t,x(:,1))
Like this:
tspan=[0 5];
x0=[0; 1];
[t,x]=ode23(@diff_secondorder, tspan, x0);
plot(t, x(:,2))
This gives:
10
3 Numerical Techniques
Task 5:
Interpolation
Energy, u [KJ/kg]
2506.7
2582.8
2658.1
2733.7
2810.4
2967.9
3131.6
Plot u versus T. Find the interpolated data and plot it in the same graph. Test out different
interpolation types.
What is the interpolated value for u=2680.78 KJ/kg?
[End of Task]
Solution:
MATLAB Script:
T = [100, 150, 200, 250, 300, 400, 500];
u=[2506.7, 2582.8, 2658.1, 2733.7, 2810.4, 2967.9, 3131.6];
figure(1)
plot(u,T, '-o')
11
12
Numerical Techniques
ans =
215.0000
i., for
we get
This is because the points listed above are quite linear in their nature.
13
Numerical Techniques
Task 6:
Linear Regression
Energy, u [KJ/kg]
2506.7
2582.8
2658.1
2733.7
2810.4
2967.9
3131.6
Plot u versus T.
Find the linear regression model from the data
a=
0.6415
b=
-1.5057e+003
14
Numerical Techniques
Task 7:
Polynomial Regression
y
23
45
60
82
111
140
167
198
200
220
Use the polyfit and polyval functions in MATLAB and compare the models using different orders
of the polynomial.
Use subplots and make sure to add titles, etc.
[End of Task]
MATLAB Course, Part II - Solutions
15
Numerical Techniques
Solution:
MATLAB Script:
x=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
y=[23, 45, 60, 82, 111, 140, 167, 198, 200, 220];
for n=2:5
p=polyfit(x,y,n);
ymodel=polyval(p,x)
subplot(2,2,n-1)
plot(x,y,'o',x,ymodel)
title(sprintf('Model of order %d', n));
end
The result is:
Task 8:
Model fitting
16
Numerical Techniques
0
1.7
1.95
2.60
2.92
4.04
5.24
0
2.6
3.6
4.03
6.45
11.22
30.61
Create a 1. (linear), 2. (quadratic) and 3.order (cubic) model. Which gives the best model? Plot the
result in the same plot and compare them. Add xlabel, ylabel, title and a legend to the plot and use
different line styles so the user can easily see the difference.
[End of Task]
Solution:
MATLAB Script:
clear,
% Real
height
flow =
clc
Data
= [0, 1.7, 1.95, 2.60, 2.92, 4.04, 5.24];
[0, 2.6, 3.6, 4.03, 6.45, 11.22, 30.61];
17
Numerical Techniques
-5.8380
-2.5990
1.1350
-2.6501
4.9412
-0.1001
Where p1 is the linear model (1.order), p2 is the quadratic model (2.order) and p3 is the cubic model
(3.order).
This gives:
1. order model:
2. order model:
3. order model:
18
Numerical Techniques
Discussion:
As you see the cubic model (3.order) gives the best result, i.e., the cubic model fits the data best.
Task 9:
Numerical Differentiation
Find
Define a vector x from -5 to +5 and use the diff function to approximate the derivative y with respect
to x ( ).
Compare the data in a 2D array and/or plot both the exact value of
same plot.
Increase number of data point to see if there are any difference.
Do the same for the following functions:
MATLAB Course, Part II - Solutions
19
Numerical Techniques
[End of Task]
Solution:
Analytically solution:
MATLAB Script:
x = -5:1:5;
% Define the function y(x)
y = x.^3 + 2*x.^2 - x + 3;
% Plot the function y(x)
plot(x,y)
title('y')
% Find nummerical solution to dy/dx
dydx_num = diff(y)./diff(x);
dydx_exact = 3*x.^2 + 4.*x -1;
dydx = [[dydx_num, NaN]', dydx_exact']
% Plot nummerical vs analytical solution to dy/dx
figure(2)
plot(x,[dydx_num, NaN], x, dydx_exact)
title('dy/dx')
legend('numerical solution', 'analytical solution')
Plot of
20
Plot of
Numerical Techniques
54
MATLAB Course, Part II - Solutions
21
Numerical Techniques
22
8
0
-2
2
12
28
50
78
NaN
31
14
3
-2
-1
6
19
38
63
94
The procedure is exactly the same and will not be shown here.
Task 10:
Differentiation on Polynomials
22
Numerical Techniques
polyder(p)
The solution is:
p =
1
-1
-1
ans =
3
Which is correct.
Task 11:
Differentiation on Polynomials
% Method 2
p = conv(p1,p2)
polyder(p)
23
ans =
9
p =
3
ans =
9
Numerical Techniques
24
21
12
21
24
21
18
As expected, the result are the same for the 2 methods used above.
Task 12:
Numerical Integration
)|
[End of Task]
Solution:
MATLAB Script:
clc
x = -1:0.1:1;
y = myfunc(x);
24
Numerical Techniques
plot(x,y)
% Exact Solution
a=-1;
b=1;
Iab = 1/4*(b^4-a^4 )+2/3*(b^3-a^3 )-1/2*(b^2-a^2 )+3*(b-a)
% Method 1
avg_y = y(1:length(x)-1) + diff(y)/2;
A1 = sum(diff(x).*avg_y)
% Method 2
A2 = quad(@myfunc, -1,1)
% Method 3
A3 = quadl(@myfunc, -1,1)
Where the mathematical function is defined in the following function:
function y = myfunc(x)
y = x.^3 + 2*x.^2 - x + 3;
The plot of the function from -1 to 1 is as follows:
25
Numerical Techniques
A2 =
7.3333
A3 =
7.3333
and:
)|
The MATLAB Code is not shown, but the procedure is exactly the same for these functions.
Task 13:
Integration on Polynomials
26
Numerical Techniques
0.2500
0.6667
-0.5000
3.0000
)|
4 Optimization
Task 14:
Optimization
MATLAB Code:
%Optimization
clear, clc
x = -3:0.1:3;
f = mysimplefunc2(x);
plot(x, f)
27
28
Optimization
[xmin,fmin]=fminbnd(@mysimplefunc2, -3, 3)
Task 15:
Optimization
29
Optimization
The global minimum is inside a long, narrow, parabolic shaped flat valley. To find the valley is trivial.
To converge to the global minimum, however, is difficult.
MATLAB Script:
[x,fval] = fminsearch(@bananafunc, [-1.2;1])
Where the function is defined:
function f = bananafunc(x)
f = (1-x(1)).^2 + 100.*(x(2)-x(1).^2).^2;
This gives:
x=
1.0000
1.0000
fval =
8.1777e-010
It has a global minimum at
where
30
Optimization
figure(3)
surfl(x,y,f)
shading interp;
colormap(hot);
This gives:
31
6 Transfer Functions
Task 16:
Transfer function
Use the tf function in MATLAB to define the transfer function above. Set K=2 and T=3.
Type help tf in the Command window to see how you use this function.
Example:
% Transfer function H=1/(s+1)
num=[1];
den=[1, 1];
H = tf(num, den)
[End of Task]
Solution:
Transfer function:
With values:
MATLAB Script:
% Transfer function H=K/(Ts+1)
K=2;
T=3;
num=[K];
den=[T, 1];
H = tf(num, den)
MATLAB responds:
Transfer function:
2
------32
33
Transfer Functions
3 s + 1
Task 17:
[End of Task]
Solution:
Transfer function:
. Select
as
34
Transfer Functions
We can use:
s = tf('s')
This specifies the transfer function H(s) = s (Laplace variable).
You can then specify transfer functions directly as rational expressions in s, e.g.,
s = tf('s');
H = (s+1)/(s^2+3*s+1)
MATLAB Script (
):
clc
% Second order Transfer function
% H(s)=K/((s/?_0 )^2+2? s/?_0 +1)
% Define variables:
K=1;
MATLAB Course, Part II - Solutions
35
Transfer Functions
w0=1;
zeta=1;
% Define Transfer function
s = tf('s');
H = K/((s/w0)^2 + 2*zeta*s/w0 + 1)
step(H)
The plot from the step response becomes (
):
36
Transfer Functions
s = tf('s');
H = K/((s/w0)^2 + 2*zeta*s/w0 + 1)
step(H)
pause
end
Task 18:
Time Response
Plot the time response for the transfer function using the step function. Let the time-interval be from
0 to 10 seconds, e.g., define the time vector like this:
t=[0:0.01:10]
and use step(H,t).
[End of Task]
Solution:
MATLAB Script:
% Define the transfer function
num=[1,1];
den=[1,-1,3];
H=tf(num,den);
% Define Time Interval
t=[0:0.01:10];
% Step Response
step(H,t);
37
Transfer Functions
We see the system is unstable (which we could see from the transfer function!)
Task 19:
Integrator
, e.g.,
[End of Task]
Solution:
Pole(s):
The Integrator has a pole in origo:
38
Transfer Functions
Im(s)
Re(s)
In MATLAB you may use the pole function in order to find the poles.
% Integrator
clc
K=1;
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
pole(H)
Step Response:
% Integrator
K=1
% Define the transfer function
s = tf('s');
H = K/s
% Step Response
step(H);
or:
% Integrator
clc
K=1;
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
% Step Response
step(H);
39
Transfer Functions
You can easily modify the program in order to try with different values for K. It is also easy to modify
the program using a For Loop, e.g.:
% Integrator
clc
for K = [0.2, 1, 5]
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
% Step Response
step(H);
hold on
end
The plot becomes:
40
Transfer Functions
We can also find the mathematical expression for the step response (
Where
Then we get:
41
Transfer Functions
Task 20:
1. order system
Step response 1: Use different values for K, e.g., K=0.5, 1, 2. Set T=1
Step response 2: Use different values for T, e.g., T=0.2, 0.5, 1, 2, 4. Set K=1
[End of Task]
Solution:
Pole(s):
A 1.order system has a pole:
Im(s)
Re(s)
-1/T
In MATLAB you may use the pole function in order to find the poles.
Step Response:
MATLAB Script:
% 1.order system
clc
% Define the transfer function
K=1;
T=1;
MATLAB Course, Part II - Solutions
42
Transfer Functions
num = K;
den = [T 1];
H = tf(num, den)
pole(H)
% Step Response
step(H);
We can easily modify the program for other values for K and T.
Here is an example where we set K=0.5, 1, 2 (T=1):
MATLAB Course, Part II - Solutions
43
Transfer Functions
% 1.order system
clc
t=[0:0.5:10];
T=1;
K=0.5;
num = K;
den = [T 1];
H1 = tf(num, den);
K=1;
num = K;
den = [T 1];
H2 = tf(num, den);
K=2;
num = K;
den = [T 1];
H3 = tf(num, den);
% Step Response
step(H1,H2,H3,t);
legend('K=0.5', 'K=1', 'K=2');
This gives the following plot:
44
Transfer Functions
T=2;
num = K;
den = [T 1];
H4 = tf(num, den);
% Step Response
step(H1,H2,H3,H4,t);
legend('T=0.2', 'T=0.5', 'T=1', 'T=2');
45
Transfer Functions
Coclusion: We see from Figure above that smaller T (Time constant) gives faster response.
Mathematical expression for the step response (
).
We compare the simulation results above with the mathematical expression for the step response
(
).
Where
We use inverse Laplace and find the corresponding transformation pair in order to find
gives:
This gives:
):
. This
46
Transfer Functions
Task 21:
2. order system
Where
is the gain
zeta is the relative damping factor
[rad/s] is the undamped resonance frequency.
, e.g.,
[End of Task]
Solution:
Poles:
Poles:
gives:
MATLAB Course, Part II - Solutions
. Set
47
Transfer Functions
Step Response:
We then have that:
48
Transfer Functions
num = K;
den = [(1/w0)^2, 2*zeta/w0, 1]
H2 = tf(num, den)
zeta=2;
num = K;
den = [(1/w0)^2, 2*zeta/w0, 1]
H3 = tf(num, den)
step(H1, H2, H3, t)
legend('zeta=0.2', 'zeta=1', 'zeta=')
49
Transfer Functions
Task 22:
2. order system
We see that this system can be considered as two 1.order systems in series.
Set
and
,
,
[End of Task]
Solution:
Poles:
The poles are:
Step Response:
,
. Use the step
50
Transfer Functions
We use inverse Laplace and find the corresponding transformation pair in order to find
The step response for this system is:
Where
Then we get:
Then we get:
[
).
51
Transfer Functions
We see that the step response is a (weighted) sum of two exponential functions. The response will be
with no overshot and it will be overdamped, as shown in the simulations above.
7 State-Space Models
Task 23:
State-space model
We have that:
This gives:
[ ]
][ ]
[ ]
MATLAB Script:
% Define State-space model
A = [0 1; -1 -3];
52
][
53
State-Space Models
B = [0 0; 2 4];
C = [5 6];
D = [7 0];
sys = ss(A, B, C, D)
step(sys)
Step Response:
sys = ss(A, B, C, D)
H = tf(sys)
The result becomes:
54
State-Space Models
Task 24:
Mass-spring-damper system
[ ]
][ ]
[ ]
[ ]
Define the state-space model above using the ss function in MATLAB.
Set c=1, m=1, k=50.
Apply a step in F (u) and use the step function in MATLAB to simulate the result.
Find the transfer function from the state-space model
MATLAB Course, Part II - Solutions
55
State-Space Models
[End of Task]
Solution:
MATLAB Script:
clc
% Define variables
k = 50;
c = 1;
m = 1;
%
A
B
C
D
sys = ss(A, B, C, D)
step(sys)
The result becomes:
Transfer function:
H = tf(sys)
MATLAB Course, Part II - Solutions
56
State-Space Models
This gives:
Transfer function:
1
-----------s^2 + s + 50
Task 25:
Block Diagram
Find the state-space model from the block diagram below and implement it in MATLAB.
Set
57
State-Space Models
This gives:
[ ]
][ ]
[ ]
[ ]
Simulation:
8 Discrete Systems
Task 26:
Discretization
[ ]
][ ]
[ ]
Set some arbitrary values for
and
sys = ss(A, B, C, D)
Ts=0.1; % Sampling Time
sysd = c2d(sys, Ts);
A_disc
B_disc
C_disc
D_disc
=
=
=
=
sysd.A
sysd.B
sysd.C
sysd.D
0.0874
0.6805
58
[ ]
59
Discrete Systems
B_disc =
0.0046
0.0874
C_disc =
1
0
D_disc =
0
Note! We have to specify the sampling time when using the c2d function.
Task 27:
Discrete Simulation
birth rate=bx
death rate = px2
Then the total rate of change of bacteria population is:
Where
We get:
This gives:
60
Discrete Systems
9 Frequency Response
Task 28:
1. order system
and
Plot the frequency response of the system in a bode plot using the bode function in MATLAB.
Discuss the results.
Find
and
for the following frequencies using MATLAB code (use the bode function):
0.1
0.16
0.25
0.4
0.625
2.5
Make sure
Find
and
is in dB.
and
for the same frequencies above using the mathematical expressions for
. Tip: Use a For Loop or define a vector w=[0.1, 0.16, 0.25, 0.4, 0.625, 2.5].
[End of Task]
Solutions:
What is the break frequency?
Solution:
and
61
62
Frequency Response
Solution:
|
Plot the frequency response of the system in a bode plot using the bode function in MathScript.
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Bode Plot
bode(H)
grid
The Bode Plot becomes:
63
Find
Frequency Response
and
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Bode Plot
bode(H)
grid
% Margins and Phases
w=[0.1, 0.16, 0.25, 0.4, 0.625,2.5];
[mag, phase] = bode(H, w);
magdB=20*log10(mag); %convert to dB
magdB
phase
The result from the script is:
Transfer function:
4
------2 s + 1
magdB(:,:,1)
11.8709
magdB(:,:,2)
11.6178
magdB(:,:,3)
11.0721
magdB(:,:,4)
9.8928
magdB(:,:,5)
7.9546
magdB(:,:,6)
-2.1085
=
=
=
=
=
=
phase(:,:,1) =
-11.3099
phase(:,:,2) =
-17.7447
phase(:,:,3) =
MATLAB Course, Part II - Solutions
64
Frequency Response
-26.5651
phase(:,:,4) =
-38.6598
phase(:,:,5) =
-51.3402
phase(:,:,6) =
-78.6901
Find
and
0.1
11.9
-11.3
0.16
11.6
-17.7
0.25
11.1
-26.5
0.4
9.9
-38.7
0.625
7.8
-51.3
2.5
-2.1
-78.6
and
for the same frequencies above using the mathematical expressions for
. Tip: Use a For Loop or define a vector w=[0.1, 0.16, 0.25, 0.4, 0.625, 2.5].
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Frequency List
wlist=[0.1, 0.16, 0.25, 0.4, 0.625,2.5];
N= length(wlist);
for i=1:N
gain(i) = 20*log10(4) - 20*log10(sqrt((2*wlist(i))^2+1));
phase(i) = -atan(2*wlist(i));
MATLAB Course, Part II - Solutions
65
Frequency Response
phasedeg(i) = phase(i) * 180/pi; %convert to degrees
end
% Print to Screen
gain_data = [wlist; gain]'
phase_data=[wlist; phasedeg]'
%-----------------------------------------% Check with results from the bode function
[gain2, phase2,w] = bode(H, wlist);
gain2dB=20*log10(gain2); %convert to dB
% Print to Screen
gain2dB
phase2
Task 29:
Bode Diagram
and
66
Frequency Response
Plot the frequency response of the system in a bode plot using the bode function in MATLAB.
Discuss the results.
Find
and
for some given frequencies using MATLAB code (use the bode function).
Find
and
and
for the same frequencies above using the mathematical expressions for
. Tip: use a For Loop or define a vector w=[0.01, 0.1, ].
[End of Task]
Solutions:
What is the break frequencies?
Solution:
and
Solution:
|
Plot the frequency response of the system in a bode plot using the bode function in MATLAB.
Find
and
Solution:
MATLAB Script:
clc
clf
% Transfer function
num=[5,1];
den1=[2, 1];
den2=[10,1]
67
Frequency Response
den = conv(den1,den2);
H = tf(num, den)
% Bode Plot
bode(H)
grid
% Margins and Phases
w=[0.01, 0.1, 0.2, 0.5, 1, 10, 100];
[mag, phase] = bode(H, wlist);
magdB=20*log10(mag); %convert to dB
% Print to Screen
magdB
phase
Bode Plot:
Find
and
and
for the same frequencies above using the mathematical expressions for
. Tip: use a For Loop or define a vector w=[0.01, 0.1, ].
Solution:
Same procedure as for previous Task, the code will not be shown here.
MATLAB Course, Part II - Solutions
68
Frequency Response
Task 30:
Where
, where
Where
69
Frequency Response
close all %closes all opened figures. They will be opened as the script
is executed.
s=tf('s'); %Defines s to be the Laplace variable used in transfer
functions
%Model parameters:
Ks=0.556; %(kg/s)/%
A=13.4; %m2
rho=145; %kg/m3
transportdelay=250; %sec
%Defining the process transfer function:
K=Ks/(rho*A);
Hp=K/s;
%Defining sensor transfer function:
Km=1; Hs=tf(Km); %Defining sensor transfer function (just a gain in this
example)
%Defining controller transfer function:
Kp=1.5; Ti=1000;
Hc=Kp+Kp/(Ti*s); %PI controller transfer function
%Calculating control system transfer functions:
L=Hc*Hp*Hs; %Calculating Loop transfer function
T=L/(1+L) %Calculating tracking transfer function
S=1-T; %Calculating sensitivity transfer function
figure(1)
bodemag(L,T,S), grid %Plots maginitude of L, T, and S in Bode diagram
figure(2)
step(T), grid %Simulating step response for control system (tracking
transfer function)
The Bode plot becomes:
70
Frequency Response
71
Frequency Response
72
Frequency Response
Task 31:
Stability Analysis
We will find the crossover-frequencies for the system using MATLAB. We will also find also the gain
margins and phase margins for the system.
Plot a bode diagram where the crossover-frequencies, GM and PM are illustrated. Tip! Use the
margin function in MATLAB.
73
Frequency Response
[End of Task]
Solution:
MATLAB Code:
clear
clc
% Transfer function
num=[1];
den1=[1,0];
den2=[1,1];
den3=[1,1];
den = conv(den1,conv(den2,den3));
H = tf(num, den);
% Bode Plot
figure(1)
bode(H)
grid
% Margins and Phases
wlist=[0.01, 0.1, 0.2, 0.5, 1, 10, 100];
[mag, phase,w] = bode(H, wlist);
magdB=20*log10(mag); %convert to dB
% Display magnitude and phase data
%magdB
%phase
74
Frequency Response
We can find the crossover-frequencies, the gain margins and phase margins for the system from the
plot above.
But if we use the margin function:
margin(H)
we get the following plot:
75
Frequency Response
We can alos calculate the values using the same margin function:
[gm, pm, gmf, pmf] = margin(H);
gm_dB = 20*log10(gm);
This gives:
gm_dB =
6.0206
pm =
21.3877
gmf =
1
pmf =
0.6823
10 Additional Tasks
Task 32:
ODE Solvers
Use the ode45 function to solve and plot the results of the following differential equation in the
interval
:
[End of Task]
Solution:
We rewrite the equation:
This gives:
function dw = diff_task34(t,w)
dw = (cos(t) - (w/(1+t^2)))/3;
and:
tspan=[0 5];
w0=1;
[t,w]=ode23(@diff_task34, tspan, w0);
plot(t,w)
76
77
Task 33:
Additional Tasks
Mass-spring-damper system
[ ]
][ ]
[ ]
78
Additional Tasks
legend('x1', 'x2')
The differential equations is defined in the function msd_diff (msd_diff.m):
function dx = msd_diff(t,x)
%
k
c
m
Define variables
= 50;
= 1;
= 1;
u=1;
% Define State-space model
A = [0 1; -k/m -c/m];
B = [0; 1/m];
dx = A*x + B*u;
Results:
79
Task 34:
Additional Tasks
Numerical Integration
Find the work produced in a piston cylinder device by solving the equation:
80
Additional Tasks
where
P= pressure
V=volume, m3
n=number of moles, kmol
R=universal gas constant, 8.314 kJ/kmol K
T=Temperature, K
We also assume that the piston contains 1 mol of gas at 300K and that the temperature is constant
during the process.
Use both the quad and quadl functions. Compare with the exact solution by solving the integral
analytically.
[End of Task]
Solution:
We start by solving the integral analytically:
( )
Note! Because this work is positive, it is produced by (and not on) the system.
MATLAB Script:
clear, clc
quad(@piston_func, 1, 5)
quadl(@piston_func, 1, 5)
where the function is defined in an own function called piston_func:
function P = piston_func(V)
% Define constants
MATLAB Course, Part II - Solutions
81
Additional Tasks
n = 1;
R = 8.315;
T=300;
P=n*R*T./V;
Running the script gives:
ans =
4.0147e+003
ans =
4.0147e+003
Task 35:
State-space model
where m is the mass, r is the length of the arm of the pendulum, g is the gravity, b is a friction
coefficient.
Define the state-space model in MATLAB
Solve the differential equations in MATLAB and plot the results.
Use the following values
[End of Task]
Solution:
State-space model:
[ ]
][ ]
MATLAB Script:
clear,clc
tspan=[0 100];
MATLAB Course, Part II - Solutions
82
Additional Tasks
x0=[0.5; 0];
[t,y]=ode23(@pendulum_diff, tspan,x0);
plot(t,y)
legend('x1', 'x2')
The function is defined:
function dx = pendulum_diff(t,x)
%
g
m
r
b
83
Additional Tasks
Task 36:
lsim
[ ]
][ ]
[ ]
[ ]
Simulate the system using the lsim function in the Control System Toolbox.
Set c=1, m=1, k=50.
[End of Task]
Solution:
MATLAB Script:
clear, clc
% Define variables
k = 50;
c = 1;
m = 1;
%
A
B
C
D
84
Additional Tasks
t = 0:0.01:5;
u = eye(length(t),1);
x0=[0.5; 0];
lsim(sssys, u, t, x0)
The result becomes:
E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/
Room: B-237a