Changing initial conditions of a Simulink model when simulating using a for loop

10 views (last 30 days)
I am trying to run a vehicle simulink model (taken from here) for 5 seconds. When I try running it directly, the response seems to make sense.
However, when I use a for loop to do the same using a for loop (see attached runExample.m), the response is quite different. My assumption is I am not changing the initial conditions after each loop properly. Is there a way to update the initial conditions automatically instead of the way I am doing it, i.e., manually? What other ways could I trye to have the same responses with and without the for loop?
All the relevant files are uploaded. Simply set runDirect in the runExample.m file to true to run without the for loop, otherwise, set it to false.
Thanks in advance for your time. Looking forward to get some solution.
  3 Comments
J AI
J AI on 6 Jun 2023
Thanks for asking. Let me try to break down:
if runDirect == true
out = sim('DynamicModel',totalTime);
figure()
plot(out.x.data,out.y.data,'b','LineWidth',2)
xlabel('x (m)')
ylabel('y (m)')
else
The above code inside runExample.m runs the simulink model for 5 seconds and produces the following output (blue):
However, what I am trying to do is: instead of running the whole model in one-go, run the model for each time step, i.e., dt = 0.1 s, using for loop by passing the final boundary condition as the initial boundary condition at the end of the loop. This of course requires that all the boundary conditions be updated at the end of the loop, but I am not sure how to identify these boundary conditions in a simulink model. I have manually identified some [x0,y0,yaw0,yaw_rate0,vx0,vy0,volume0,omega0,z0,zdot0], but it seems like these are not sufficient since the output using the for loop is quite different.
In other words, I want to be able to store the boundary conditions at the end of each iteration so that they can be used in the next iteration.
Hope that clarifies the question further. Please let me know if you need any other information.
Thanks in advance!
P.S. I have slightly updated the files. So re-attaching them.
J AI
J AI on 7 Jun 2023
Here is a very minimal example of what I want but using script (instead of simulink). Here, we just have one boundary condition, y0, and thus changing it in every loop results in perfect match for both the outputs.
% run directly
dt = 0.1;
y0 = 1;
time = 0:dt:5;
[t, y] = ode45(@odeExample, time, y0);
% using for loop
y0 = 1;
tspan = 0:dt/10:dt;
y_loop = y0;
for i = 2:length(time)
[t,y_arr] = ode45(@odeExample, tspan, y0);
y0 = y_arr(end);
y_loop = [y_loop, y0];
end
hold on
plot(time, y, 'r','LineWidth',2);
plot(time, y_loop, 'b--','LineWidth',2);
xlabel('Time');
ylabel('y');
title('Solution of dy/dt = -2y');
legend('Direct','For loop')
function dydt = odeExample(t, y)
dydt = -2 * y;
end

Sign in to comment.

Answers (1)

Paul
Paul on 11 Jun 2023
I think you can do what you want using Model Operating points. Here's a doc page to get started.
Why do you want to do this: instead of running the whole model in one-go, run the model for each time step, i.e., dt = 0.1 s, using for loop by passing the final boundary condition as the initial boundary condition at the end of the loop.
Is there a need to take action at the end of one time step and before starting the simulation for the next time step? Asking because there may be an alternative to accomplish whatever the goal is.
  4 Comments
J AI
J AI on 14 Jun 2023
"In line 32, what is the value of the variable DynamicModel?": I am not sure what you mean. To my understanding, the simulink model I uploaded has several model operating points, of which I have identified some. Changing those variables manually at the end of each loop manually does not solve my issue. So I was hoping the SaveFinalState option would allow me to do that automatically in the variable xfinal.
"Why not implement the control algorithm in the Simulink model itself?": The control algorithm is coded in Python using its neural network libraries. I am not proficient and lack quite a bit of understanding on the issue of communicating between python and matlab. With simulink, I believe there is another added layer of complexity.
Paul
Paul on 14 Jun 2023
In this line
set_param(DynamicModel, 'SaveFinalState', 'on', 'FinalStateName', 'xfinal', 'SaveFormat', 'Structure with time')
DynamicModel is not in quotes as would be typical in set_param usage. Are you sure it shouldn't be
set_param('DynamicModel', 'SaveFinalState', 'on', 'FinalStateName', 'xfinal', 'SaveFormat', 'Structure with time')

Sign in to comment.

Categories

Find more on Automotive Applications in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!