how do I calculate "to raise to the power of" in a matlab fuction in simulink when the exponent is not an integer?
2 次查看(过去 30 天)
显示 更早的评论
Hi everybody.
I am new using simulink and developing a simulation I have an error into a Matlab function block that I was not able to correct. x1 is an iterative variable into a convergent cycle.
When I try to calculate the following expression:
y1 = (hvap-x1)/x1^1.102 - Aconst;
I have the following error.
Error:An error occurred during simulation and the simulation was terminated
Caused by:
Domain error. To compute complex results, make at least one input complex, e.g. 'power(complex(a),b)'.
Error in power.m (line 82)
coder.internal.error('Coder:toolbox:power_domainError');
Error in power.m (line 71)
Error in mpower.m (line 44)
I have changed the expression: x1^1.102 for x1.^1.102 and power(x1,1.102). In addition I have change power(x1,1.102) for nthroot(x1,1.102). With this last sustitution the block runs, but the result is not the same when the code is running directly in a .m file.
After that I did this:
y1 = (hvap-x1)/power(x1+0i,1.102) - Aconst;
I got this error message:
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component:MATLAB Function | Category:Coder error
Cannot assign a complex value into a non-complex location. Function 'MATLAB Function ...' (#24.2054.2056), line 74, column 3: "a1" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of
(name of the simulation file)
Component:MATLAB Function | Category:Coder error
Simulink is unable to determine sizes and/or types of the outputs for block (name of the simulation file) due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink is unable to determine sizes and/or types of the outputs for block (name of the simulation file) due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:Simulink | Category:Model error
Error in port widths or dimensions. 'Output Port 1' of (name of the simulation file)/hliq is a one dimensional vector with 1 elements.
Component:Simulink | Category:Model error
Could you help me with some sort of advice to proper calculate the variable.
Thanks in advance.
11 个评论
Walter Roberson
2025-8-12
One implication of that is that it is possible for x1 to become 0 exactly -- which would give a division by 0.
Your code should probably be protecting against negative or 0 values of x1
采纳的回答
Sam Chak
2025-8-9
Hi @Carlos
Thanks for the update. Which specific method did you use? Was it
? Regardless of the method employed, you should be aware that even if the simulation runs smoothly, it does not necessarily mean that the result is valid according to the laws of physics. Therefore, you should carefully verify whether the result makes sense.

For example, in a common mechanical engineering problem where the friction or drag force model exhibits nonlinear behavior and is proportional to velocity, students often use the power law when the object is moving through non-Newtonian fluids.

What would happen if the velocity is a negative value? Mathematically, this results in a complex output. However, the negative sign of the velocity does not imply a "negative" amount. Rather, it indicates the opposite direction of velocity when the reference frame is defined in the model.
v = linspace(-2, 2, 4001);
k = 1;
lambda = 1.2;
Fd = - k*(v.^lambda);
figure
plot(v, Fd), hold on
plot(v, -v, '--'), hold off
grid on, ylim([-2.5, 2.5])
legend('Nonlinear Friction', 'Linear Friction')
title('Case 1: Power law')
xlabel('v')
ylabel('F_{d}')
To avoid complex outputs, students typically use the absolute value operator. However, do the magnitude and direction of the friction force make sense?

Fd = - k*(abs(v).^lambda);
figure
plot(v, Fd), hold on
plot(v, -v, '--'), hold off
grid on, ylim([-2.5, 2.5])
legend('Nonlinear Friction', 'Linear Friction')
title('Case 2: Power law with absolute value')
xlabel('v')
ylabel('F_{d}')
To ensure the inverse symmetric property in the nonlinear friction model, the signum function can be used.
Fd = - k*(abs(v).^lambda).*sign(v);
figure
plot(v, Fd), hold on
plot(v, -v, '--'), hold off
grid on, ylim([-2.5, 2.5])
legend('Nonlinear Friction', 'Linear Friction')
title('Case 3: Absolute power law with sign function')
xlabel('v')
ylabel('F_{d}')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!