`Hello, I’m a novice at programming and am currently learning how to do simulations for my paper. I’m currently practicing on Octave. I have 3 variables v1, v2 and v3 equal to 1. So v1 + v2 + v3 = 1. I’m trying to get every possible combination of these three variables with increments of 0.1 via looping. I’m not aware of any advanced commands or tools so I just used the basics of C++ that we’ve been taught in one of our classes.
Here’s my code:
v1 = 1; v2 = 0; v3 = 0; n = 1; while (v2 < 1) v3 = 0; v4 = v2; for v3 = 0:0.1:v4 varsum = v1 + v2 + v3; data(1,n) = v1; data(2,n) = v2; data(3,n) = v3; data(4,n) = varsum; v2 -= 0.1; n+=1 endfor v1 -= 0.1; v2 = 1 - v1; endwhile
My expected results would look like this:
v1 v2 v3
10 0 0
9 1 0
9 0 1
8 2 0
: : :
0 2 8
0 1 9
0 0 10
I know it’s crude, I just programmed it the way I would’ve done it manually. I know there are 66 combinations in total, but I only get 65.
When checking the results, I found that it skips the
9 0 1
result and goes straight to
8 2 0
Everything else looks fine. I’ve had issues before with variable/s refusing to get 0 value so I’m afraid to modify it further out of fear I’ll make it worse. Any help would be much appreciated. Any tips on how to do this more efficiently are welcome too. Thanks.`
Atlas Titan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.