Make Error in Simulink – Division by Zero?

24 views (last 30 days)
I looked up Make Errors here, and most of the issues were solved by calling 'mex -setup'. I gave that a shot, and everything looked fine. I'm using MATLAB R2011a Student Version along with its built-in lcc C Compiler.
My Simulink model consists of two MATLAB functions, some constants and 'From Workspace' sources. One of the MATLAB functions is 154 lines long and includes four extrinsic functions, all of which are declared as such at the beginning of the function. The other is basically a placeholder and passes the outputs as the same as inputs.
Here are the errors, buried in a long error report:
Error c2_cogen2_simulink.c: 5380 Division by zero
Error c2_cogen2_simulink.c: 5383 Division by zero
Error c2_cogen2_simulink.c: 5411 Division by zero
Here's the full error report, in case it sheds more light on the issue:
Making simulation target "cogen2_simulink_sfun", ...
C:\Users\Zack\Documents\Italy Research\cogen1\slprj\_sfprj\cogen2_simulink\_self\sfun\src>"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lccmake" -f cogen2_simulink_sfun.lmk
Warning c1_cogen2_simulink.c: 482 static `void function(pointer to void,pointer to const incomplete struct mxArray_tag defined at C:\Program Files (x86)\MATLAB\R2011a Student\extern\include\matrix.h 298,pointer to const char,pointer to void) c1_c_sf_marshallIn' is not referenced
Warning c1_cogen2_simulink.c: 455 static `pointer to const incomplete struct mxArray_tag defined at C:\Program Files (x86)\MATLAB\R2011a Student\extern\include\matrix.h 298 function(pointer to void,pointer to void) c1_c_sf_marshallOut' is not referenced
0 errors, 2 warnings
Error c2_cogen2_simulink.c: 5380 Division by zero
Error c2_cogen2_simulink.c: 5383 Division by zero
Error c2_cogen2_simulink.c: 5411 Division by zero
Warning c2_cogen2_simulink.c: 6780 static `void function(pointer to void,pointer to const incomplete struct mxArray_tag defined at C:\Program Files (x86)\MATLAB\R2011a Student\extern\include\matrix.h 298,pointer to const char,pointer to void) c2_u_sf_marshallIn' is not referenced
Warning c2_cogen2_simulink.c: 6753 static `pointer to const incomplete struct mxArray_tag defined at C:\Program Files (x86)\MATLAB\R2011a Student\extern\include\matrix.h 298 function(pointer to void,pointer to void) c2_u_sf_marshallOut' is not referenced
Warning c2_cogen2_simulink.c: 6671 static `double function(pointer to SFc2_cogen2_simulinkInstanceStruct,double) c2_sign' is not referenced
Warning c2_cogen2_simulink.c: 6570 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,int,double,int,int,pointer to double,int,int,pointer to double,pointer to int,int,int,pointer to double,pointer to int) c2_ceval_xger' is not referenced
Warning c2_cogen2_simulink.c: 6524 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,int,double,pointer to double,pointer to int,int,int,pointer to double,pointer to int,int,int,double,pointer to double,int,int,pointer to double) c2_ceval_xgemv' is not referenced
Warning c2_cogen2_simulink.c: 6480 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,int,pointer to double,pointer to int,int,int,pointer to double,pointer to int,int,pointer to double,pointer to double) c2_eml_blas_xgemv' is not referenced
Warning c2_cogen2_simulink.c: 6452 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,int,int,double,pointer to double,pointer to int,int,int,pointer to double,pointer to double,pointer to int,pointer to double) c2_eml_matlab_zlarf' is not referenced
Warning c2_cogen2_simulink.c: 6443 static `double function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,double,double,int,int) c2_b_ceval_xscal' is not referenced
Warning c2_cogen2_simulink.c: 6434 static `double function(pointer to SFc2_cogen2_simulinkInstanceStruct,double,double) c2_b_eml_xscal' is not referenced
Warning c2_cogen2_simulink.c: 6425 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,double,double,pointer to double,pointer to double,pointer to double) c2_b_eml_matlab_zlarfg' is not referenced
Warning c2_cogen2_simulink.c: 6403 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,double,pointer to double,pointer to int,int,int,pointer to double,pointer to int) c2_ceval_xscal' is not referenced
Warning c2_cogen2_simulink.c: 6383 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,double,pointer to double,pointer to int,int,pointer to double,pointer to int) c2_eml_xscal' is not referenced
Warning c2_cogen2_simulink.c: 6331 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,double,pointer to double,pointer to int,int,pointer to double,pointer to double,pointer to int,pointer to double) c2_eml_matlab_zlarfg' is not referenced
Warning c2_cogen2_simulink.c: 6309 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,pointer to double,pointer to int,int,int,int,int,pointer to double,pointer to int) c2_ceval_xswap' is not referenced
Warning c2_cogen2_simulink.c: 6287 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,pointer to double,pointer to int,int,int,int,int,pointer to double,pointer to int) c2_eml_refblas_xswap' is not referenced
Warning c2_cogen2_simulink.c: 6265 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,pointer to double,pointer to int,int,int,int,int,pointer to double,pointer to int) c2_eml_blas_xswap' is not referenced
Warning c2_cogen2_simulink.c: 6245 static `void function(pointer to SFc2_cogen2_simulinkInstanceStruct,int,pointer to double,pointer to int,int,int,pointer to double,pointer to int) c2_eml_xswap' is not referenced
3 errors, 17 warnings
"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lcc.exe" -c -Zp8 -DMATLAB_MEX_FILE -noregistrylookup -I"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\extern\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\simulink\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\mex\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\debugger\include" "cogen2_simulink_sfun.c"
"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lcc.exe" -c -Zp8 -DMATLAB_MEX_FILE -noregistrylookup -I"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\extern\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\simulink\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\mex\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\debugger\include" "cogen2_simulink_sfun_registry.c"
"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lcc.exe" -c -Zp8 -DMATLAB_MEX_FILE -noregistrylookup -I"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\extern\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\simulink\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\mex\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\debugger\include" "c1_cogen2_simulink.c"
"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lcc.exe" -c -Zp8 -DMATLAB_MEX_FILE -noregistrylookup -I"C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\extern\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\simulink\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\mex\include" -I"C:\Program Files (x86)\MATLAB\R2011a Student\stateflow\c\debugger\include" "c2_cogen2_simulink.c"
C:\Program Files (x86)\MATLAB\R2011a Student\sys\lcc\bin\lccmake: Error code 1
Thanks for any help you can give.
Zack

