Matlab coder - wrong answer
1 view (last 30 days)
Show older comments
Tamir Mizrahi
on 8 Jun 2020
Commented: Tamir Mizrahi
on 9 Jun 2020
Hi guys,
I'm tring to export matlab code to C code, in my matlab code i have Eigen function in order to calculate the Eigen vector right matrix.
Although when i compile my code in C compiler (atollic true studio) i received a wrong answers.
Can anyone please help me to solve this problem ?
input matrix and function :[cN,~]=eig([6506.1709323862497 + 0 * i, 352.72061684167767 + -3710.3651327887619 * i, 2011.5655185722712 + 384.26985324363284 * i, 1725.5363173165599 + -5930.123440499453 * i; 352.72061684167767 + 3710.3651327887619 * i, 6199.5160239281295 + 0 * i, 356.19284194608099 + -3663.5190286699631 * i, 1768.5916083618101 + 331.40000184453999 * i; 2011.5655185722712 + -384.26985324363284 * i, 356.19284194608099 + 3663.5190286699631 * i, 6447.9241828420545 + 0 * i, 382.01986570001651 + -4005.4837002418008 * i; 1725.5363173165599 + 5930.123440499453 * i, 1768.5916083618101 + -331.40000184453999 * i, 382.01986570001651 + 4005.4837002418008 * i, 6611.8755981240902 + 0 * i])
The first picture is the values that i received after the eigen function, and the second picture the the correct values that i expect to receive
2 Comments
Wilson A N
on 9 Jun 2020
Hi Tamir,
Can you share the exact values you had used with the eigen function. Additionally, providing the commands used for code generation would help.
- Wilson
Accepted Answer
Areej Varamban Kallan
on 9 Jun 2020
Edited: Areej Varamban Kallan
on 9 Jun 2020
Hi Tamir,
Thanks for sharing the values. I am able to reproduce the results.
The computed results in the generated code are correct. For a given matrix, the eigenvector corresponding to a particular eigevalue is not unique. If v is an eigenvector, then alpha*v, which is a scaled version of v is also an eigenvector for the same eigenvalue.
For the inputs considered here, each eigenvector returned by the C code is a scaled version of the corresponding eigenvector returned by MATLAB.
To better understand the results, let us compute eigenvalues as well.
function [v,d] = myeig(a)
%#codegen
[v,d] = eig(a);
>> cfg = coder.config('lib');
>> cfg.VerificationMode = 'SIL';
>> codegen myeig -args {coder.typeof(1i,[4 4])} -config cfg
>> [vmatlab,dmatlab] = myeig(A)
vmatlab =
Columns 1 through 2
-0.376796540203342 - 0.355408671476361i -0.398541780958145 + 0.374702617926804i
0.001275398603249 + 0.561417931091213i -0.143284708674997 + 0.311017096268371i
0.480804533726921 - 0.184887065618415i 0.248485110194001 + 0.324562032274043i
-0.388794414636469 + 0.000000000000000i 0.645302171278108 + 0.000000000000000i
Columns 3 through 4
-0.045415333593944 + 0.168282082335769i 0.176847576029146 - 0.608945780485925i
-0.699329885477756 - 0.125756891058523i 0.246152272181504 + 0.045586320593164i
0.056552956858085 - 0.672545130245288i 0.032517800265024 - 0.333145334730278i
0.096053707488093 + 0.000000000000000i 0.650536467977805 + 0.000000000000000i
dmatlab =
1.0e+04 *
0.000172468967993 0 0 0
0 0.000229944404698 0 0
0 0 1.036674139012830 0
0 0 0 1.539472121342531
[vccode,dccode] = myeig_sil(A)
vccode =
Columns 1 through 2
-0.634105692072699 + 0.000000000000000i -0.174302644158693 - 0.000000000000000i
-0.024872565084105 - 0.249099195565696i -0.060800388869246 - 0.707940955930921i
-0.328995848224188 + 0.061684303010346i 0.664049974356741 - 0.120635072441042i
-0.181430002787685 - 0.624724619538480i 0.025027223135727 + 0.092735930600402i
Columns 3 through 4
0.547026144759982 + 0.000000000002153i -0.517968103718098 + 0.000000000008760i
0.317432475274703 - 0.128447520612772i 0.386149968271705 + 0.407529044359775i
0.041282386650599 - 0.406670784952457i 0.222899861257117 - 0.464404864894278i
-0.470141837021641 - 0.442019847222879i -0.282828979683157 + 0.266774933423510i
dccode =
1.0e+04 *
1.539472121342530 0 0 0
0 1.036674139012831 0 0
0 0 0.000229944404698 0
0 0 0 0.000172468967994
The eigenvalues returned by MATLAB and C code are the same but they appear in a different order.
Now we must check if the computed results are correct by verifying if they satisfy the eigenvalue equation, ie for
[V,D] = eig(A), A*V - V*D = 0 must be satisfied within some tolerance.
>> norm(A*vmat- vmat*dmat)
ans =
3.294189142814234e-12
>> norm(A*vccode- vccode*dccode)
ans =
1.220174523947488e-11
We see that the computed values are solutions to the given eigenvalue problem
More Answers (1)
Areej Varamban Kallan
on 9 Jun 2020
Hi Tamir,
Could you please share the inputs that were passed to eig. Which version of MATLAB are you using?
Please note that the generated code might return eigenvalues in a different sorted order compared to MATLAB. Moreover, the matrix of eigen vectors could be returned in a different basis.
See Also
Categories
Find more on Array and Matrix Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!