# Different values obtained from same function file on different computers

5 views (last 30 days)
ahmad eldeeb on 23 Aug 2018
Edited: Stephen23 on 24 Aug 2018
Hello, I use particle swarm built-in function on matlab 2017b. I have my function file that to optimized. I got results. I took this results to another computer with the same version of matlab and resubtitute in the same function file with the optimum position but I don't get the same value of function. The two computers are 64 bit. Any solution, or what is the right answer.
##### 11 CommentsShow 9 older commentsHide 9 older comments
ahmad eldeeb on 24 Aug 2018
I tried or the commented lines. Some give the same results but with the same warning.
Stephen23 on 24 Aug 2018
Edited: Stephen23 on 24 Aug 2018
@ahmad eldeeb: I was not talking about inv, I was talking about your original question: i.e. why the same function on two different machines produces two different values with the same input data. You have made it clear that there are no random numbers, so your best option is to do some debugging, exactly as I described in my last comment. This has nothing (in particular) to do with inv or any warnings, it is solely a method to find out where the data in the two functions diverge from each other. At this point debugging and comparing the intermediate calculations is what I would do, and what other experienced users would do. You can too!
Probably the first and most important step is to confirm that the two functions are actually the same: the MATLAB IDE has a file comparison tool to help with that:

Sign in to comment.

### Answers (1)

Steven Lord on 23 Aug 2018
There are many reasons why calling the same function file twice with the same inputs may return different answers. The most common is, as Adam Danz said, calling the random number generators.
function y = myfun415924(x)
y = x + randi([0 10], 1);
Now call this function repeatedly:
z = zeros(1, 10);
for k = 1:10
z(k) = myfun415924(5);
end
disp(z)
You may have some duplicate values in z, but you will also have some values that are different. If you want reproducible results in this type of scenario, control the random number generator to generate random numbers that are repeatable. [Note: you may be tempted later on to adapt my example to change the random number generator state at each iteration of a loop to generate "more random" numbers. DON'T. As that documentation page states, reseeding too frequently may be hazardous to the statistical properties of your random numbers.]
z = zeros(1, 10);
for k = 1:10
rng default
z(k) = myfun415924(5);
end
disp(z)
##### 7 CommentsShow 5 older commentsHide 5 older comments
ahmad eldeeb on 23 Aug 2018
i don'y use rand or particleswarm in the function fuile.
Adam Danz on 23 Aug 2018
Hi Ahmad, I just glanced at your code briefly and saw 3 built-in functions in the first few lines:
size(); round(); sum()
A built-in function is a function that comes along with Matlab. Many of Matlab's built-in function use randomization. I suggest you take the advice of Jan and Stephen and it also wouldn't hurt to try the random number seed that I suggested above to rule out any random processes that you may be unaware of.

Sign in to comment.

### Categories

Find more on Particle Swarm in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!