4.4 Modelling Dynamical Systems
4.4 Modelling Dynamical Systems
• 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
• Programming
• Data Analysis
• GUI design
Mathematical modelling of linear systems
(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):
2. A system is given by below ODE. Use the dsolve function to solve the
equation.
• 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