MATLAB: Sequential calling to SIMULINK from a time marching MATLAB simulation

MATLABode45simsimulinktime marching simulation

I have a compex MATLAB simulation model of an aircraft. The simulation uses MATLAB's ode45 solver to integrate the equations of motions in time. I'm trying to implement a flight control system (FCS) in SIMULINK which will continuosly provide the required control commands to the simulation during each integration step (using the "sim" command). The only way I could achieve this is: 1. Integrate the aircraft state equations during a short time interval (say dt). 2. Call the sim command using the current aircraft states (speed, angular rates, etc…) and the last step complete SimState of the FCS, and advance the control system by this short time interval (dt), in order to produce the next time step control command to the aircraft. 3. Save a complete SimState to the workspace, which will be used during the next sim command as initial conditions. 4. Advance simulation time by dt and return to step 1.
The problem is that this technique (saving the complete SimState at the end of each time step, and reloading it prior to the next step "sim" command) is both time consuming, and prevents the use of the 'rapid' acceleration mode.
My question is: How can I run SIMULINK continuously, such that my model's internal workspace is kept between successive calls, so that I would just have to update the input ports values in between calls to the model, and continue the simulation for the next time step ? (Obviously, I would need access to the model input and output ports inorder to provide continuos input to the model, and extract the output command).

Best Answer

Instead of calling SIM manually, I think a better approach might be to use one of three MATLAB custom blocks that will let you run MATLAB code inside a block. You can use this block to perform your ode45 computations and output the results to be used as inputs for the rest of the model.