How to solve higher order polynomial roots in Simulink faster in Rapid Accelerator Mode?

Hello! I tried to speed up my simulink model and eventually found the bottleneck of speed is the Matlab Fcn block that is iterating the second order polynomial roots by using the roots(Poly) command. (What I am doing here is to subtract the parameters of one curve from the other and get A1, A2 and A3; so the intersecting point is achieved by solving A1*X^2 + A2*X + A3 = 0 and only selects the positive root as output) I did some research and found that iterating polynomial roots in simulink is not recommended because of the slow speed; but, the performance is able to be increased by converting the Matlab code into C code and run it from there. However, right now my model is running in Rapid Accelerator mode (still with 9x real time speed though), so I think the model is already compiled as C when running; My first question is: Will the speed being further increased in Rapid Accelerator mode if the Matlab function block is converted into C code? If yes, how do I do it? Should I follow the instructions of Legacy Code tool or I can somehow convert it by right click the block and choose code generation? My second question is: Is there a better way for me to solve the polynomial roots in Simulink faster in Rapid Accelerator mode?
Thanks a lot!! Penn

Answers (2)

Sorry, the MATLAB Function related blocks were recently renamed (see http://www.mathworks.com/matlabcentral/answers/36656-what-kind-of-simulink-block-is-this), so I can't answer the question directly - could you select the MATLAB fcn block in the model and run this command in the command window: get_param(gcb, 'BlockType'). What does it return?
If it returns MATLABFcn, then you should switch to the (Embedded) MATLAB Function block for code-generation support. However, the fact that you are able to run the model in Rapid Accelerator mode makes me think that you are indeed using the (Embedded) MATLAB Function block already. This block already generates C code, so I don't think you can do better than this unless you find a corresponding implementation in C/C++ that is optimized. If you do have one available, then you can indeed using Legacy Code Tool to create a Simulink custom block (S-function+TLC file) to call into the optimized library instead.

2 Comments

Oops, sorry, this is my first time using this... So, I post it here again to comment your answer. Sorry about that!!!
Hello Kaustubha, Thank you! I am using the (Embedded) Matlab Function and the %#codegen is also on the second line. My model is a complicated model in nature but this single tiny Matlab Function block still seems taking too much of simulation time (9.1% of simulation time as listed in the profiler report and is called every time step). Do you think there is a better way to solve the second order polynomial roots other than the roots command? Because all I need is just the positive root. I have tried using the Algebraic Constraint to solve it; unfortunately, Rapid Accelerator mode is not supported for this block...so it only faster in Normal mode; but that is still way too slow...
You mentioned S-function + TLC file in your answer, this reminds me the second solution listed at this page: How can I speed up simulation of my Simulink model? (<http://www.mathworks.com/support/solutions/en/data/1-18AAG/?solution=1-18AAG)>. Are you suggesting me to convert the M-file to MEX-file? Will this be faster in Rapid Accelerator Mode? In the profiler report, the name of this block time consumption is called PolynomialSolve/Matlab Function/ SFunction (Output), why it is called SFunction (output) here? Is the Matlab file automatically converted to S-function in Rapid Accelerator mode?
Yes, the (Embedded) MATLAB Function block always generate C code from the MATLAB code as an S-function and calls it, so it is exactly like generating a MEX-file from a MATLAB-file.
My suggestion was to actually have your *own* optimized implementation of the roots function - perhaps even a commercial/free third-party library function written in C/C++ that you can call from a custom S-function block. You will need to find an optimized library that does better than a MEX-function generated from MATLAB. The other thing you might want to try is whether the MATLAB implementation is actually faster than the MEX-function that could be auto-generated from it. Sometimes MATLAB implementations use BLAS implementations with multi-threading and may be faster. In this case, using the Intepreted MATLAB block might be a better option, so that Simulink does not attempt to generate a MEX-file from the code.

Sign in to comment.

Hello Kaustubha, Thank you! I am using the (Embedded) Matlab Function and the %#codegen is also on the second line. My model is a complicated model in nature but this single tiny Matlab Function block still seems taking too much of simulation time (9.1% of simulation time as listed in the profiler report and is called every time step). Do you think there is a better way to solve the second order polynomial roots other than the roots command? Because all I need is just the positive root. I have tried using the Algebraic Constraint to solve it; unfortunately, Rapid Accelerator mode is not supported for this block...so it only faster in Normal mode; but that is still way too slow...
You mentioned S-function + TLC file in your answer, this reminds me the second solution listed at this page: How can I speed up simulation of my Simulink model? (<http://www.mathworks.com/support/solutions/en/data/1-18AAG/?solution=1-18AAG)>. Are you suggesting me to convert the M-file to MEX-file? Will this be faster in Rapid Accelerator Mode? In the profiler report, the name of this block time consumption is called PolynomialSolve/Matlab Function/ SFunction (Output), why it is called SFunction (output) here? Is the Matlab file automatically converted to S-function in Rapid Accelerator mode?
Thanks, Penn

Categories

Products

Asked:

on 14 Jun 2012

Community Treasure Hunt

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

Start Hunting!