0% found this document useful (0 votes)
21 views21 pages

4.4 Modelling Dynamical Systems

Uploaded by

Koskei Vic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views21 pages

4.4 Modelling Dynamical Systems

Uploaded by

Koskei Vic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Graphics

Mathematical modelling: Dynamic System Modelling, linear


systems, numerical differentiation and integration.

Dr. Anthony - CUK


Dynamic System Modelling (Using MATLAB)

• Some algorithms have a dynamic component. For example frame based


processing e.g. simple waveforms.
• Dynamic System Models generally represent systems that have internal
dynamics or memory of past states such as integrators, delays, transfer
functions, and state-space models.
• Dynamic systems have algorithms which are state dependent, meaning
that it depends on time and previous input we have different output.

Qtn: How do I track these states in Matlab code?


Consider a simple waveform in Matlab code…
• Framebased sinewave to process: Amplitude: 0.8, freq. 100, sampling
frequency of 1000, framesize 796.

• So how do we do make sure our code ‘remembers’ the state we are in?
We use “System objects”
Same waveform problem but with a System Object

• Create the system object with default properties


H=dsp.SineWave;
H.Amplitude=0.8;
H.Frequency=100;
H.SampleRate=1000;
H.SamplesPerFrame=796;
S=[];
for n = 1:100
x = step (H);
z = [s:x];
end
System Objects

• System objects are designed specifically for implementing and simulating


dynamic systems and process streamed data with inputs that change over
time. Many signal processing, communications, and controls systems are
dynamic.
• Uses internal stored states to store the past behaviour.
• Optimised for iterative computations that process large streams of data –
Radar, audio, video processing systems.
• System objects use a minimum of two commands to process data:
• Creation of the object (such as, fft256 = dsp.FFT)
• Running data through the object (such as, fft256(x))
Creating your own System Object

• Matlab has defined API for developing SO


• Code to create them is divided into 2 parts:
• Properties, including discrete states that must be preserved
• Methods, three of which are compulsory:
✓ Setuplmpl - Perform one-time calculations, such as computing constants
✓ steplmpl - Implement algorithm. E.g. Calculate y as a function of input u and discrete states.
✓ Resetlmpl - Initialize / reset discrete-state properties
• Code to call the SO is divided into 3 parts:
➢Declaration – parameter and state initialisation and validation
➢Execution – computing outputs, updating values, resetting of states. Execution is typically
done in a loop
➢Termination – releasing resources, enabling objects to be set again
Running a System Object

• To run a SO and perform the operation defined by its algorithm, you


call the object as if it were a function. For example, to create an FFT
object that uses the dsp.FFT SO, specifies a length of 1024, and names
it dft, use:
dft = dsp.FFT('FFTLengthSource','Property','FFTLength',1024);
• To run this object with the input x, use:
dft(x);
• If you run a SO without any input arguments, you must include empty
parentheses. For example, dft().
Where else can you use System Objects

• Programming
• Data Analysis
• GUI design
Mathematical modelling of linear systems

• Discusses how to create and work with matrices and vectors in


MATLAB, focusing on using the LINSOLVE and backslash operators to
solve linear systems. We looked at examples of solving different linear
systems.
Numerical differentiation and integration

Solve Ordinary Differential Equation


• Solve an ODE analytically by using the dsolve function, with or without
initial conditions.

First-Order Linear ODE


𝑑𝑦
Solve this differential equation = 𝑡𝑦
𝑑𝑡
First, represent y by using syms to create the symbolic function y(t). syms y(t)
Define the equation using == and represent differentiation using the diff function.
ode = diff(y,t) == t*y
ode(t) =
diff(y(t), t) == t*y(t)
Solve the equation using dsolve.
ySol(t) = dsolve(ode)
ySol(t) =
C1*exp(t^2/2) % Constant C1 appears because no condition was specified.
Solve Differential Equation with Condition
Solve the equation with the initial condition y(0) == 2. The dsolve function finds a value of C1 that satisfies
the condition.
cond = y(0) == 2;
ySol(t) = dsolve(ode,cond)
ySol(t) =
2*exp(t^2/2)
If dsolve cannot solve your equation, then try solving the equation numerically.
Nonlinear Differential Equation with Initial Condition
Solve this nonlinear differential equation with an initial condition. The equation has multiple solutions.
ⅆ𝑦 2
+𝑦 = 1,
ⅆ𝑡
𝑦 0 = 0.

