Inf or NaN whilst trying to run a function

3 views (last 30 days)
SL
SL on 23 May 2023
Commented: SL on 23 May 2023
Hi, I'm very new to MATLAB and I am having some trouble. I'm trying to integrate an equation to solve for specific heat capacity. Could somebody please explain what this error is and how to fix it? Thanks.
T_datum_K = 25+273.15;
T_in_K = 199.3 + 273.15;
T_out_K = 359 + 273.15;
%Ar
C1_Ar = 20786;
C2_Ar = 0;
C3_Ar = 0;
C4_Ar = 0;
C5_Ar = 0;
func = @(T) C1_Ar + C2_Ar.*((C3_Ar./T)./(sinh(C3_Ar./T))).^2 + C4_Ar.*((C5_Ar./T)./(cosh(C5_Ar./T))).^2;
Cp_Ar_in = integral(func, T_datum_K, T_in_K);
Warning: Inf or NaN value encountered.
Cp_Ar_out = integral(func, T_datum_K, T_out_K);
Warning: Inf or NaN value encountered.

Answers (1)

VBBV
VBBV on 23 May 2023
Edited: VBBV on 23 May 2023
T_datum_K = 25+273.15;
T_in_K = 199.3 + 273.15;
T_out_K = 359 + 273.15;
%Ar
C1_Ar = 20786;
C2_Ar = 1;
C3_Ar = 0.1;
C4_Ar = 0.1;
C5_Ar = 0.1;
func = @(T) C1_Ar + C2_Ar.*((C3_Ar./T)./(sinh(C3_Ar./T))).^2 + C4_Ar.*((C5_Ar./T)./(cosh(C5_Ar./T))).^2;
Cp_Ar_in = integral(func, T_datum_K, T_in_K)
Cp_Ar_in = 3.6232e+06
Cp_Ar_out = integral(func, T_datum_K, T_out_K)
Cp_Ar_out = 6.9429e+06
  3 Comments
Walter Roberson
Walter Roberson on 23 May 2023
T_datum_K = 25+273.15;
T_in_K = 199.3 + 273.15;
T_out_K = 359 + 273.15;
%Ar
C1_Ar = 20786;
syms C2_Ar C3_Ar C4_Ar C5_Ar T real
func = @(T) C1_Ar + C2_Ar.*((C3_Ar./T)./(sinh(C3_Ar./T))).^2 + C4_Ar.*((C5_Ar./T)./(cosh(C5_Ar./T))).^2;
Cp_Ar_in = int(func, T, T_datum_K, T_in_K)
Cp_Ar_in = 
Notice the exp(40*C3_Ar/5963) - 1 in the denominator. As C3_Ar approaches 0, the exp() approaches 1, and subtracting 1 from that approaches 0, leading to a division by 0 if you are operating numerically. But there is also a C3_Ar^2 in the numerator and that can "cancel" the division, if you operate symbolically on the limit
limit(Cp_Ar_in, C3_Ar, 0)
ans = 
As C5_Ar approaches 0, the C5_Ar multiple of the terms approaches 0 so you get divisions by 0 there, with two such divisions with slightly different fractions being subtracted. But the whole thing is being multiplied by C5_Ar^2 so again you get some canceling in the limit:
limit(ans, C5_Ar, 0)
ans = 
Therefore, if you are careful to operate in the limit case, C3_Ar == 0 and C5_Ar == 0 can be accounted for. But not if you operate neively with integral()
SL
SL on 23 May 2023
Got it, thank you so much for your help!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!