fzero solver within a for loop stops working at a certain step
Show older comments
Hello!
This is my first time posting a question. I am usually good at scouring the web and finding helpful threads for my MATLAB issues, but this time I can't find something that resemble what I am facing.
To give you some context first, I have a blowdown system simulation, and at the moment I have an explicit procedure to compute the state of the gas flowing through my system in a certain time interval. I use a for loop, where at each step I update the state of the gas in the tank and then go through a series of computations to obtain the state (pressure, temperature, density, Mach number) of the gas in strategic points along the piping system (nodes).
My problem shows up when I try to calculate the Mach number at a certain node. The expression I am using is the following:
M7(k) = fzero(@(x2) m_dot(k)-A_7*x2*p7_stag(k)*sqrt(gamma_he/(R_he*T_stag(k)))*(1+d*x2^2)^c, 0.1);
I basically try to solve the mass flow rate expression (m_dot(k), which changes at each step) for the Mach number M7.
A_7, gamma_he, R_he, d and c are known constants, while m_dot(k), p7_stag(k), T_stag(k) are values that change at every step k of the for loop but are all computed before the incriminated line.
The weird thing is that the solver works perfectly up until a certain value of k, where it suddenly gives me the following error:
Error using fzero (line 308). Initial function value must be finite and real.
I'll add a plot of M7 just to show you what happens:

The peak you see at x=65 is correct since I have an increase in the mass flow rate. Afterwards, though, it should continue to decrease gradually towards zero, instead it drops down at x=195.
Any idea what may be the cause of it and how I can solve it?
Thanks in advance!
4 Comments
Davide Masiello
on 7 Apr 2022
@Valentina Lo Gatto It would be useful if you could share the entire code.
Valentina Lo Gatto
on 7 Apr 2022
Edited: Valentina Lo Gatto
on 7 Apr 2022
Riccardo Scorretti
on 7 Apr 2022
When I try to run your script, I get an error at line 130:
T_tank(k) = T_tank_0;
Indeed the variable T_tank_0 is undefined at this point. Please fixe the code, so that it can be executed.
Valentina Lo Gatto
on 7 Apr 2022
Edited: Valentina Lo Gatto
on 7 Apr 2022
Answers (1)
I am not sure why you are using fzero. It looks like the root can be solved explicitly for x2.
In any case, you can more easily see what is happening if you evaluate the function
fun = @(x2) m_dot(k)-A_7*x2*p7_stag(k)*sqrt(gamma_he/(R_he*T_stag(k)))*(1+d*x2^2)^c
at the offending k and initial x2 and see what value it returns. If d is negative and c is non-integer, it seems likely that (1+d*x2^2)^c will be complex valued for sufficiently large x2. The sqrt could also be returning complex values, depending on the specific values of the arguments.
1 Comment
Valentina Lo Gatto
on 7 Apr 2022
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!