Why do I get different outputs when executing the same code and same inputs twice?

Well, I created a code and for some reason, when I run the exact same code and inputs on the same computer, it gives different values. For example, the outputs from the first part of the code are always the same but the values from one function tend to change. Usually, if I run another function (that has nothing do do with the code) before the one that is giving me problems, the results are correct, but when I run everything at once the results are incorrect. I don't understand why though.
function [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
%This is the main code, where I call the other funtions:
%start
Fj=Fl+Fg;
[Gj,Lj]=MetodoSR(N,Fg,Fl);
Kij=ctteK1(Pj,Tj,N);
Xij=MatrizxijT(N,Fg,Fl,Gj,Kij,Zij);
[Lj,Gj,Xij,Yij]=Flujos(N,Lj,Xij,Fg,Fl,Kij);
[MatrizTH]=TH(N,TjF,Xij,Yij,Lj,Gj);
[HFlj,HFgj]=THFeed(N,TF,Zij);
[Tj]=Balance(N,TjF,Yij,Xij,Lj,Gj,Fj,HFlj,HFgj,Qj,MatrizTH);
Now, the problem with the outputs are on this part of the code:
function [Tj]=Balance(N,Tj,Yij,Xij,Lj,Gj,Fj,HFlj,HFgj,Qj,MatrizTH)
HFj=HFlj+HFgj;
PropHenley
%Cálculo de entalpías i,j
%fase vapor
Hvij=zeros(size(Yij,1),N);
for i=1:size(Yij,1)
for j=1:N
if Yij(i,j)~=0
Hvij(i,j)=(PropHT(i,1)+PropHT(i,2)*(Tj(j))+PropHT(i,3)*(Tj(j))^2);
else
Hvij(i,j)=0;
end
end
end
%fase líquida
Hlij=zeros(size(Xij,1),N);
for i=1:size(Xij,1)
for j=1:N
if Xij(i,j)~=0
Hlij(i,j)=(PropHT(i,4)+PropHT(i,5)*(Tj(j))+PropHT(i,6)*(Tj(j))^2);
else
Hlij(i,j)=0;
end
end
end
%Entalpías i,j x Xij
HlijXij=Xij.*Hlij;
%Entalpías i,j x Yij
HvijYij=Yij.*Hvij;
%Sumatoria de los valores de H por etapa
Hlj=sum(HlijXij);
Hvj=sum(HvijYij);
%Balance de energía
R=zeros(1,N);
for j=1:N
if j==1
R(j)=Gj(j+1)*Hvj(j+1)+Fj(j)*HFj(j)-Lj(j)*Hlj(j)-Gj(j)*Hvj(j)-Qj(j);
elseif j==N
R(j)=Lj(j-1)*Hlj(j-1)+Fj(j)*HFj(j)-Lj(j)*Hlj(j)-Gj(j)*Hvj(j)-Qj(j);
else
R(j)=Lj(j-1)*Hlj(j-1)+Gj(j+1)*Hvj(j+1)+Fj(j)*HFj(j)-Lj(j)*Hlj(j)-Gj(j)*Hvj(j)-Qj(j);
end
end
%Resolución Matriz
R=R';
DTj=linsolve(MatrizTH,R)';
%Temperaturas
Tjn=Tj+DTj;
Tj=Tjn;
end
I'd appreciate it a lot if you could help me, it's for my thesis. Thank you!

15 Comments

I think its because the usage of function is inappropriate i.e., the previous function has to be called so that it can use the previously found values.
I just edited the question because I had made a mistake while writing it. I'm not sure I understand what you mean. I call all the functions created on the main file.
Your code is very difficult to read due to your variable names that look like random letters to an outsider.
It seems like you are calling additional custom functions/scripts within your function. For example, what is PropHenley ?
One possibility is that somewhere one of your function is not deterministic. Perhaps you're using a random variable (randperm, rand, randi, ...).
To test that, set the seed of your random number generator, run the code, then set the same seed and run the code again. If your outcomes match, then you can bet that a random process is involved.
Here's how to do that (I chose 359 but you can choose any number).
rng(359);
% Run your code, save outputs.
rng(359)
% Run your code again, save outputs, compare.
Yes, it's very hard to understand since it's a chemical engineering method.
PropHenley is a script (I believe that's what it's called) It takes the values from a matrix but I have no problem with it though.
I'm not using any random variable.
It's not that hard though, I think it somehow makes a mistake while multiplying two matrices.
You'll notice that there's a variable called Xij, which is later rewritten by another function, so we would now have different values for Xij. Do you think it's possible that it somehow uses the old values?
What's in the PropHenley script? How are you running your code "at once" versus the other option?
For instance is this how you run the code?
>> [Gj,Lj,Xij,Yij,Tj] = SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
>> a = 1;
>> [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
%This works?
>> [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
>> [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
%This fails?
Summoning a script in a function is a bad practice because it because it "poofs" variable into a function, making it hard to follow the code for debugging. For example, what are the scripts doing in this case?
function a = myFcn(b)
randomScript1
randomScript2
randomScript3
a = c+d-f; %Where did "c", "d", "f" come from!?
Could you zip up all relevant .m files and data that we would need to test, and attach the zip ?
Well, when I run the main file, I obtain wrong results. That's the SR function. When I run function by function (not the main file), it works.
This is PropHenley:
PropHT=[1604, 9.357, 0.001782, 0, 14.17, -0.001782, 4.35, 2.542E-2, -2.0E-5,8.333E-9;
4661, 15.54, 0.003341, 0, 16.54, 0.003341, 0.65, 8.183E-3, 2.25E-5, -2.333E-8;
5070, 26.45, 0, 0, 22.78, 0.004899, 0.15, 2.383E-3, 2.35E-5, -2.333E-8;
5231, 33.9, 0.005812, 0, 31.97, 0.005812, 0.0375, 5.725E-4, 1.075E-5, -2.5E-10;
5411, 42.09, 0.008017, 0, 39.68, 0.008017, 0.0105, 2.692E-4, 2.55E-6, 1.108E-8;
8000, 74.67, 0.03556, 0, 69.33, 0.03556, 1.42E-5, 3.64E-7, 3.44E-9, 1.50E-11];
There are several problems with the code that make it difficult to troubleshoot without running it.
  • First, the variable names as I mentioned before, are very difficult to follow.
  • Second, you're calling scripts and custom functions that we can't see and they could be manipulating the variables in ways we'd never no. They could involve random processes, they could transform the shape, size, or values of variable, they are a wildcards without seeing them.
  • Third, you're overwriting variables which is sometimes OK but it makes it hard to follow what's going on.
As far as I can imagine, it's impossible for a function to produce different results with the same exact inputs unless there is a random process involved. So it must be the case that either your inputs are not exactly the same or there is a random process involved.
Usually, if I run another function (that has nothing do do with the code) before the one that is giving me problems, the results are correct, but when I run everything at once the results are incorrect
This makes me think that your inputs are not identical.
Do you know how to work in debug mode? You should pause your code just before calling the problematic function and save the input values so you can compare them to the next time that function is called. This will ensure that they are always exactly the same - even when you have different outputs.
Another possibility is that you've got a global variable or persistent variable that is changing between function calls (which is obviously a bad idea).
Hm.. To clarify how you run then, it looks like this?
%Wrong run
>> [Gj,Lj,Xij,Yij,Tj]=SR(N,Tj,TjF,Fg,Fl,Zij,TF,Qj,Pj)
%Right Run
>>Fj=Fl+Fg;
>>[Gj,Lj]=MetodoSR(N,Fg,Fl);
>>Kij=ctteK1(Pj,Tj,N);
>>Xij=MatrizxijT(N,Fg,Fl,Gj,Kij,Zij);
>>[Lj,Gj,Xij,Yij]=Flujos(N,Lj,Xij,Fg,Fl,Kij);
>>[MatrizTH]=TH(N,TjF,Xij,Yij,Lj,Gj);
>>[HFlj,HFgj]=THFeed(N,TF,Zij);
>>[Tj]=Balance(N,TjF,Yij,Xij,Lj,Gj,Fj,HFlj,HFgj,Qj,MatrizTH);
And you're saying Tj is the wrong value, but everything else is correct?
Yes, I saved the workspace so you can have the values. Also, you can see the inputs of the balance function on the image that I attached. The values of Tj or Tjn must be between 80 and 90, approximately.
Yes, the only incorrect values are given by the Balance function.
%Right Run
>>Fj=Fl+Fg;
>>[Gj,Lj]=MetodoSR(N,Fg,Fl);
>>Kij=ctteK1(Pj,Tj,N);
>>Xij=MatrizxijT(N,Fg,Fl,Gj,Kij,Zij);
>>[Lj,Gj,Xij,Yij]=Flujos(N,Lj,Xij,Fg,Fl,Kij);
>>[MatrizTH]=TH(N,TjF,Xij,Yij,Lj,Gj);
>>[HFlj,HFgj]=THFeed(N,TF,Zij);
%Here I execute another function that has nothing to do with this code.
%Eg. excelvalores, which only creates an excel file.
>>[Tj]=Balance(N,TjF,Yij,Xij,Lj,Gj,Fj,HFlj,HFgj,Qj,MatrizTH);
Here's the correct zip file. To load the workspace you just have to write:
load SRWorkspace on the command window
The workspace variables in the mat file in this zip are
Gj HFgj HFlj Kij Lj MatrizTH Xij Yij
These are not the inputs to the Balance() nor the SR() functions so I cannot run the function.

Sign in to comment.

Answers (0)

Categories

Asked:

on 21 Aug 2018

Edited:

on 22 Aug 2018

Community Treasure Hunt

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

Start Hunting!