Error when using if-else statement in MATLAB function block. How can I fix this error described below?
조회 수: 5 (최근 30일)
이전 댓글 표시
function m = fcn(Q, T_sat, T_w, sigma, rho_v, h_fg, M, P_v, R, g, beta, x, nu_novec, alpha_novec, k_m, d_pore, psi, k_f, rporous_outer, height_porous)
q_w = Q/(2*pi*rporous_outer*height_porous); %heat input converted to heat flux
if T_w>=T_sat
m = (((2*sigma)/(2-sigma))*((rho_v*(h_fg)^2)/T_sat)*((M/(2*pi*R*T_sat))^(0.5))*(1- ((P_v)/(2*rho_v*h_fg))));
else
m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
%the power of 0.4 is what triggers the complex number
end
When I run this code, I get the following error, which is triggered by the line ' m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
I've checked if any of the values result in a negative number, but none of the terms are negative, nor their result. I've run this separately, and this the same line works fine and I get a positive results without an complex number or any error? Why would this occur, does this error have to do anything with the if-else statement used? Any thoughts would be much appreciated.
I think this issue is triggered by the use of ^0.4, which cannot be changed. Why would an error/complex result occur in simulink and not in MATLAB?
Many thanks
댓글 수: 1
Sam Chak
2024년 5월 6일
Could you please provide us with the values of the parameters? This will allow us to test the function 'fcn()' and compute 'm'.
m = fcn(1, 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
function m = fcn(Q, T_sat, T_w, sigma, rho_v, h_fg, M, P_v, R, g, beta, x, nu_novec, alpha_novec, k_m, d_pore, psi, k_f, rporous_outer, height_porous)
q_w = Q/(2*pi*rporous_outer*height_porous); %heat input converted to heat flux
if T_w>=T_sat
m = (((2*sigma)/(2-sigma))*((rho_v*(h_fg)^2)/T_sat)*((M/(2*pi*R*T_sat))^(0.5))*(1- ((P_v)/(2*rho_v*h_fg))));
else
m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
end
end
답변 (4개)
Sulaymon Eshkabilov
2024년 5월 5일
Have you tried to change ^0.4 with: ((...)^2)^(1/5) or ((...)^2)^(0.2)
댓글 수: 0
Walter Roberson
2024년 5월 6일
m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
1 2 345 4 5 43 456 5 6 7 654 32 3 210 1 0
Simulink is calculating the expression
(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))
as being negative. When negative is raised to the fractional power 0.4 it results in a complex value.
My guess is that psi is outside the range -1 to +1
If you want a complex result, then code
m = (k_f*complex(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
If you do not want a complex result, you will need to protect against it, such as
if abs(psi) >= 1
m = nan;
elseif T_w>=T_sat
m = (((2*sigma)/(2-sigma))*((rho_v*(h_fg)^2)/T_sat)*((M/(2*pi*R*T_sat))^(0.5))*(1- ((P_v)/(2*rho_v*h_fg))));
else
m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
end
댓글 수: 9
Sam Chak
2024년 5월 9일
m = (k_f*(0.5*(((g*beta*q_w*x^4)/(nu_novec*alpha_novec*k_m))*(((d_pore^2)*psi^3/(180*(1-psi^2)))/x^2))^(0.4)))/(x);
I tend to divide long equations and conquer them later. This approach helps 100% prevent human mistakes such as mismatched parentheses. I suggest you rewrite the equation 'm' in multiple terms and then return here for a review.
%% parameters
a = ...;
b = ...;
c = ...;
d = ...;
%% terms
term1 = ...;
term2 = ...;
term3 = ...;
%% equation
m = term1 + (term2/term3)^0.4 ...;
참고 항목
카테고리
Help Center 및 File Exchange에서 Thermal Liquid Library에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!