Mark
Mark

Reputation: 347

How to set initial condition t=0 for Simulink integrator loop

I have a differential equation: dx/dt = a * x. Using Matlab Simulink, I need to solve this equation and output it using Scope block.

The problem is, I don't know how to specify an initial condition value t = 0.

So far I have managed to create a solution that looks like this:

Model

I know that inside integrator, there is a possiblity to set "Initial condition" but I can't figure out how that affects the final result. How can I simply set the value of x at t = 0; e.i. x(0) = 6?

Upvotes: 0

Views: 13660

Answers (2)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

The way to think about the integrator blocks is either completely in the Laplace picture, or as representing the equivalent integral equation for the IVP

y'(t)=f(t,y(t)),  y(0) = y_0

is equivalent to

y(t) = y_0 + int(s=0 to t) f(s,y(s)) ds

The feed-back loop in the block diagram realizes almost literally this fixed-point equation for the solution function.

So there is no need for complicated constructions and extra blocks.

Upvotes: 0

Wolfie
Wolfie

Reputation: 30046

Let's work this problem through analytically first so we know if the model is correct.

   dx/dt = a*x       % Seperable differential equation
=> (1/x) dx = a dt   % Now we can integrate
=> ln(x)  = a*t + c  % We can determine c using the initial condition x(0)
=> ln(x0) = a*0 + c
=> ln(x0) = c 
=> x = exp(a*t + ln(x0)) % Subbing into 3rd line and taking exp of both sides
=> x = x0 * exp(a*t)

So now we have an idea. Let's look at this for t = 0 .. 1, x0 = 6, a = 5:

% Plot x vs t using plain MATLAB
x0 = 6; a = 5; 
t = 0:1e-2:1; x = x0*exp(a*t);
plot(t,x)

figure


Now let's make a Simulink model which acts as a numerical integrator. We don't actually need the Integrator block for this application, we simply want to add the change at each time step!

model

To run this, we must first set a couple of things up. In Simulation > Model Configuration Parameters, we must set the time step to match the time step we've used to switch between dx/dt and dx (2nd Gain block).

config

Lastly, we must set the initial condition for x0, this can be done in the Memory block

initial

Setting the end time to 1s and running the model, we see the expected result in the Scope. Because it matches our analytical solution, we know it is correct.

scope


Now we understand what's going on, we can re-introduce the integration block to make the model more flexible. Using the integrator means that dt is automatically calculated, and we don't need to micro-manage the Gain block, in fact we can get rid of it. We still need a Memory block though. We now also need intial conditions in both the integrator, and the memory block. Put scopes in different locations and just complete the first few time steps to work out why!

simple

Note that the initial conditions are less clear when using the integrator block.

Upvotes: 2

Related Questions