Simulink S-Function internal loop
2 views (last 30 days)
Show older comments
Hi, I need help, I hope some of you will be able to do it. I have just started using Matlab and Simulink for a thesis project. This is my problem: I am developing a control algorithm for an electrical machine. This algorithm requires to calculate an inverse matrix. To calculate the inverse I generated a file.c and a file.h in a "S-Function". There are two subroutines which call each other to calculate the inverse matrix. I run the subroutines by using devc++ compiler and it works properly. The matrix calculated by using devc++ is the expected one by doing several tests. But using it by simulink in a s-function, matlab crashes without any error message but I have to force to close matlab and restart it again. I am sure that the problem is caused by this loop between the subroutines because if I comment getting the sobroutine without loop matlab works properly.
I do hope I explained properly the problem and somebody will be able to help me. If you want I can send you the details about the codes. For now I have just wanted to understand if is something solvable by some tricks or using some simulink block to avoid it, or I must change algorithm to get the inverse matrix?
Thank you very much in advance
0 Comments
Answers (1)
Mark Schwab
on 26 Jan 2018
Edited: Mark Schwab
on 26 Jan 2018
There are several functions for taking the inverse of matrix in MATLAB. You can use a MATLAB function block in your model to take the inverse of an input matrix. I attached a picture of a simple model I made to demonstrate this workflow. The MATLAB function block contains the following code:
function y = fcn(u)
y = inv(u);
I have included links to documentation for the MATLAB function block and a couple of useful inverse functions below:
https://www.mathworks.com/help/simulink/ug/creating-an-example-model-that-uses-a-matlab-function-block.html https://www.mathworks.com/help/matlab/ref/inv.html https://www.mathworks.com/help/matlab/ref/pinv.html
In regards to creating a C S-function in Simulink, the process for manually creating an S-function can be rather particular. I have included documentation below for tools we have to make this process easier if you are still interested in using C S-functions:
0 Comments
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!