Sorting with high precision

3 views (last 30 days)
Simon
Simon on 25 Nov 2018
Commented: Simon on 27 Nov 2018
Hello,
first, allow me to present the setup:
Setup
I am working on a high-order spectral element method code, using rectangles. To add the extra degrees of freedom needed to define the higher order shape functions, I use the Gauss-Legendre integration points (which will later be used with a Gauss-Lobatto intergation scheme, but this is not relevant). The numbering of the nodes follows the usual counter-clockwise fashion, e.g. for a 2nd order element it looks like this:
4---------7---------3
| |
8 9 6
| |
1---------5---------2
The final product of the code is a vector p, whose i-th row has the coordinate in its first column and on its second column. Also, we have a vector u that contains the solution of my equation in each point, i.e. with being the approximate solution.
Visualization
After solving the equation, I wish to vizualize the solution. Seeing at the ordering is all mixed up, the easiest solution is to use the scatter command, i.e. scatter(p(:,1),p(:,2),u). This however gives a very pixelated solution, even for a very fine mesh or a high order. I've tried toying with the plot options for scatter, but to no avail. For a coarse mesh it gives a very distinct discrete solution, while for a fine mesh it doesn't reflect the quality of the approximation.
The most solid option are the imagesc/pcolor functions, which interpolate the data appropriately in order to create a smooth image. However, in order to use either of those commands it seems like you need a sorted version of p, and then image appropriately. What i do is the following:
px = p(:,1);
py = p(:,2);
[p_x,ix] = sort(px);
p_y = py(ix)
Where the problem arises
Seeing as the Gauss-Legendre points used for the degrees of freedom are (in general) irrational numbers, we have to truncate at the double precision. I am aware that when sorting, if MATLAB encounters the same number twice, it will put the one it encountered once first. However, the numbers seem to be different in the other of machine epsilon, so MATLAB sees them as two distinct numbers when sorting, therefore 'mixing' the result. The same thing happens if I use the unique command in order to choose only each occurunce of a certain position in x or y.
Solution?
Is there a way to choose how accurate the sort function is? As in, only sort numbers up to the 10th digit or so (same with unique). I have tried rounding up the node locations up to a digit, e.g. round(px,9) or round(px,9,'significant') but it doesn't seem to do the trick, as the rounding produces different numbers in some occasions as well.

Accepted Answer

Stephen23
Stephen23 on 25 Nov 2018
Edited: Stephen23 on 25 Nov 2018
"Is there a way to choose how accurate the sort function is?"
No. And rounding is not a good solution, because it introduces artefacts that are not in the data.
Use uniquetol. You might find its third output useful too (you could sort that, and use the indices to sort the original data).
  1 Comment
Simon
Simon on 27 Nov 2018
Not quite what I needed but it surely nudged me in the right direction to create a fix for this. Thanks Stephen!

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!