Inconsistency between Matlab eig() function and Matlab generated C code eig() function
Show older comments
Hello,
I'm looking to generate a C code from a Matlab function using Matlab in-built eig() function.
I have the following matrix :
M = [-0.0062 -0.1497 0.1435; ...
0.8728 -0.6724 0.2994; ...
1.7736 -0.4364 -0.0062];
The associated eigenvectors using Matlab in-built eig() function are :
V = [0.2184 0.3540 -0.0643; ...
0.5088 0.4597 -0.7629; ...
0.8327 -0.8145 -0.6433];
When using generated C code (currently using Simulink Accelerator) I get the following results :
Vc = [0.2019 - 0.0831i -0.2295 - 0.2696i -0.0572 + 0.0294i; ...
0.4705 - 0.1937i -0.2979 - 0.3500i -0.6784 + 0.3489i; ...
0.7701 - 0.3170i 0.5280 + 0.6202i -0.5721 + 0.2943i];
Granted, the complex magnitude of the previous expression returns the same values as the absolute values of V, but I want to get the eigenvector associated to the minimal non-negative value of the expression 4*Vc(1,:).*Vc(3,:)-Vc(2,:).^2 and in that case, with Matlab, I end up with :
sol = [0.4685 -1.3646 -0.4164];
And with the generated C code, with :
solc = [0.3327 - 0.3298i 0.2179 - 1.3471i -0.2422 + 0.3387i];
As you can see neither selecting the real or the imaginary part of the previous expression would allow for some consistency between Matlab code and generated C code. Taking the complexe magnitude of the previous expression would mean losing information about negative values.
Is there any workaround that I could use for both solutions to give consistent and comparable results ? Or do I need to scrap the idea of generating code while using the eig() function ?
Sorry in advance if there's some obvious mathematical solution to my issue, I'm not really comfortable working with eigenvalues/eigenvectors.
Accepted Answer
More Answers (2)
From the documentation page for the eig function, specifically the C/C++ Code Generation item in the Extended Capabilities section, two items:
- V might represent a different basis of eigenvectors. This representation means that the eigenvector calculated by the generated code might be different in C and C++ code than in MATLAB. The eigenvalues in D might not be in the same order as in MATLAB. You can verify the V and D values by using the eigenvalue problem equation A*V = V*D.
- Outputs are complex.
There is no guarantee that the output from the C++ code will be identical to the output from MATLAB. Remember that if V is an eigenvector of A with eigenvalue d, then so is k*V for a non-zero scalar k.
M = [-0.0062 -0.1497 0.1435; ...
0.8728 -0.6724 0.2994; ...
1.7736 -0.4364 -0.0062];
[V, D] = eig(M)
Vc = [0.2019 - 0.0831i -0.2295 - 0.2696i -0.0572 + 0.0294i; ...
0.4705 - 0.1937i -0.2979 - 0.3500i -0.6784 + 0.3489i; ...
0.7701 - 0.3170i 0.5280 + 0.6202i -0.5721 + 0.2943i];
checkV = M*V-V*D
checkVc = M*Vc-Vc*D
It may seem like checkVc is not that close to 0, but if you'd given us more than 4 decimal places of the output from the C++ code I'd bet it would contain values that are similarly close to 0 as the ones in checkV. If we use the 4 decimal place approximation from your post instead of the full double precision V from the computation:
V2 = [0.2184 0.3540 -0.0643; ...
0.5088 0.4597 -0.7629; ...
0.8327 -0.8145 -0.6433];
howCloseAreVandV2 = V-V2
checkV2 = M*V2-V2*D
Remember how above I said that eigenvectors aren't unique? Let's divide each element in Vc by its corresponding element in V and see what values we get for k.
k = Vc./V
So if we scale the first eigenvector does it still satisfy the eigenvalue/eigenvector relationship?
KV = k(1, 1)*V(:, 1)
checkKV = M*KV-KV*D(1, 1)
Looks like it.
William Rose
on 19 Jan 2024
0 votes
@Hugoz, I am not sure how you generated the C code, but I suspect the C code is wrong. Matlab's eig() is extremely well tested and verified by real world use. Maybe you can go to a C site for assistance debugging the C code.
2 Comments
Steven Lord
on 19 Jan 2024
Can it give different answers? Yes.
Can it give incorrect answers? If it does that would be a bug, but I don't see any evidence that its answers are incorrect.
Hugoz
on 19 Jan 2024
Categories
Find more on Matrix Indexing 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!