Error in generating C/C++ codes: Cannot assign a complex value into a non-complex location
Mostrar comentarios más antiguos
Codegen is giving the same error for this line:
state.icaweights = V / sqrt(D) * V' * state.icaweights;
When I tried the codegen again by writing:
state.icaweights = complex(V / sqrt(D) * V' * state.icaweights);
OR
state.icaweights = double(V / sqrt(D) * V' * state.icaweights);
it still provided the same error. Can anyone please help in rectification of this error?
Respuestas (1)
Walter Roberson
el 13 de Sept. de 2021
1 voto
state.icaweights has been created as non-complex, but either V is complex or D is suspected of being negative so code generation figures that the right hand side is generating a complex value.
If you are expecting complex values, then you need to initialize state.icaweights to complex.
For example if you initialized it to 0 before, then initialize it to complex(0,0)
6 comentarios
PIYUSH SWAMI
el 13 de Sept. de 2021
Editada: PIYUSH SWAMI
el 14 de Sept. de 2021
Walter Roberson
el 14 de Sept. de 2021
Are you taking roots of a polynomial of degree more than 2 ? And you are expecting real-valued outputs ?
roots here could possibly include eigenvalues for this purpose.
PIYUSH SWAMI
el 16 de Sept. de 2021
Editada: Walter Roberson
el 16 de Sept. de 2021
Walter Roberson
el 16 de Sept. de 2021
If your system is larger than 2 x 2, then the eigenvalues involve numeric solutions of polynomials of degree 3 or higher. There are exact solutions for degree 3 and 4, but both of those involve intermediate complex values that where algebraically the complex portions happen to cancel out in cases that the eigenvalues should be real-valued.
However, when you generate code, you are not using the exact same eigenvalue code as is used by regular MATLAB: regular MATLAB calls upon high-performance MKL (Math Kernel Library) or LAPACK external libraries, And because the two cases use different code, you can get slightly different rounding, leading to the complex parts not exactly balancing. Those last few bits of difference can then get multiplied by large coefficients, so the overall differences can end up large.
Even if the exact same code were being used, MKL and LAPACK are compiled by vendors for high performance, and if you do not use exactly the same compiler flags, you could get differences in outcomes.
So... try taking real(V) and real(D) (but print max(abs(imag(V))) and max(abs(imag(D))) to see how much you are throwing away.) .
PIYUSH SWAMI
el 17 de Sept. de 2021
Editada: Walter Roberson
el 18 de Sept. de 2021
Walter Roberson
el 18 de Sept. de 2021
Compute the whole thing as complex, assigned into a local variable. Extract real() part of that to assign into icaweights .
Simulink appears to be predicting that Dreal is potentially negative. There might be Good Reasons in your case why it cannot be negative for you, but Simulink either does not have sufficient evidence of that or else Simulink just was not able to follow the flow well enough to prove it.
Categorías
Más información sobre Algorithm Design Basics en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!