Why do I get different outputs when executing the same code and same inputs twice?
Show older comments
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
madhan ravi
on 21 Aug 2018
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.
madhan ravi
on 21 Aug 2018
Also show us how you use the functions.
Giulliana Marin
on 21 Aug 2018
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.
Giulliana Marin
on 21 Aug 2018
Giulliana Marin
on 21 Aug 2018
OCDER
on 21 Aug 2018
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!?
Walter Roberson
on 21 Aug 2018
Could you zip up all relevant .m files and data that we would need to test, and attach the zip ?
Giulliana Marin
on 21 Aug 2018
Edited: Walter Roberson
on 21 Aug 2018
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).
OCDER
on 21 Aug 2018
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?
Giulliana Marin
on 21 Aug 2018
Giulliana Marin
on 21 Aug 2018
Edited: Walter Roberson
on 22 Aug 2018
Giulliana Marin
on 21 Aug 2018
Adam Danz
on 22 Aug 2018
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.
Answers (0)
Categories
Find more on Cell Arrays 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!