Reputation: 391
So I tried the matrix exponential function using MATLAB's Coder toolkit, and I got it to build. I went on to test to see if the results were reliable and more efficient. While the code was faster, the answer it produced was very slight.
I ran the original function and got an answer of:
p =
1 0 0 0 0 0 0 0 0 0 0 0
-0.05 1 -1.25e-07 5e-06 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
1.25e-07 -5e-06 -0.05 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 -0.05 1 0 0 0 0 0 0
-2.0833e-05 0.00125 -5.2083e-11 4.1667e-09 0 0 1 0.05 0 1.25e-07 0 0
-0.00125 0.05 -4.1667e-09 2.5e-07 0 0 0 1 0 5e-06 0 0
5.2083e-11 -4.1667e-09 -2.0833e-05 0.00125 0 0 0 -1.25e-07 1 0.05 0 0
4.1667e-09 -2.5e-07 -0.00125 0.05 0 0 0 -5e-06 0 1 0 0
0 0 0 0 -2.0833e-05 0.00125 0 0 0 0 1 0.05
0 0 0 0 -0.00125 0.05 0 0 0 0 0 1
And I then ran the mexed version of the function with the same input:
p2 =
1 0 0 0 0 0 0 0 0 0 0 0
-0.05 1 -1.25e-07 5e-06 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
1.25e-07 -5e-06 -0.05 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 -0.05 1 0 0 0 0 0 0
-2.0833e-05 0.00125 -5.2083e-11 4.1667e-09 0 0 1 0.05 0 1.25e-07 0 0
-0.00125 0.05 -4.1667e-09 2.5e-07 0 0 0 1 0 5e-06 0 0
5.2083e-11 -4.1667e-09 -2.0833e-05 0.00125 0 0 0 -1.25e-07 1 0.05 0 0
4.1667e-09 -2.5e-07 -0.00125 0.05 0 0 0 -5e-06 0 1 0 0
0 0 0 0 -2.0833e-05 0.00125 0 0 0 0 1 0.05
0 0 0 0 -0.00125 0.05 0 0 0 0 0 1
At first glance, these two matrices are equal, but they are actually VERY SLIGHTLY off:
p-p2
ans =
0 0 0 0 0 0 0 0 0 0 0 0
-6.9389e-18 0 0 8.4703e-22 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
-3.3881e-21 -2.1684e-19 -3.2312e-26 8.2718e-25 0 0 0 0 0 5.294e-23 0 0
-2.1684e-19 0 -8.2718e-25 5.294e-23 0 0 0 0 0 8.4703e-22 0 0
6.4623e-27 0 -3.3881e-21 2.1684e-19 0 0 0 0 0 0 0 0
0 0 0 6.9389e-18 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Most of the result is equivalent to the original function, but some of it is not. Also, the difference between the two is so small that I cant possibly believe it would be a mathematical error, rather than perhaps a precision error. And the reason I am so concerned with this, is because this does cause issues with the overlaying reason I am using the function.
Is there a reason why the mex function is off by so little, and is there a way to fix this?
Upvotes: 1
Views: 94
Reputation: 53809
The differences you observe are so little that you can consider the results actually are the same.
The way they are computed are different and that is why you do not get exactly the same result. Yet the difference is roughly machine epsilon
and is just due to the fact that computers do not work with an infinite precision but with some discrete representation of the numbers.
Upvotes: 3