Matlab + Simulink Crash while running CarSim S Function in loop

Hello,
I am using CarSim S-Function in Simulink and calling it using Matlab command as written below:
warning('off')
K = lqr(A,B(:,2),Q,R);
beep off;
simout = sim('model', 'SrcWorkspace','current');
This is in a loop which runs about 2000 - 5000 times (it is an optimization loop) to calculate fitness values from real-time simulation.
The program runs properly but randomly crashes after 100 - 200 iterations. I observed the crash and it occurs randomly during which Simulink and Matlab crash with no crash log or continue message. It just instantly closes along with simulink.
My Simulink .mdl model has no errors/warnings or algebraic loop warnings.
I have attached an image of my simulink block. Below is the complete code that is called to run in a loop
function z = pfot(x, A, B, C, D)
Q = [ x(1) 0 0 0;
0 x(2) 0 0;
0 0 x(3) 0;
0 0 0 x(4)];
R = 1;
%% Ensuring system is stable
E = eig(Q);
if all(E > 0)
try
warning('off')
K = lqr(A,B(:,2),Q,R);
beep off;
simout = sim('model', 'SrcWorkspace','current');
truckAcc = simout.Ay1.Data;
traiAcc = simout.Ay2.Data;
%Getting peak lateral acc
peakAccTruck = max(abs(truckAcc));
peackAccTrail = max(abs(traiAcc));
%rwa = info3.Max/info2.Max;
rwa = peackAccTrail/peakAccTruck;
errorRWA = abs(1 - rwa);
%PFOT Using Target
targetX = simout.xTarget.Data;
targetY = simout.yTarget.Data;
trailerX = simout.Xo2.Data;
trailerY = simout.Yo2.Data;
PFOTX = abs(sum(targetX - trailerX));
PFOTY = abs(sum(targetY - trailerY));
PFOT = (PFOTX+PFOTY)/2;
if (errorRWA > 2)% || peackAccTrail > 0.65 || peakAccTruck > 0.65 )
z = [inf inf]';
else
z = [errorRWA PFOT]';
end
catch
z = [inf inf]';
end
else
z = [inf inf]';
end
end
simulink.png

2 Comments

In the block diagram atatched, a Gain block with K*u value can be seen. Its marked in red, which possibly denotes an error. Can you open it and check what the error message is
Hey Vineeth,
Thank you for your response. The K*u block is red as it needs a value from the matlab file. When I fun it it is fine. I took the screenshot without running it

Sign in to comment.

Answers (0)

Products

Release

R2018b

Asked:

on 6 Apr 2019

Commented:

on 16 Apr 2019

Community Treasure Hunt

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

Start Hunting!