Why do I get different outputs at different run's?
古いコメントを表示
Hi, there's something very odd happening with my code. I'm doing three times the same calculations for three different input values. I.E.:
x1=a;
x2=b;
x3=c;
y=x^2;
plot
Well, this should always give the same answer for x1, x2 and x3. My routine, a bit larger, does not any fuzzy nor random calculus. When I execute the program I get an answer and if I execute it a second time I get another one. Since second execution, I always get the same answer. If I change a, b and c I will get the same answer as the last execution, and I need to execute it a second time.
I added clear x1; clear x2; and clear x3; trying to get rid of old memory data but nothing changes.
The part of the code where it occurs is:
global C1 C2 C3;
C1=7.996328e4;
C2=4.859647e4;
C3=4.965676e4;
clear C;
C=C1;
[ resu1 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
clear C;
C=C2;
[ resu2 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
clear C;
C=C3;
[ resu3 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
rD1=resu1(2,:)./d;
rpD1=resu1(4,:)./d;
rD2=resu2(2,:)./d;
rpD2=resu2(4,:)./d;
rD3=resu3(2,:)./d;
rpD3=resu3(4,:)./d;
plot (rD1,rpD1,rD2,rpD2,rD3,rpD3)
title('rp/d vs r/d');
xlabel('r/d');
ylabel('rp/d');
nInt is a script proven to work correctly. I'm 99% sure the failure must be in this part of the code. MATLAB 2014.
8 件のコメント
Brendan Hamm
2015 年 7 月 16 日
編集済み: Brendan Hamm
2015 年 7 月 16 日
Does nInt use any sort of random number generation?
Furthermore, why are you using global variables?
Muthu Annamalai
2015 年 7 月 16 日
I don't see a definition for variable d. Is it a global? Does its value somehow change?
Felix Lauwaert
2015 年 7 月 16 日
Brendan Hamm
2015 年 7 月 17 日
This is a poor reason for using global variables. Consider re-writing your code to just accept these variables as input and I would not be surprised if your problem vanishes.
Brendan Hamm
2015 年 7 月 17 日
See: Global Variable. "They are usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere."
Brendan Hamm
2015 年 7 月 17 日
The last thing I would point out is just because C1,C2,C3 are global, this does not make C global. But other than this, there is not enough information to know with certainty what is wrong.
John D'Errico
2015 年 7 月 17 日
I like the statement that you are 99% sure the problem is in this code, and ONLY in that code, since you are POSITIVE that nInt is written correctly. Of course, we can't see what is in that code, so how can we possibly disprove your statement? For that matter, how can we know what you have done wrong? We are not given the ability to see fun either. So any guess on our part is just random.
That nInt has been proven to work correctly merely means that you have not seen it fail. It may just mean that your testing was insufficient.
As for the use of global variables, they are a bad thing. They make debugging a mess, as I think you are finding out.
Using globals comes in second place on Doug's list here:
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!