MATLAB Answers

Different (Simulink) simulation results when called from App designer than a matlab script (.m)

18 views (last 30 days)
Ruta Czaplinska
Ruta Czaplinska on 19 Apr 2021 at 12:30
Commented: Ruta Czaplinska on 19 Apr 2021 at 15:35
I have a matlab app which takes in some properties and want to get it to simulate based on these in simulink, then show the results. I have a script which includes some while/if functions, to control the simulation:
%% Sim Setup
close all
load('Battery_2.mat')
%% Operating Conditions
SOC_Initial = 0.95;
SOC_Target = 0.95;
T_Initial = 15;
T_Amb = 15;
Weather = 0;
if(Weather==1)
Weatherstr = 'Rough';
else
Weatherstr = 'Calm';
end
s = 112;
%% Day Cycle
Run_Time = 14.5 * (60 * 60); %Day run time in seconds
Wait_Time = 20 * 60; %turnaround time when not charging in seconds
Home = 1; %=1 if hovercraft is at day start point
Charge_Threshold = 0.25; %SOC at which full charge is initiated
Charge_at_home = 1;
Charge_at_dest = 0;
%% Drive Cycle
%Acceleration profile
HAH = 0.1; %Home acceleration from dock to harbour speed in m/s/s
HVH = 10 / 1.944; %Home max velocity in harbour in m/s
HXH = 135; %Home distance within harbour in m
HAC = 0.2; %Home acceleration from harbour speed to cruising speed in m/s/s
VC = 30 / 1.944; % Cruising speed in m/s
%Deceleration profile
DAC = 0.2; %Destination deceleration from cruising to harbour speed in m/s/s
DXH = 135; %Destination distance within harbour in m
DVH = 10 / 1.944; %Destination velocity in harbour in m/s
DAH = 0.1; %Destination deceleration from harbour speed to dock in m/s/s
Xtrip = 7250; %Trip distance in m
HX = HXH + ((VC^2 - HVH^2)/(2*HAC)); %Distance travelled to cruising speed in m
DX = DXH + ((VC^2 - DVH^2)/(2*DAC)); %Distance travelled from cruising speed in m
XC = Xtrip - HX - DX; %Cruising distance
TC = XC / VC; %Time at cruising speed
OTimeq = []; OTimeq(1)=0; OTimeq(2)=(OTimeq(1) + HVH/HAH); OTimeq(3)=(OTimeq(2)/2 + HXH/HVH); OTimeq(4)=(OTimeq(3) + (VC-HVH)/HAC); OTimeq(5)=(OTimeq(4) + TC); OTimeq(6)=(OTimeq(5) + (VC-DVH)/DAC); OTimeq(7)=(OTimeq(6) + DXH/DVH - DVH/(2*DAH)); OTimeq(8)=round(OTimeq(7) + DVH/DAH);
OVelocityq = [0 HVH HVH VC VC DVH DVH 0];
ITimeq = []; ITimeq(1)=0; ITimeq(2)=(ITimeq(1) + DVH/DAH); ITimeq(3)=(ITimeq(2) + DXH/DVH - DVH/(2*DAH)); ITimeq(4)=(ITimeq(3) + (VC-DVH)/DAC); ITimeq(5)=(ITimeq(4) + TC); ITimeq(6)=(ITimeq(5) + (VC-HVH)/HAC); ITimeq(7)=(ITimeq(6) + HXH/HVH - HVH/(2*HAH)); ITimeq(8)=round(ITimeq(7) + HVH/HAH);
IVelocityq = flip(OVelocityq);
figure(1)
plot(OTimeq./60,OVelocityq)
hold on
title('Outbound Velocity Profile')
xlabel('Time (min)')
ylabel('Hovercraft Velocity (m/s)')
%% Run Day Simulation
while Time_Total<Run_Time && ans.End==0
%Run time loop
while Time_Total<Run_Time && ans.End==0 && SOC_Initial>Charge_Threshold
%Discharge cycles
if (Time_Total<Run_Time && Home==1 && ans.End==0 && SOC_Initial>Charge_Threshold)
% Run Outward Trip
State = 1;
Time = linspace(0,OTimeq(end),(OTimeq(end)+1))';
Duration = Time(end);
Velocity = interp1(OTimeq,OVelocityq,Time);
set_param('Sim_Powertrain_1','Solver','VariableStepAuto','MaxStep','1')
sim('Sim_Powertrain_1.slx');
Home = 0;
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
if (Time_Total<Run_Time && ans.End==0 && SOC_Initial>Charge_Threshold)
%Run turnaround cycle
if ((Home==1 && Charge_at_home==1) || (Home==0 && Charge_at_dest==1))
State = -1;
SOC_Change = ans.SOC_Cell(end) - SOC_Target;
Capacity_Change = SOC_Change * Cell_Capacity;
SOC_Range = linspace(ans.SOC_Cell(end),SOC_Target,100)';
ITimeT = interp1(T_ILim,ITime',ans.T_Cell(end));
Duration = min([60.*60.*Capacity_Change./mean(interp1(SOC_ICharge,ITimeT,SOC_Range)) Wait_Time]);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
elseif ((Home==1 && Charge_at_home==0) || (Home==0 && Charge_at_dest==0))
State = 1;
Time = [0;Wait_Time];
Velocity = [0;0];
Duration = Time(end);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
end
end
end
if (Time_Total<Run_Time && Home==0 && ans.End==0 && SOC_Initial>Charge_Threshold)
% Run Inbound Trip
State = 1;
Time = linspace(0,ITimeq(end),(ITimeq(end)+1))';
Duration = Time(end);
Velocity = interp1(ITimeq,IVelocityq,Time);
set_param('Sim_Powertrain_1','Solver','VariableStepAuto','MaxStep','1')
sim('Sim_Powertrain_1.slx');
Home = 1;
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
if (Time_Total<Run_Time && ans.End==0 && SOC_Initial>Charge_Threshold)
%Run turnaround cycle
if ((Home==1 && Charge_at_home==1) || (Home==0 && Charge_at_dest==1))
State = -1;
SOC_Change = ans.SOC_Cell(end) - SOC_Target;
Capacity_Change = SOC_Change * Cell_Capacity;
SOC_Range = linspace(ans.SOC_Cell(end),SOC_Target,100)';
ITimeT = interp1(T_ILim,ITime',ans.T_Cell(end));
Duration = min([60.*60.*Capacity_Change./mean(interp1(SOC_ICharge,ITimeT,SOC_Range)) Wait_Time]);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
elseif ((Home==1 && Charge_at_home==0) || (Home==0 && Charge_at_dest==0))
State = 1;
Time = [0;Wait_Time];
Velocity = [0;0];
Duration = Time(end);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
end
end
end
end
if (Time_Total<Run_Time && ans.End==0 && SOC_Initial<=Charge_Threshold)
% Run charge cycle
if ((Home==1 && Charge_at_home==1) || (Home==0 && Charge_at_dest==1))
State = -1;
SOC_Change = ans.SOC_Cell(end) - SOC_Target;
Capacity_Change = SOC_Change * Cell_Capacity;
SOC_Range = linspace(ans.SOC_Cell(end),SOC_Target,100)';
ITimeT = interp1(T_ILim,ITime',ans.T_Cell(end));
Duration = 60 .* 60 .* Capacity_Change ./ mean(interp1(SOC_ICharge,ITimeT,SOC_Range));
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
elseif (Home==1 && Charge_at_home==0)
% Wait then Run Outward Trip
State = 1;
Time = [0;Wait_Time];
Velocity = [0;0];
Duration = Time(end);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
State = 1;
Time = linspace(0,OTimeq(end),(OTimeq(end)+1))';
Duration = Time(end);
Velocity = interp1(OTimeq,OVelocityq,Time);
set_param('Sim_Powertrain_1','Solver','VariableStepAuto','MaxStep','1')
sim('Sim_Powertrain_1.slx');
Home = 0;
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
elseif (Home==0 && Charge_at_dest==0)
State = 1;
Time = [0;Wait_Time];
Velocity = [0;0];
Duration = Time(end);
set_param('Sim_Powertrain_1','Solver','FixedStepAuto','FixedStep','1')
sim('Sim_Powertrain_1.slx');
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
State = 1;
Time = linspace(0,ITimeq(end),(ITimeq(end)+1))';
Duration = Time(end);
Velocity = interp1(ITimeq,IVelocityq,Time);
set_param('Sim_Powertrain_1','Solver','VariableStepAuto','MaxStep','1')
sim('Sim_Powertrain_1.slx');
Home = 1;
SOC = [SOC;ans.SOC_Cell];
Temperature = [Temperature;ans.T_Cell];
Current = [Current;ans.Pack_Current];
Voltage = [Voltage;ans.Pack_Voltage];
Power = [Power;ans.Pack_Power];
SOC_Initial = ans.SOC_Cell(end);
T_Initial = ans.T_Cell(end);
Time_Total = Time_Total + ans.tout(end) + 1;
end
end
end
%% Plot Results
TIME = linspace(0,(Time_Total-1),(Time_Total))';
figure(2)
plot(TIME./3600,Current)
hold on
title(strcat(Weatherstr,' Weather - Pack Current'))
xlabel('Time (hours)')
ylabel('Current (A)')
figure(3)
plot(TIME./3600,Voltage)
hold on
title(strcat(Weatherstr,' Weather - Pack Voltage'))
xlabel('Time (hours)')
ylabel('Voltage (V)')
figure(4)
plot(TIME./3600,Temperature)
hold on
title(strcat(Weatherstr,' Weather - Cell Temperature'))
xlabel('Time (hours)')
ylabel('Temperature (degC)')
figure(5)
plot(TIME./3600,Power)
hold on
title(strcat(Weatherstr,' Weather - Pack Power'))
xlabel('Time (hours)')
ylabel('Power (W)')
figure(6)
plot(TIME./3600,SOC)
hold on
title(strcat(Weatherstr,' Weather - Pack SOC'))
xlabel('Time (hours)')
ylabel('SOC')
When I run it, it returns graphs as expected. When I copied and pasted the code from the script to the app, the results came out different. I then tried calling the script from app designer, instead of copying and the same issue came up.
Any ideas what could be causing it or potential work arounds?

Answers (1)

Fangjun Jiang
Fangjun Jiang on 19 Apr 2021 at 14:05
This happened several times now and it might be related.
Give a return variable when you call sim() such as Result=sim(...). Otherwite, the results from previous simulation run in the workspace is not over-written. Search in the answers. It resolved the problem at least three times.
  5 Comments
Ruta Czaplinska
Ruta Czaplinska on 19 Apr 2021 at 15:35
I added a function in app designer:
function Script_Powertrain_4(app)
Script_Powertrain_4
end
and called it, it showed the wrong results again.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!