3
$\begingroup$

I've got the solutions of three systems on two variables (a and b) that gives results sometimes almost equal. The three list are not of equal length! For example:

sol1={{1.13627*10^-10, 0.517141}, {1822.1623, 0.130514}, {4104.7111, 
0.303997}, {3.89363*10^19, 1.01968}};
sol2={{5.56174*10^-10, 0.517141}, {1822.1612, 0.130514}, {3592.4222, 0.26549}};
sol3={{1.75707*10^-11, 0.517141}, {1822.1723, 0.130514}, {3273.7822, 
0.228122}, {806077., 2.98304}}

As you can notice the second term of sol1, the second term of sol2, and the second term of sol3 are almost equal. I want to extract only one of them, for example {1822.1623, 0.130514}. So, can I extract the couple of solutions that are almost equal? For example if |ai-aj|<0.01 and |bi-bj|<0.0001? Thanks!

$\endgroup$
  • 1
    $\begingroup$ Related, perhaps duplicate: (21711). If possible Round (quantize) your values rather than using SameTest; see my answer there for explanation. $\endgroup$ – Mr.Wizard Sep 18 '14 at 15:15
2
$\begingroup$

You can use Intersection or Union with the option SameTest. For example, solutions for all 3 equations:

Intersection[sol1, sol2, sol3, 
  SameTest -> (Abs[#[[1]] - #2[[1]]] < 0.01 && Abs[#[[2]] - #2[[2]]] < 0.0001 &)]

(* {{1.13627*10^-10, 0.517141}, {1822.16, 0.130514}} *)

Or if you want to gather almost equal solutions:

Gather[Join[sol1, sol2, sol3], 
 Abs[#[[1]] - #2[[1]]] < 0.01 && Abs[#[[2]] - #2[[2]]] < 0.0001 &]

(* {{{1.13627*10^-10, 0.517141}, {5.56174*10^-10, 0.517141}, {1.75707*10^-11, 0.517141}}, 
    {{1822.16, 0.130514}, {1822.16, 0.130514}, {1822.17, 0.130514}},
    {{4104.71, 0.303997}}, 
    {{3.89363*10^19, 1.01968}}, 
    {{3592.42, 0.26549}}, 
    {{3273.78, 0.228122}}, 
    {{806077., 2.98304}}} *)

Or to find all solutions, which are close to sol1[[2]]

With[{s = sol1[[2]]},
 Select[Join[sol1, sol2, sol3], 
  Abs[#[[1]] - s[[1]]] < 0.01 && Abs[#[[2]] - s[[2]]] < 0.0001 &]]
(* {{1822.16, 0.130514}, {1822.16, 0.130514}, {1822.17, 0.130514}} *)

I hope one of these cases helps you.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ In fact I used it, but it gives two elements. I want only one element in the list. Thank you anyway $\endgroup$ – meriens Sep 18 '14 at 14:28
  • $\begingroup$ @meriens What do you mean? Find solutions that are close to {1822.1623, 0.130514} if it exist? $\endgroup$ – ybeltukov Sep 18 '14 at 14:30
  • $\begingroup$ y problem is that I do not consider solution too small. I solve myself this problem, and now I can use correctly your solution. Thanks again. $\endgroup$ – meriens Sep 18 '14 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.