Greetings,
I have NSolve solution that gives me roots but not in right order as expected. When I plot data, I am getting the following image (an illustrated simple example):
Ideally, my data should be sorted so that it can be plotted like this:
Below is the code that simulates roots solution:
ClearAll;
max=6;steps=0.05;
x=Range[0,max,steps];
z=RandomInteger[1,Length[x]];
f1=0.05*Sin[x]+0.1;
f2=0.05*Sin[x]-0.1;
f3=-0.05*Sin[x]+0.1;
f4=-0.05*Sin[x]-0.1;
ideally=ListLinePlot[{f1,f2,f3,f4}]
ph=0.1;offs=0.1;
e1=(Abs[0.05*Sin[x+ph]]+offs)*(-1)^z;
e2=(Abs[0.05*Sin[x+ph]]-offs)*(-1)^z;
e3=(-Abs[0.05*Sin[x+ph]]+offs)*(-1)^(z);
e4=(-Abs[0.05*Sin[x+ph]]-offs)*(-1)^(z);
e5={e1,e2,e3,e4};
roots=Transpose[{e1,e2,e3,e4}];
ListPlot[e5,PlotRange->{-0.2,0.2}]
roots1=Abs[roots];
ListPlot[Transpose@roots1,PlotRange->{-0.2,0.2}]
I have access to Mathematica v9. My root solution is always symmetrical in y-axis or have one curves at center. The first idea is to fold along the x-axis, take the derivative so that it will give me min,max points that are close to each other at the intersection of supposed two sine waves and then flip the other halves from that point. The other approach is spline fit but it works only for one curve. Has any one experience with this form of fitting? Is it possible to do on two curves intersecting? Any ideas for sorting roots data so that they are ordered for sine waves instead of abs(sine waves)? How do I find seemingly intersecting points?
I also add code that gives similar results using NSolve. Is it possible to modify NSolve to correct the problem without being aware of equation that makes this root solution. That is why I called it a blackbox.
Remove["Global`*"];//Quiet
ClearAll;
blackbox[a_]=(x-Sin[a]-1)*(x-Sin[a-Pi]-1)
(-1+x-Sin[a]) (-1+x+Sin[a])
xx=Range[0,2*Pi,0.01];
roots=Table[x/.NSolve[blackbox[b]==0,x],{b,xx}];
ListPlot[Transpose@roots]