0% found this document useful (0 votes)
22 views

Sim U Link Example

This Simulink model simulates the motion of an object based on a differential equation. The model includes subsystems to represent the dynamic system and uses blocks like integrators, gains, and if/else statements. Simulation parameters and initialization values are set in callback functions, and results are plotted and saved to workspace upon completion.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Sim U Link Example

This Simulink model simulates the motion of an object based on a differential equation. The model includes subsystems to represent the dynamic system and uses blocks like integrators, gains, and if/else statements. Simulation parameters and initialization values are set in callback functions, and results are plotted and saved to workspace upon completion.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Simulink Example

This example uses Simulink to simulate the following differential equation


( t ) = Bv ( t ) Fc Mv v ( t ) + F (t ) Fp Fc v ( t ) = 0 F n if if if v (t ) > 0 v (t ) = 0 v (t ) < 0

Main Simulink Diagram


x
position

F input force

f orce v elocity

position v velocity

dynamic system

Dynamic System Subsystem


B B 1 force -K1/M 1 s velocity integrator 1 s position integrator 2 velocity

1 position

positive velocity
Out1 if { }

negative velocity Merge


Out1elseif { }

if (u1 > 0)

zero velocity
Out1 else { }

elseif (u1 < 0)

u1

else

If

Initialization Function in Model Properties Callbacks clear all, close all, clc

data = load('data.txt'); t = data(:,1); % time [s] dt = t(2) - t(1); % sample period [s] tfinal = max(t); % final time [s] F = [t data(:,2)]; % input force [N] xe = data(:,3); % experimental position [m]

% simulation parameters % t = linspace(0,2,20001)'; % time [s] % F = [t 4*sign(sin(2*pi*t))]; % square wave input voltage [V] % F = [t 4*sin(2*pi*t)]; % sine wave input voltage [V] % F = [t 4*(2/pi)*asin(sin(2*pi*t))]; % triangle wave input voltage [V] dt = t(2) - t(1); % sample period [s] tfinal = max(t); % final time [s]

x0 = 0; % initial position [m] v0 = 0; % initial velocity [m/s]

% define model parameters M = 4; % mass [kg] B = 2; % damping coefficient [N/(m/s)] Fp = 0.1; % Coulomb friction magnitude in positive direction [N] Fn = 0.15; % Coulomb friction magnitude in negative direction [N] Stop Function in Model Properties Callbacks figure, subplot(2,1,1), plot(t,x,'k-',t,xe,'k:')

set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') legend('simulation','experimental','Location','NorthWest','Orientation','Horizontal') legend('Boxoff'), ylabel('position (m)') subplot(2,1,2); plot(t,F(:,2),'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') xlabel('time (s)'), ylabel('force (N)')

figure, subplot(2,1,1), plot(t,x,'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') ylabel('position (m)') subplot(2,1,2); plot(t,F(:,2),'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') xlabel('time (s)'), ylabel('force (N)')

lhndl = findobj('type','line'); set(lhndl,'linewidth',1.5,'color','black') h = findobj('type','axes'); set(h,'linewidth',1.5)

To use Simulink, make sure the Current Directory in the Command Window is set to the directory containing your Simulink model. In the Model Explorer Window, under Model Properties, code can be inserted into the InitFcn and the StopFcn. The simulation parameters can be set by pressing Ctrl+E. Under Simulation Time, set the Start Time to 0.0 and Stop Time to tfinal, where the variable tfinal is set in the Callback InitFcn. Under Solver Options, set Type to Fixedstep, set Solver to ode4 (RungeKutta), and Fixedstep size to dt, where the variable dt is set in the Callback InitFcn. Some common blocks are given below.

Coulomb and Viscous Friction block Located in Discontinuities The output is a linear and nonlinear function of a velocity.

Coulomb friction

From Workspace block Located in Sources The output drives the simulation. The variable is a vector. The first column is time and the subsequent columns are the inputs corresponding to time. Gain block Located in Math Operations The output is the input multiplied by a number.

input input

K gain

Integrator block Located in Continuous Output is integral of input. Initial condition of output is contained in block. Mux block Located in Signal Routing The output is a vector composed of the input scalars. Subsystem block Located in Ports & Subsystems The output(s) are a complex function of the input(s). The complex function is described by a Simulink diagram. Sum block Located in Math Operations The output is a summation of the inputs. To Workspace block Located in Sinks The signal input to this block creates a vector of this signal at each sample period. Set the Save Format to Array.

1 s integrator

in

out

subsystem

output output

An ifthen diagram is given below.

if
In1 if { } Out1

if (u1 > 0)

1 in

u1 elseif (u1 < 0) elseif { } Out

Merge

1 out

elseif
else

If
In else { } Out

else

You might also like