MATLAB Answers

Update S-function Parameters in Fast Restart

1 view (last 30 days)
Enrico Anderlini
Enrico Anderlini on 7 Feb 2018
Edited: Meeshawn Marathe on 1 Mar 2018
I have read the Mathworks documentation carefully and tried to find a solution on forums as well. However, I have not been able to find a solution to my problem yet.
I am using Matlab/Simulink to simulate the dynamics of a vehicle, which picks up an object during operation. The way I am planning to do this is to:
  • Simulate the motions of the vehicle by itself at the start of the simulation;
  • Stop the simulation, save the final state, update the model parameters (so that they now represent the vehicle and the object)
  • Initialize the simulation again starting from the end time of the previous run and using the previous final state as input state to the new simulation.To do this, I have been using the options FastRestart, SaveFinalState, SaveCompleteFinalSimState and update. Unluckily, the simulation runs seamlessly, but the model parameters are not updated.
The dynamics of the vehicle and vehicle+body are modelled by the same C-coded S-function. This function receives the parameters of the dynamic equations (e.g. inertia, damping, etc.) as parameters to the S-function block. I think this is the main problem with my approach: even though I run the update command, the S-function does not recognize the update of the parameters in the workspace. Do I need to recompile it? I guess that is not feasible under Fast Restart mode, is it?
Any advice is really appreciated! Thank you!

Answers (1)

Meeshawn Marathe
Meeshawn Marathe on 1 Mar 2018
Edited: Meeshawn Marathe on 1 Mar 2018
Try this exercise with a simple model ('vdp') first:
The following piece of code helps you automate the process of saving the last state (let's say xFinal) and using it as the initial state for the second run of your simulation:
vdp % Opens the Van-der-Pol Oscillat0r Demo
set_param('vdp','SaveFinalState','on') % Saves the final states of the model
set_param('vdp','FinalStateName','xFinal')% Gives a name to the final states
sim('vdp')% Simulates the model
xFinal% Displays the value in Command Window for reference
set_param('vdp','SaveFinalState','off')% To prevent overwriting of xFinal for sake of comparison
set_param('vdp','LoadInitialState','on')% Set this on to load the initial state
set_param('vdp','InitialState','xFinal')% Load the initial state from the final state of the previuos simulation
set_param('vdp','SaveState','on','StateSaveName','xout')% Save states for all the time values
sim('vdp')% simulate 2nd time
xout(1,:)% Check for the initial state which equals the final state from previous simulation
If this works, try the same for your model. Comment back if this does not work.

Community Treasure Hunt

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

Start Hunting!