2
$\begingroup$

I would like to solve an equation for different parameter values, thus, I used Table[] to create an array of solutions. For some parameter combinations {k,A} NSolve does not give an answer, but when I call NSolve outside of Table with the same parameters, it actually works. Below is a minimal example of this behaviour - any ideas where this comes from are much appreciated :-). FYI I am using Mathematica 13.0 MacOS.

function[k_, A_, u1_, u2_] := (1 + Tanh[k (u1 - 2 A u2)])/2 - u1
results[k_, A_] := NSolve[function[k, A, u1, u2] == 0 && function[k, A, u2, u1] == 0 && 0 <= u1 <= 1 &&0 <= u2 <= 1, {u1, u2}]
{results[0.2, 2.2], results[0.2, 2.3], results[0.2, 2.4]}
Table[results[0.2, b], {b , 2.2, 2.4, 0.1}]

Follow up: Here are some more variations. It seems there is a difference between numbers explicitly typed in and those created automatically. When I type all numbers in Table, it works, but when I create a list of numbers with Range and use that in Table, it does not work anymore...

Table[results[0.2, b], {b, {2.2, 2.3, 2.4}}] // Quiet
values = Range[2.2, 2.4, 0.1];
Table[results[0.2, b], {b, values}] // Quiet

And similar issue with Map

Map[results[0.2, #] &, {2.2, 2.3, 2.4}] // Quiet
Map[results[0.2, #] &, values] // Quiet
$\endgroup$
2
  • $\begingroup$ Table[results[0.2, b], {b, {2.2, 2.3, 2.4}}] $\endgroup$
    – cvgmt
    Commented Jan 6, 2022 at 13:03
  • $\begingroup$ Yes, that works, but for a large parameter search, I can not type all of them explicitly. $\endgroup$
    – Tim
    Commented Jan 6, 2022 at 13:08

2 Answers 2

3
$\begingroup$

Edit

Another way is use Rationalize to the values k and A

function[k_, A_, u1_, u2_] := (1 + Tanh[k (u1 - 2 A u2)])/2 - u1;
results[k_, A_] := 
 NSolve[function[Rationalize@k, Rationalize@A, u1, u2] == 0 && 
   function[Rationalize@k, Rationalize@A, u2, u1] == 0 && 
   0 <= u1 <= 1 && 0 <= u2 <= 1, {u1, u2}]
values = Range[2.2, 2.4, 0.1];
Table[results[0.2, b], {b, values}]
Table[results[0.2, b], {b, 2.2, 2.4, .1}]

Original

Maybe use exact number by Rationalize

values = Range[2.2, 2.4, 0.1] // Rationalize;
Table[results[0.2, b], {b, values}]
$\endgroup$
2
  • $\begingroup$ Great, that seems to work - thanks a lot @cvgmt! $\endgroup$
    – Tim
    Commented Jan 6, 2022 at 13:19
  • $\begingroup$ @Tim We can use Rationalize to k and A then avoid all the warning. $\endgroup$
    – cvgmt
    Commented Jan 6, 2022 at 13:24
3
$\begingroup$

Try Map (don't know why Table doesn't work)

Map[results[0.2, #] &, { 2.2, 2.4, 0.1}] // Quiet
(*{{{u1 -> 0.375147, u2 -> 0.375147}}, 
{{u1 -> 0.364816,u2 -> 0.364816}}, 
{{u1 -> 0.54336, u2 -> 0.54336}}}*)

Edit I still do not understand why NSolve (numerical solver!) only evaluates with Rationalize!

NMinimize solves without this limitation:

res[k_, A_] := 
NMinimize[{1, function[k, A, u1, u2] == 0,function[k, A, u2, u1]== 0, 0 <= u1 <= 1, 0 <= u2 <= 1}, {u1, u2}] [[2]]

Table[res[0.2, b], {b, {2.2, 2.4, .1}}]
(*{{u1 -> 0.375147, u2 -> 0.375147}, {u1 -> 0.364816,u2 -> 0.364816}, {u1 -> 0.54336, u2 -> 0.54336}}*)
$\endgroup$
1
  • $\begingroup$ Thanks for the suggestion, but unfortunately it does not solve the issue! Here, you use 0.1 as an input value, not 2.2, 2.3, 2.4. like in Table. It seems to me there is a difference between numbers explicitly typed in and those generated automatically. I added some more variations in my questions to show this. When you explicitly write all numbers in Table it works, but when I create a list of numbers with Range and use that list in Table, it does not work.. $\endgroup$
    – Tim
    Commented Jan 6, 2022 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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