# meaning of sys=@(k, xkm1, uk) xkm1/2 + 25*xkm1/(1+xkm1^2) + 8*cos(1.2*k) + uk;

1 view (last 30 days)
Hi, I am new to matlab and trying to do particle filtering based on particle filter code example by matlab. At the beginning of the coding, there is a equation or function
sys=@(k, xkm1, uk) xkm1/2 + 25*xkm1/(1+xkm1^2) + 8*cos(1.2*k) + uk;
and I dont really understand the equation meaning. I reaaly need help on this.

Walter Roberson on 11 Nov 2022
Edited: Walter Roberson on 15 Nov 2022
In order to understand what MATLAB will calculate for that, then it is important to know that in your code at https://www.mathworks.com/matlabcentral/answers/1843283-particle-filter-error-solution?s_tid=srchtitle that all of those inputs are scalars. The code could also work if k and uk are non-scalars that are compatible sizes, but the code is broken for the case where xkm1 is non-scalar.
The / operator is the MATLAB "matrix right divide" operator. However in the case where the right hand side is a scalar, the / operator uses the behaviour of the ./ element-by-element division operation instead. Since 2 is scalar, xkm1/2 will be treated as xkm1./2 which will divide each element of xkm1 by 2, giving a temporary output the same size as xkm1 .
The * operator is the "inner product" operator, sometimes referred to as algebraic matrix multiplication. However, in the case where one side or both sides are scalars, the * operator uses the behaviour of the .* element-by-element multiplication operation instead. Since 25 is scalar, 25*xkm1 will be treated as 25.*xkm1 which will multiple each element of xkm1 by 25, giving a temporary output the same size as xkm1.
The ^ operator is the matrix-power operator. xkm1^2 is the same as xkm1*xkm1 where * is the inner-product operator. For inner product P*Q the number of columns of the first operand must be the same as the number of rows of the second operand, and the result would be the number of rows of the first operand by the number of columns of the second operand. If xkm1 is a square matrix, N x N (including 1 x 1) then that would be a (N x N) * (N x N) giving an N x N result and the operation would be permitted (even if it were not what you intended) But if xkm1 is a row vector, say 1 x N, then that would be (1 x N) * (1 x N) and the number of columns of the first operand, N, would not match the number of rows of the second operand, 1, and the operation would fail. So xkm1^2 will fail if xkm1 is not a square matrix (including that it would not fail for xkm1 scalar) . Chances are high that you wanted the element-by-element square .^2 instead of ^2
If you manage to get through xkm1^2 getting out a square result, then the 1+ would add one to each element, giving a square result (non-square would have failed in the ^2 operation.)
You now have xkm1 matrix-right-divide an matrix the same size, and you can be sure that the two are the same size if you managed to get through the ^2 operation. A matrix matrix-right-divide another matrix of the same size is well defined, and will be roughly the same as (25*xkm1) * pinv(1+xkm1^2) where pinv is pseudo-inverse and * is the inner-product operation. You will get out a matrix the same size as xkm1 ... provided that xkm1 was a square matrix. It just probably isn't what you wanted to calculate: you probably wanted to do element-by-element division, ./ instead of /
After the temporary variables for the two terms xkm1/2 and 25*xkm1/(1+xkm1^2) are calculated, the temporary variable will be added together, giving a result the same size as xkm1 .
8*cos(1.2*k) + uk
Again, scalar * something is treated the same as if you had properly specified .* element-by-element multiplication. so that will be treated as 8 .* cos(1.2 .* k) with appropriate multiplications and cosine calculation and more multiplication. Then that temporary variable would be added to the result from the first two terms, and it would be an error if k turned out to be an incompatible size to add to xkm1 . We cannot easily say what size the output will be; you would need to know about implicit expansion to calculate the size properly for the case where xkm1 and k are not scalars.
Finally uk is added to the temporary output, with it being an error if they are incompatible sizes. Again implicit expansion might occur so we cannot say at the moment what the output size would be, not without using the knowledge that in practice you will be passing in scalars.
Now, I suspect that you wanted to know why that expression is used, what the physical significance of that calculation is. However that is not a question about MATLAB. We can tell you how MATLAB will process the expression, but information about why that particular calculation and not a different one is off-topic here.
Thank you very much for the recommendation. I will try to read these and find the answer to my question.