Please note that total numbers of iterations depending on how fast the value Tsintial increasing, maybe it takes a very long time if Tsintial increases in very slow.
Second case when Tsintial greater than Tsouttemp, while logic will fail.
I dont have the data, if it possible then try it for some small range of values Tsintial & Tsouttemp.
Be attention during while loop, if the logic true always, it run continue
I have tested it, it seems to continue, are you sure the following expression increasing the value of Tsintial?
Or Increasing in very small steps.
If somehow Tsintial decreasing, the loop runs infinitely.
When Tsintial>Tsouttemp, then Tsintial-Tsouttemp<0, logic fails. loop stop, when Tsintial=Tsouttemp=1373
disp(Tsouttemp); disp(Mg); disp(Tgout);