Swapping of data points

11 views (last 30 days)
Sreeraj T
Sreeraj T on 12 Apr 2018
Edited: Chris on 11 Sep 2022 at 16:53
I have a MATLAB code which has been attached with this question. when you run it and plot it (which has been already done in the program), the output file produces a zig-zag pattern. What really happens is that the values that is being written are being done so very randomly. For e.g. look the r(118,4) which is -32.654347733133000 and look at the value just below it r(119,4) which is 24.888699542411665 + 14.273828030014714i. If you look at the trend in which the fourth column goes one can see that whatever given in r(119,4) is unacceptable, but on the other-hand whatever in r(119,6)= -32.892395565247682 is acceptable. And once you do this for all the places where this happens, the plot becomes clear.
My question is why is this "root swapping" happens? And how can this be solved? I have tried 'sort' command, but that destroys whole order of 'r'.
Any help will be deeply appreciated...

Answers (1)

Chris
Chris on 11 Sep 2022 at 16:32
Edited: Chris on 11 Sep 2022 at 16:53
The hopping happens because the output of roots() isn't sorted in a way that is useful for this script.
Further, when you use sort() on complex numbers, the function considers their absolute values by default. That doesn't work in this case.
Try:
r(i,:)=sort(roots(q),'ComparisonMethod','real');
or after the loop has finished:
r = sort(r,2,'ComparisonMethod','real');

Community Treasure Hunt

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

Start Hunting!