Clear Filters
Clear Filters

"eig" in Simulink Matlab Function returns complex value.

4 views (last 30 days)
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??

Answers (1)

Animesh
Animesh on 30 Jun 2023
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.

Community Treasure Hunt

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

Start Hunting!