Accepted Answer

Zachary Santer
Zachary Santer on 29 Nov 2011
Turns out, trying to use 'load' and 'save' as extrinsic functions in a MATLAB Function Block in Simulink doesn't work.
That situation has been replaced by global variables, Data Store Memory Blocks, and an If Action Subsystem. The If Action Subsystem has variables being loaded from the workspace through Constant Blocks and loaded into Data Store Write Blocks.
Everything works fine, now.
Thanks anyway,
Zack

More Answers (2)

Fangjun Jiang
Fangjun Jiang on 23 Nov 2011
I assume you understand what is division by zero error. Every time you do a division, in C or in MATLAB, the safest way is to check whether the denominator is zero.
if abs(b)<eps
% do something, such as assign b or c to a fixed number
else
c=a/b;
end
So check the code in your MATLAB function to see if you have such operation and if you have the safeguard.
In Simulink, you can turn on the diagnositcs. Under menu Simulation->Configuration Parameters...->Diagnostics-> Data validity, turn on the check for "Signal"->"Inf or NaN block output". So when you run simulation, hopefully it can help you find out these division by zero errors early on so you don't have to deal with it until generating code and compiling it.
  6 Comments
Fangjun Jiang
Fangjun Jiang on 23 Nov 2011
Yes. The C code is generated but the source my be in the M code, or could be the problem of the code generation.
Zachary Santer
Zachary Santer on 25 Nov 2011
Okay. I examined my M code carefully, found a couple of issues and fixed them. Running the simulation still gives the same error. I built the same simulation in another window, copying and pasting my M code, thinking that could do some good. I still got the same error.
Here's my M code. Sorry that it's going to look cruddy here.
function [command_el_MCI,MCI_grid_out] = ...
MCI(MCI_th_set,MCI_el_set,simulation_time,water_temp_in,water_temp_out,...
water_M_dot,Pel,m_dot_fuel)
%#codegen
persistent MCI_grid i predictor_time intercept_time corrector_count ...
corrector_wait corrector_coeff time_vector Pth_vector saved_time ...
Pel_vector m_dot_fuel_vector water_data max_index step_time stop_time
coder.extrinsic('load','save','now','medfilt1')
world_time = 0;
world_time = now;
%initialize everything
if isempty(MCI_grid)
%doing this before loading the variables from a file insures they are
%in a Simulink-compatible format
MCI_grid = zeros(16,5,26);
corrector_coeff = 1;
water_data = zeros(6,3);
step_time = 1;
stop_time = 30;
load('MCI_data.mat')
% load('MCI_grid.mat')
i = 0;
predictor_time = 180; %s
intercept_time = 90; %s
corrector_count = 0;
corrector_wait = 90; %s must be less than predictor time
% load('corrector_coeff.mat')
max_index = ceil(max([predictor_time intercept_time corrector_wait])/step_time);%/step_time
% load('water_data.mat')
time_vector = zeros(1,max_index);
Pth_vector = time_vector;
Pel_vector = time_vector;
m_dot_fuel_vector = time_vector;
saved_time = world_time;
end
hour_index = floor(rem(world_time,1)*24) + 1;
hour_part = rem(world_time*24,1);
if hour_part < 0.5
early_weight = 0.5 - hour_part;
center_weight = 0.5 + hour_part;
late_weight = 0;
% elseif hour_part == 0.5 redundant
% early_weight = 0;
% center_weight = 1;
% late_weight = 0;
else
early_weight = 0;
center_weight = 1.5 - hour_part;
late_weight = -0.5 + hour_part;
end
command_el_MCI_early = interp1(MCI_grid(:,4,hour_index-1),MCI_grid(:,3,hour_index-1),MCI_th_set,'linear','extrap');%point
command_el_MCI_center = interp1(MCI_grid(:,4,hour_index),MCI_grid(:,3,hour_index),MCI_th_set,'linear','extrap');
command_el_MCI_late = interp1(MCI_grid(:,4,hour_index+1),MCI_grid(:,3,hour_index+1),MCI_th_set,'linear','extrap');
command_el_MCI = command_el_MCI_early*early_weight + command_el_MCI_center*center_weight + command_el_MCI_late*late_weight;
%assumed temperature inputs are in C, water volumetric flow input is in L/sec
%water pressure passing through the engine is 2 Bar. The data used here is
%for 1 Bar. Fix this.
H_in = interp1(water_data(:,1),water_data(:,2),water_temp_in,'linear','extrap'); %kJ/kg
H_out = interp1(water_data(:,1),water_data(:,2),water_temp_out,'linear','extrap'); %kJ/kg
water_temp_ave = (water_temp_in + water_temp_out)/2; %degC
water_density = interp1(water_data(:,1),water_data(:,3),water_temp_ave,'linear','extrap');%kg/m^3
Pth = water_M_dot*water_density*0.001*(H_out - H_in);%kW
if i < max_index
i = i + 1;
time_vector(i) = simulation_time;
Pth_vector(i) = Pth;
Pel_vector(i) = Pel;
m_dot_fuel_vector(i) = m_dot_fuel;
data_length = i;
else
time_vector(1:max_index-1) = time_vector(2:max_index);
time_vector(max_index) = simulation_time;
Pth_vector(1:max_index-1) = Pth_vector(2:max_index);
Pth_vector(max_index) = Pth;
Pel_vector(1:max_index-1) = Pel_vector(2:max_index);
Pel_vector(max_index) = Pel;
m_dot_fuel_vector(1:max_index-1) = m_dot_fuel_vector(2:max_index);%point
m_dot_fuel_vector(max_index) = m_dot_fuel;
data_length = max_index;
end
%This part finds whether or not the MCI_grid predicted the current response
%correctly. If it did not, a correction factor is found and multiplied into
%the result from MCI_grid.
if simulation_time > predictor_time
cutoff = simulation_time - predictor_time;
ii = i;
while time_vector(ii)>cutoff
ii = ii - 1;
end
th_predictor = polyfit(time_vector(ii:i),Pth_vector(ii:i),1);
th_predicted = th_predictor(1)*(simulation_time + intercept_time) + th_predictor(2); %check this
th_delta_predicted = th_predicted - MCI_th_set;
th_delta = Pth - MCI_th_set;
if sign(th_delta_predicted) == sign(th_delta)
corrector_count = corrector_count + 1;
corrector_index = data_length - corrector_count;
if (simulation_time - time_vector(corrector_index)) > corrector_wait
if abs(Pth) < eps
corrector_count = 0;
else
correction = MCI_th_set/Pth;
corrector_coeff = corrector_coeff*correction;
corrector_count = 0;
end
end
else
corrector_count = 0;
end
end
%make routine to replace elements in MCI_grid
%use medfilt1
if i >= max_index %consider putting this under the else statement of the if structure begun on line 34
filter_order = 5; %consider other n's than 5
Pth_vector_filtered = zeros(size(Pth_vector));%point 'double'
Pel_vector_filtered = Pth_vector_filtered;
m_dot_fuel_vector_filtered = Pth_vector_filtered;
Pth_vector_filtered = medfilt1(Pth_vector,filter_order);
Pel_vector_filtered = medfilt1(Pel_vector,filter_order);
m_dot_fuel_vector_filtered = medfilt1(m_dot_fuel_vector,filter_order);
for MCI_grid_step = 1:length(MCI_grid(:,1,hour_index))
for filtered_vector_step = floor(filter_order/2):(max_index-floor(filter_order/2))
if (Pth_vector_filtered(filtered_vector_step) > (MCI_grid(MCI_grid_step,2,hour_index) - 0.5)) && (Pth_vector_filtered(filtered_vector_step) < (MCI_grid(MCI_grid_step,2,hour_index) + 0.5)) && ((MCI_grid(MCI_grid_step,1,hour_index) + (23/24)) < world_time)
MCI_grid(MCI_grid_step,1,hour_index) = world_time - (time_vector(max_index) - time_vector(filtered_vector_step))/86400; %serial date number of the time data was taken
MCI_grid(MCI_grid_step,3,hour_index) = Pel_vector_filtered(filtered_vector_step); %kW electric
MCI_grid(MCI_grid_step,4,hour_index) = Pth_vector_filtered(filtered_vector_step); %kW thermal power
MCI_grid(MCI_grid_step,5,hour_index) = m_dot_fuel_vector_filtered(filtered_vector_step); %l/s fuel
if hour_index == 2
MCI_grid(:,:,26) = MCI_grid(:,:,2);
elseif hour_index == 25
MCI_grid(:,:,1) = MCI_grid(:,:,25);
end
end
end
end
end
MCI_grid_out = MCI_grid;
if (simulation_time == stop_time) || ((saved_time + 4/24) < world_time);
save('MCI_data.mat','MCI_grid','corrector_coeff','-append')
% save('MCI_grid.mat','MCI_grid')
% save('corrector_coeff.mat','corrector_coeff')
saved_time = world_time;
end

Sign in to comment.


Kaustubha Govind
Kaustubha Govind on 23 Nov 2011
There seems to be have been a similar bug before. See Bug#534529. However, since it was fixed in R2009b, it is likely not the same issue.
You could however try the workaround suggested that Bug Report and see if it works for you:
  1. In the Simulink Editor, select Simulation > Configuration Parameters.
  2. In the Configuration Parameters dialog box, select Optimization.
  3. Change the Loop unrolling threshold value to 1, or to any number smaller than the length of the vector passed to norm.
If not, I would recommend reporting this to MathWorks Tech Support to see if this really is another bug.
  1 Comment
Zachary Santer
Zachary Santer on 23 Nov 2011
R2011a apparently doesn't have a 'Loop unrolling threshold' option in the Configuration Parameters dialog box. Additionally, I am not using 'norm.'
If I don't find a solution in the next day or two, I will go ahead and report this to Tech Support.
Thanks to both of you for your help.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!