Bug with for loop increment
Show older comments
Hello,
I am using a funciton in a simulink Model. This function has 5 Inputs and 1 output.
I need a 100 lines * 9 col. as output but I get a 5100 * 9 matrix...
During a debugg try I observed that the ii for loop is resetting to 1 after reaching the maximum value. How is it possible?
I know that the output is writting 3 times in the first column and I habe to work on that, but even though the output fills the column after the end of the first 100 values (kk) I should get max 300 values in one column, not 5100!!!
THe actual code is below:
length(F_Spring) = 100
FS_op_all_hill is a 100 lines per 3 columns matrix
Axial_Fric is a 3 lines per 1 column matrix
Lever_Arm_Opening is a constant
Open_out should be a 100 lines oer 9 columns matrix.
function Open_out = fcn(F_Spring, FS_op_all_hill, Axial_Fric, Lever_Arm_Sp,Lever_Arm_Opening)
Open_out = zeros(100,9);
y = zeros(100,3);
%%!!! Attention, has to be modified : result should be a matrix of 100 lines for 9 columns !!!!!%%%
for jj = 1 : 3 : 9
for ii = 1 : 3
for kk = 1 : length(F_Spring)
y(kk,ii) = F_Spring(kk,1) - FS_op_all_hill(kk,ii);
Open_out(kk,jj) = (y(kk,ii) - Axial_Fric(ii,1)) * Lever_Arm_Sp(kk,1) / Lever_Arm_Opening *(-1);
end
ii = ii +1;
end
jj = jj + 1;
end
Hier is a screen shot of a part of the Simulink model:

Has anybody an idea where the Problem comes from?
Thanks
11 Comments
These lines are unlikely to do anything useful:
ii = ii +1;
jj = jj + 1;
MATLAB for loops already specify the loop variable. Your changes to ii and jj at the end of the loops will be simply overwritten at the start of the next loop iterations.
"During a debugg try I observed that the ii for loop is resetting to 1 after reaching the maximum value. How is it possible?"
Because that is what nested loops do.
Cyril CRIER
on 19 Feb 2021
Edited: Cyril CRIER
on 19 Feb 2021
Walter Roberson
on 19 Feb 2021
How exactly are you getting F_Spring in as a signal? Is column 1 of it the timestamps and you are using Discrete solver with timestep exactly equal to the difference in timestamps and algebra carefully designed to not have any 3:1 rate transitions?
It looks to me as if you have a From Spreadsheet block (or equivalent) and that you have signal interpolation going on that happens to be 3 samples for every time difference in the file.
Cyril CRIER
on 19 Feb 2021
Walter Roberson
on 19 Feb 2021
I suspect that you might have continuous solver in effect and that it is sampling three times as fast as you expected.
Cyril CRIER
on 22 Feb 2021
Cyril CRIER
on 9 Mar 2021
Cyril CRIER
on 10 Mar 2021
Edited: Cyril CRIER
on 10 Mar 2021
Walter Roberson
on 10 Mar 2021
I don't want to change solvers without fully understanding the functionning
It could take a long time to fully understand the implications of all of the solver parameters, and you are unlikely to learn the implications without experimenting.
Take a copy of your system, change the solver to discrete; if everything goes haywire, revert to the continuous version.
Cyril CRIER
on 16 Mar 2021
Answers (0)
Categories
Find more on Discontinuities in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