(syms y(t)
ode = (diff(y,t)+y)^2 == 1;
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)

ySol(t) =
exp(-t) - 1 1 - exp(-t)
Second-Order ODE with Initial Conditions
Solve second-order differential equation with two initial conditions.
ⅆ2 𝑦
2
= cos 2𝑥 − 𝑦
ⅆ𝑥
y(0)=1,
y′(0)=0.
Define the equation and conditions. The second initial condition involves the first derivative of y.
Represent the derivative by creating the symbolic function Dy = diff(y) and then define the condition
using Dy(0)==0.
syms y(x)
Dy = diff(y);
ode = diff(y,x,2) == cos(2*x)-y;
cond1 = y(0) == 1;
cond2 = Dy(0) == 0;
Solve ode for y. Simplify the solution using the simplify function.
conds = [cond1 cond2];
ySol(x) = dsolve(ode,conds);
ySol = simplify(ySol)

ySol(x) =
1 - (8*sin(x/2)^4)/3
Solve a System of Differential Equations (Linear Example):

• Solve this system of linear first-order differential equations.


ⅆ𝑢
ⅆ𝑡
= 3𝑈 + 4𝑣+3v,
ⅆ𝑣
= −4𝑢 + 3𝑣
ⅆ𝑡
First, represent u and v by using syms to create the symbolic functions u(t) and v(t).
syms u(t) v(t)
Define the equations using == and represent differentiation using the diff function.
ode1 = diff(u) == 3*u + 4*v;
ode2 = diff(v) == -4*u + 3*v;
odes = [ode1; ode2]
odes(t) =
diff(u(t), t) == 3*u(t) + 4*v(t)
diff(v(t), t) == 3*v(t) - 4*u(t)
• Solve the system using the dsolve function which returns the solutions as elements of a structure
S = dsolve(odes)
Solve Differential Equations in Matrix Form using dsolve
• Consider this system of differential equations

syms x(t) y(t)


A = [1 2; -1 1];
B = [1; t]; Y = [x; y];
odes = diff(Y) == A*Y + B
Exercise
1. Given that the y= , find the first-order derivative.
Using the dsolve function. Using matlab, write an algorithm to solve
the problem

2. A system is given by below ODE. Use the dsolve function to solve the
equation.

• Solve this second-order differential equation


ⅆ𝑦
3. A function y(t) satisfies the differential equation = 𝑦−4 6𝑦 3 + 5𝑦 2 .
ⅆ𝑡
Solve the nonlinear ode for y using dsolve.

4. A second-order differential equation is given by


Solve the equation given that y(0)=1 and y’(0)=0.

Write a matlab command algorithm to solve the above equation


under same conditions.
Numerical Differentiation & Integration

Derivatives and Integrals


• We can use MATLAB to numerically differentiate or integrate
functions!
• The key is to remember the definitions of derivative and definite
integral:
Numerical Differentiation
• For small ∆x, we have the following estimate for f’(x):

• Using the MATLAB commands “linspace” and “diff”, we can find reasonable
approximations to f’(x), provided ∆x is small and f is differentiable!
Numerical Differentiation
• Example 1:
• Use MATLAB to find the numerical derivative of y = sin(x) on the interval [0, 2π].
• Compare this estimate for dy/dx to the actual derivative of y.
Answer: Assume dy/dx is yprime:
x = linspace(0, 2*pi, 1000);
y = sin(x);
deltax = diff(x);
deltay = diff(y);
yprime = deltay./deltax;
• Commands:
subplot(1,2,1)
plot(x(1:999), yprime, ‘r‘)
title(‘yprime = \Deltay/\Deltax’)
subplot(1,2,2)
plot(x(1:999), cos(x(1:999)),‘b’)
title(‘dy/dx = cos(x)’)
Numerical Integration

You might also like