Why do I receive different results when I use MLDIVIDE at MATLAB command prompt and in an embedded MATLAB Fcn block to solve the equation Ax = C, where A is a singular matrix?

1 view (last 30 days)
When I use the MLDIVIDE function to find the solution x to the equation, Ax = C in MATLAB, where matrix A is singular, there is a warning that the matrices are singular but the division is still carried out for the rows that are not singular and results "x" are available. Consider the following example:
A = [1 2 0;
3 4 0;
0 0 0];
C = [5; 6; 0]
A\C
Executing the above code issues the below warning, but the operation is still carried out
ERROR: Warning: Matrix is singular to working precision.
ans =
-4.00000000000000
4.50000000000000
0
However, this same code when executed in the Embedded MATLAB Fcn block results in NaNs for all entries of "x" even though MLDIVIDE is listed as a function that can be used in the embedded MATLAB function block.
You cannot use inv(A) because that will return Inf.
I have tried to use “pinv(A)” however it is significantly slower (several times) than MLDIVIDE even with the simulink accelerator and/or generated S-function and therefore is not a viable option.
I would like MLDIVIDE to perform in the Embedded MATLAB block as it does in the MATLAB command environment (i.e. still solve for x even if A and C are singular). Is it possible to replicate the algorithm used by MATLAB when using MLDIVIDE for singular matrices in Embedded MATLAB Fcn block?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 May 2023
Edited: MathWorks Support Team on 23 May 2023
Embedded MATLAB Fcn and MATLAB are based on two different algorithms. When the coefficient matrix is singular to working precision, the problem either has no solution or an infinite number of solutions. In cases such as this, where the solution is not unique, it is possible that Embedded MATLAB and MATLAB could give two different results. See below for information on how to implement the MATLAB's MLDIVIDE algorithm in Embedded MATLAB.
The algorithm implemented in MLDIVIDE can be found at the following address:
or use the following command in MATLAB 8.0 (R2012b)
>> web([docroot '/matlab/ref/mldivide.html'])
You can implement the appropriate algorithm for singular matrices in your Embedded MATLAB Fcn block.
Another way of implementing this in Simulink is to use the S-function block. You can call the LAPACK and BLAS commands (which are available online for free download) from within your S-function using the S-function wrapper. Documentation on this can be found at:
or
Use this command in MATLAB 8.0 (R2012b)
>> web([docroot, '/rtw/ug/s-functions-for-code-generation.html#f53144'])
Since the functions in LAPACK/BLAS are written in Fortran, you might also want to refer to the following page which provides an example C MEX S-Function calling Fortran code.
or
Use the following command in MATLAB 8.0 (R2012b)
>> web([docroot, '/simulink/sfg/creating-level-2-fortran-s-functions.html#f5-88766'])

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R14SP1

Community Treasure Hunt

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

Start Hunting!