Matlab1 Simulation and Modelling
Matlab1 Simulation and Modelling
[References]
1. Physical Modeling in MATLAB Version 4.0, Allen B. Downey, Green Tea Press, 2021.
2. Solving ODEs with MATLAB, L. F. Shampine, I. Gladwell, S. Thompson, Cambridge
University Press, 2003.
3. Continuous System Modeling, Francois E. Cellier, Springer, 1991.
4. Continuous System Simulation, François E. Cellier, Ernesto Kofman, Springer, 2006.
5. Modeling of Dynamic Systems, Lennart Ljung, Tarkel Glad, Prentice Hall, 1994.
[What is a Model?]
"A model (M) for a system (S) and an experiment (E) is anything to which E can be
applied in order to answer questions about S ."
A mathematical model is a model (M) described by mathematical equations.
% derivative function
function res=rate_func(t,y)
ydot=0.2*y;
res=ydot;
end
plot(T,Y,'--');
axis([0 5 5 12]);
legend('ODE45','Euler');
end
• When ode45 calls this rate_func function, it provides a vector as input and
expects to get a vector as output.
• When we solve a system of differential equations, we can visualize the results with
a phase plot, which shows the state of a system as a point in the space of possible
states. A trajectory is a path in a phase plot that shows how the state of a system
changes over time.
For this example, we’ll use odeset to set the Refine option to 1, which tells ode45 to
return only the time steps it computes, rather than interpolating between them.
Now we can modify the rate function to plot the places where it gets evaluated:
function res = rate_func(t, y)
dydt = y * sin(t);
res = dydt;
hold on;
plot(t, y, 'ro')
dt = 0.01;
ts = [t t+dt];
2102653 SPECIAL TOPICS IN POWER ELECTRONICS 36/
53
ys = [y y+dydt*dt];
plot(ts, ys, 'r-')
end
(a) (b)
Figure: (a) Points where ode45 evaluates the rate function and (b) zoomed in plot.
This is considered as the error in y(k+1). We calculate the optimal time interval hopt as,
1
h 5
s = , hopt = sh,
2 z (k + 1) − y (k + 1)
where h in the right side is the old time-interval, and is the acceptable tolerance error.
In practical programming, this new hopt will be used in the next step of the calculation.
KVL: KCL:
Matlab Script
• Block diagrams consist of blocks that are connected by paths. The paths represent
signals, and the blocks transform the input signals into the output signals.
• Block diagram preserves the computational structure of the system.
Simscape