Matlab and Simulink

Matlab

Matlab (download link (Links to an external site.)) is used in the laboratory sessions as well as in the computer exercise sessions. We will use the following toolboxes:

  • Control System Toolbox
  • Simulink

Simulation Basics

Simple simulations of systems can be done in Matlab using ode45. The code below can be used to perform basic simulations from different initial conditions LaTeX: x_0x0 for a system on the form LaTeX: \dot{x}=f\left(x\right),\;x\left(0\right)=x_0˙x=f(x),x(0)=x0:

%%%%%%%%%%%%%

clear

x=sym('x',[2,1]); %define system state

mu=.6;
f=[x(2);-x(2)+mu*(1-x(1)^2)*x(1)];%define nonlinear system

x0=10*[1;0];%initial condition for simulation
tspan=50;%length of simulation

fn=matlabFunction(f);%pre processing for numerically solving ode
sl=ode45(@(t,x) fn(x(1),x(2)),[0,tspan],x0);%solve ode

tt=linspace(0,tspan,1e4);
xx = deval(sl,tt);%sample more points from the solution to get smoother trajectories

figure(1)%plot solution
clf
plot(x(1,:),x(2,:))

%%%%%%%%%%%%%

A very brief introduction to Simulink

Emil goes through the basics of building and simulating models in Simulink. To complete the task yourself, see the following handout: