What is the double-period operator in MATLAB followed by a division operator (../)? - matlab

I am analyzing some MATLAB code where there is the following operator: ../. I cannot find any documentation on this operator explaining what it does. Can anyone explain it to me?
sp(it,:) = (ww).*(1../sigt).*exp(-.5*(e(it,:).^2)./(sigt.^2))*srpfrac);
Just being pedantic.

There is no ../ operator, the first . is associated with the 1 indicating a radix point and the ./ is element-wise division. This was likely written by someone who is used to Python where all numbers are considered integers unless the radix point is explicitly included. The more verbose equivalent is:
1.0 ./ sigt
In your case, the 0 has been omitted as it is optional.
To improve readability and future confusion, I would just change it to the following.
1 ./ sigt

Related

While loop does not stop - calculating armstrong number [duplicate]

Obviously, float comparison is always tricky. I have a lot of assert-check in my (scientific) code, so very often I have to check for equality of sums to one, and similar issues.
Is there a quick-and easy / best-practice way of performing those checks?
The easiest way I can think of is to build a custom function for fixed tolerance float comparison, but that seems quite ugly to me. I'd prefer a built-in solution, or at least something that is extremely clear and straightforward to understand.
I think it's most likely going to have to be a function you write yourself. I use three things pretty constantly for running computational vector tests so to speak:
Maximum absolute error
return max(abs(result(:) - expected(:))) < tolerance
This calculates maximum absolute error point-wise and tells you whether that's less than some tolerance.
Maximum excessive error count
return sum( (abs(result(:) - expected(:))) < tolerance )
This returns the number of points that fall outside your tolerance range. It's also easy to modify to return percentage.
Root mean squared error
return norm(result(:) - expected(:)) < rmsTolerance
Since these and many other criteria exist for comparing arrays of floats, I would suggest writing a function which would accept the calculation result, the expected result, the tolerance and the comparison method. This way you can make your checks very compact, and it's going to be much less ugly than trying to explain what it is that you're doing in comments.
Any fixed tolerance will fail if you put in very large or very small numbers, simplest solution is to use eps to get the double precision:
abs(A-B)<eps(A)*4
The 4 is a totally arbitrary number, which is sufficient in most cases.
Don't know any special build in solution. Maybe something with using eps function?
For example as you probably know this will give False (i.e. 0) as a result:
>> 0.1 + 0.1 + 0.1 == 0.3
ans =
0
But with eps you could do the following and the result is as expected:
>> (0.1+0.1+0.1) - 0.3 < eps
ans =
1
I have had good experience with xUnit, a unit test framework for Matlab. After installing it, you can use:
assertVectorsAlmostEqual(a,b) (checks for normwise closeness between vectors; configurable absolute/relative tolerance and sane defaults)
assertElementsAlmostEqual(a,b) (same check, but elementwise on every single entry -- so [1 1e-12] and [1 -1e-9] will compare equal with the former but not with the latter).
They are well-tested, fast to use and clear enough to read. The function names are quite long, but with any decent editor (or the Matlab one) you can write them as assertV<tab>.
For those who understand both MATLAB and Python (NumPy), it would maybe be useful to check the code of the following Python functions, which do the job:
numpy.allclose(a, b, rtol=1e-05, atol=1e-08)
numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

Why does `evalb(((3*x + 1)^5)(3*x - 1)/x^6 = ((3 + 1/x)^5)(3 - 1/x)) assuming (0 < x)` return `false` in Maple?

The question should perhaps be why does (3*x + 1)^5 * (3*x - 1) / x^6 = (3 + 1/x)^5 * (3 - 1/x) evaluate to false in Maple, even with the assumption that x > 0. The same expression evaluates to true in Mathematica and, of course, the statement itself is mathematically true under the previous assumption.
Maple's help pages don't give any clue on why this happens, and I would like someone to explain this behaviour before I think that Maple's evalb() is kind of broken. It is the type of questions I'm asking myself lately, since I'm deciding wether I should learn Maple or rather drop it and focus on learning Mathematica.
Thank you in advance.
If both sides of the equation are of type numeric (or extended complex numeric, etc), then evalb will test the equality. But for your symbolic expressions the evalb command will only test that they are the very same expression (by comparing memory address).
But they are not the very same expression. They are merely two different symbolic expressions for which you wish to do a mathematical test of equivalence.
restart;
expr:=(3*x+1)^5*(3*x-1)/x^6=(3+1/x)^5*(3-1/x):
is(expr);
true
testeq(expr);
true
simplify((rhs-lhs)(expr));
0

Why do multiple binary operators in a row work in MATLAB?

Why is this a valid MATLAB query?
3++4
which evaluates to 7. Even more disturbing:
3+-5
evaluates to -2.
Given the following, I expected
3+*5
to evaluate to 15. Instead it throws an error.
Possible resolution related to thewaywewalk's answer to my previous question at Why is a trailing comma in a cell array valid Matlab syntax?
+ and - are not only binary operators, they are also unary operators.
Documentation:
http://de.mathworks.com/help/matlab/ref/uplus.html
http://de.mathworks.com/help/matlab/ref/uminus.html
For this reasons, the first two lines are evaluated as 3+(+4) and 3+(-5) but the last fails because no unary multiplication exists.
Because Matlab's operator precedence places the unary plus above binary plus.
Therefore,
3++4
is parsed to
plus(3,uplus(4))

What are the best practices for floating-point comparisons in Matlab?

Obviously, float comparison is always tricky. I have a lot of assert-check in my (scientific) code, so very often I have to check for equality of sums to one, and similar issues.
Is there a quick-and easy / best-practice way of performing those checks?
The easiest way I can think of is to build a custom function for fixed tolerance float comparison, but that seems quite ugly to me. I'd prefer a built-in solution, or at least something that is extremely clear and straightforward to understand.
I think it's most likely going to have to be a function you write yourself. I use three things pretty constantly for running computational vector tests so to speak:
Maximum absolute error
return max(abs(result(:) - expected(:))) < tolerance
This calculates maximum absolute error point-wise and tells you whether that's less than some tolerance.
Maximum excessive error count
return sum( (abs(result(:) - expected(:))) < tolerance )
This returns the number of points that fall outside your tolerance range. It's also easy to modify to return percentage.
Root mean squared error
return norm(result(:) - expected(:)) < rmsTolerance
Since these and many other criteria exist for comparing arrays of floats, I would suggest writing a function which would accept the calculation result, the expected result, the tolerance and the comparison method. This way you can make your checks very compact, and it's going to be much less ugly than trying to explain what it is that you're doing in comments.
Any fixed tolerance will fail if you put in very large or very small numbers, simplest solution is to use eps to get the double precision:
abs(A-B)<eps(A)*4
The 4 is a totally arbitrary number, which is sufficient in most cases.
Don't know any special build in solution. Maybe something with using eps function?
For example as you probably know this will give False (i.e. 0) as a result:
>> 0.1 + 0.1 + 0.1 == 0.3
ans =
0
But with eps you could do the following and the result is as expected:
>> (0.1+0.1+0.1) - 0.3 < eps
ans =
1
I have had good experience with xUnit, a unit test framework for Matlab. After installing it, you can use:
assertVectorsAlmostEqual(a,b) (checks for normwise closeness between vectors; configurable absolute/relative tolerance and sane defaults)
assertElementsAlmostEqual(a,b) (same check, but elementwise on every single entry -- so [1 1e-12] and [1 -1e-9] will compare equal with the former but not with the latter).
They are well-tested, fast to use and clear enough to read. The function names are quite long, but with any decent editor (or the Matlab one) you can write them as assertV<tab>.
For those who understand both MATLAB and Python (NumPy), it would maybe be useful to check the code of the following Python functions, which do the job:
numpy.allclose(a, b, rtol=1e-05, atol=1e-08)
numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

Understanding colon notation in MATLAB

So I'm completely new to MATLAB and I'm trying to understand colon notation within mathematical operations. So, in this book I found this statement:
w(1:5)=j(1:5) + k(1:5);
I do not understand what it really does. I know that w(1:5) is pretty much iterating through the w array from index 1 through 5, but in the statement above, shouldn't all indexes of w be equal to j(5) + k(5) in the end? Or am I completely wrong on how this works? It'd be awesome if someone posted the equivalent in Java to that up there. Thanks in advance :-)
I am pretty sure this means
"The first 5 elements of w shall be the first 5 elements of j + the first 5 elements of k" (I am not sure if matlab arrays start with 0 or 1 though)
So:
w1 = j1+k1
w2 = j2+k2
w3 = j3+k3
w4 = j4+k4
w5 = j5+k5
Think "Vector addition" here.
w(1:5)=j(1:5) + k(1:5);
is the same that:
for i=1:5
w(i)=j(i)+k(i);
end
MATLAB uses vectors and matrices, and is heavily optimized to handle operations on them efficiently.
The expression w(1:5) means a vector consisting of the first 5 elements of w; the expression you posted adds two 5 element vectors (the first 5 elements of j and k) and assigns the result to the first five elements of w.
I think your problem comes from the way how do you call this statement. It is not an iteration, but rather simple assignment. Now we only need to understand what was assigned to what.
I will assume j,k, w are all vectors 1 by N.
j(1:5) - means elements from 1 to 5 of the vector j
j(1:5) + k(1:5) - will result in elementwise sum of both operands
w(1:5) = ... - will assign the result again elementwise to w
Writing your code using colon notation makes it less verbose and more efficient. So it is highly recommended to do so. Also, colon notation is the basic and very powerful feature of MATLAB. Make sure you understand it well, before you move on. MATLAB is very well documented so you can read on this topic here.