Sorting with high precision

조회 수: 3 (최근 30일)
Simon
Simon 2018년 11월 25일
댓글: Simon 2018년 11월 27일
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.

채택된 답변

Stephen23
Stephen23 2018년 11월 25일
편집: Stephen23 2018년 11월 25일
"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
Simon
Simon 2018년 11월 27일
Not quite what I needed but it surely nudged me in the right direction to create a fix for this. Thanks Stephen!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by