"eig" in Simulink Matlab Function returns complex value.

조회 수: 3 (최근 30일)
주형
주형 2023년 6월 30일
답변: Animesh 2023년 6월 30일
Hi. I am using simulink - matlab function. When I use it, an odd phenomenon happens.
here's my matlab function code
function y = fcn(A)
[V, ~] = eig(A);
y = V(:,4);
when I input matrix "A" as
A = [ -551.2750 -68.3967 -39.4810 1.1936;
-68.3967 197.8717 440.4691 0.9514;
-39.4810 440.4691 -204.0590 -2.1449;
1.1936 0.9514 -2.1449 557.4624]
y returns complex value in the simulink - matlab function.
-> y = [ 0.0015 + 0.0000i
-0.0041 + 0.0000i
-0.0053 + 0.0000i
1.0000 + 0.0000i]
But y returns real value in the .m file. (I used same "A" matrix)
-> y = [ 0.0015
-0.0041
-0.0053
1.0000]
why??

답변 (1개)

Animesh
Animesh 2023년 6월 30일
The difference you're observing in the output of the MATLAB function when used in Simulink versus when used in a standalone .m file is due to the way Simulink handles complex numbers.
Simulink treats signals as complex by default, even if the values are purely real. When a Simulink-MATLAB Function block is used, the output signal is treated as a complex signal unless explicitly specified otherwise. On the other hand, when you run the same function in a standalone .m file, MATLAB treats the output as a real signal by default.
To ensure consistent behavior between Simulink and standalone MATLAB, you can explicitly convert the output to real using the real() function.
You can do something like this:
function y = fcn(A)
[V, ~] = eig(A);
y = real(V(:,4));
By applying the real() function, you extract the real part of the complex value and return a real vector as the output.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by