generated C/C++ code from `pinv`-command gives different result than the command `pinv` itself

I am using Matlab Coder for my Arduino. Inside my Matlab function I use the Matlab-command `pinv`. This Matlab-function is then converted into C/C++ code (let's call it `pinvC`) using the Code Generator from Matlab.
However, I noticed the following. The result of Matlab-command `pinv` inside Matlab function is different to the result of `pinvC`. What is causing this? How can I solve it?

답변 (1개)

James Tursa
James Tursa 2018년 11월 14일
How much different? The MATLAB code is calling into BLAS and LAPACK library routines to accomplish this. But the Coder is not using that same library code to generate its results, hence the difference.

댓글 수: 2

The pseudo-inverses are a little different. But after some multiplications the small errors accumulate and the difference is about a factor of 5.
So I tried to compute the pinv beforehand and store it in .mat file and load it inside the function using `coder.load` but then some variables are blowing up to order of 1e3, while they should be below 10.
Could the C/C++ `pinv` version be different, for numerically stability reasons?
James is correct. Different stability properties induced by PINV per se does not seem likely, but different PINV implementations will issue different numerical perturbations, which might be magnified by how you use PINV.
You might try an experiment perturbing the output of PINV in MATLAB by some units in the last place to explore what your algorithm does with it in MATLAB, i.e. with no code generation involved. Something like:
X = pinv(A);
X = X + randi([-10,10],size(X)).*eps(X)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2018년 11월 14일

댓글:

2018년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by