MATLAB: “Runtime Error: Call to MATLAB function aborted: Domain error. To compute complex results, make at least one input complex, e.g. ‘power(complex(a),b)’.”

algebraic loopcoder.extrinsiccomplex resultsMATLAB and Simulink Student Suitematlab codermatlab functionruntime errorsimulink

Hello, I'm trying to solve the following system in Simulink. I have some coder.extrinsic('intpolyval') into the Embeeded Matlab Function to deal with function handles I need to use while exchanging variables between functions, for instance:
function sol=intpolyval(coeff,minx,maxx)
sol=integral(@(x) polyval(coeff,x),minx,maxx);
end
While i try to run my model I get the error in debugger:
"Runtime Error: Call to MATLAB function aborted: Domain error. To compute complex results, make at least one input complex, e.g. 'power(complex(a),b)'."
This only happens when there is a loop in the Simulink model, while the model is simple- everything goes all right. Did anyone have similiar problems? How have u worked arround it?

Best Answer

When there is a expression that might produce a complex result, the input signals for the calculation must be changed to Complex, or you must use complex() on the real signals that might produce a complex result. For example if you have sqrt() then if the input might go negative then you need to complex() the input before taking the sqrt.
I gather the error shows up at the time the need for a complex result is detected, so if you are not expecting the calculation to produce a complex result you may need to add debugging to see why it does produce such a result.
MATLAB silently transforms values to complex at need, but Simulink does not.
When you have a loop, Simulink has to solve the loop to establish the initial conditions for self-consistency because everything in one step has to be true simultaneously (if that is not needed for you then send your signal through a time delay so the rest of the calculation can be performed in the next step) . The result can include values outside the range you expect.
Related Question