Why do I get different outputs at different run's?

11 views (last 30 days)
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.:
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;
clear C;
[ resu1 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
clear C;
[ resu2 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
clear C;
[ resu3 ] = nInt( fun , ab , tspan, titol, rotulacio,C );
plot (rD1,rpD1,rD2,rpD2,rD3,rpD3)
title('rp/d vs r/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.
John D'Errico
John D'Errico on 17 Jul 2015
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.

Sign in to comment.

Accepted Answer

Felix Lauwaert
Felix Lauwaert on 17 Jul 2015
Thanks to everybody for your comments. I see global variables are a mess. I finally solved the problem this way: fun goes from the main script to nInt and from nInt to ode78, and so do their variables. I just put fun (and company) into nInt and called nInt without fun & co. input. I'll take care next time and I hope I become better in the future. I'm just starting with Matlab and programming in general.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!