0
votes

I want to model a for loop system in Simulink, how I can model the following MATLAB syntax into Simulink model?

N=3;
    for    i=0:1:N
    sum(i+1)=factorial(i)/factorial(N);  
    end

I have tried for loop sub systems in Simulink and also Sum block for iteration loop but doesn't help me. factorial function can be calculated with FCN function. Suggest me the ways to resolve this model with step time.

1
I suggest you change (in Matlab) factorial(i)/facrorial(N) to 1/prod(i+1:N). That will save computations an avoid overflow (if N is large). Also, it's best not to use i as a variable name - Luis Mendo
OK, it seems very efficient way, how can I save the computations if I have, factorial(N)/(factorial(i)*factorial(N-i)) ? Also, how can i avoid imaginary unit that gives efficiency? - user3396151
Replace for i= by for ii=; and replace factorial(N)/(factorial(i)*factorial(N-i)) by prod((N-ii+1:N)./[1:ii]) - Luis Mendo
sum(ii+1) remains there? does it affects the performance? - user3396151
This can be done quite easily using a MATLAB function block, but since the number of elements in the output is dependent on the value of N (which I assume is not really hard coded a 3, but rather can change during a simulation) you'll need to use variable sized signals. Search the doc for that term to learn how to define them. - Phil Goddard

1 Answers

0
votes

If you have the code already in matlab use a embedded matlab function to implement it i your simulink model. This is in general quite efficient since it will be compiled (compared to interpreted matlab function blocks)