4
$\begingroup$

I want to solve a nonlinear equation eq in a Manipulate environment as the following one using a Locator

Manipulate[
 eq = {u1*Cos[u2], u1*Sin[u2]};
 cond = 0 <= u1 <= 10 && 0 <= u2 < 2 Pi;
 nsol = NSolve[{p == eq, cond}, {u1, u2}, Reals] // Quiet;
 Grid[{
   {Graphics[{Point[p]}, PlotRange -> 5, Axes -> True]}
   , {nsol}
   }]
 , {{p, {1, 1}}, Locator}
 ]

enter image description here

I am aware that the nonlinear equation is the transformation into cylindrical coordinates, and I know how to get these analytically. But this is just a simple example, later I want to solve other more complex equations with other conditions. I am interested in computing the solution smoothly while dragging the locator across the plot.

My problems are

  1. the performance is poor (I suppose I have to use Dynamic sowhere but I dont know where and more importantly why). How do I have to program this correctly in order to have a smooth computation while dragging the locator in the graphic?
  2. I have to tell NSolve to be Quiet, otherwise I get the warning, that the system can not be solved but with inexact coefficients (which is fine for me). Is there any other way to do this already in NSolve?

EDIT: computation and usage of results of NSolve

@Kuba: thank you for the information, I will read through everything you posted. I have a further question (which I was not able to solve with the first version of your answer), but if this is answered in the references you gave, then just say it and I will find my way there.

Sorry for expanding the question, but my first formulation was not precise. I would also want to use the results of NSolve for other purposes within Manipulate, e.g., computing a field with the results and drawing an arrow as follows

Manipulate[
 eq = {u1*Cos[u2], u1*Sin[u2]};
 cond = 0 <= u1 <= 10 && 0 <= u2 < 2 Pi;
 nsol = NSolve[{p == eq, cond}, {u1, u2}, Reals] // Quiet;
 field = {u1 + Cos[u2^2], -Sin[u2]};
 Grid[{
   {Graphics[{Point[p], Arrow[{p, p + field /. nsol[[1]]}]}, 
     PlotRange -> 5, Axes -> True]}
   , {nsol}
   , {field /. nsol[[1]]}
   }
  , Alignment -> Left
  ]
 , {{p, {1, 1}}, Locator}
 ]

enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$

This is a must read before we proceed:

especially Nesting Dynamic and Synchronous versus Asynchronous Dynamic Evaluations chapters.


Preemptive link calculations are blocking front end till they return the result. That is why locator feels heavy, you won't see next position update till NSolve is finished.

So we need to run it through the main link (SynchornousUpdating->False) while the rest is separated (by additional Dynamic in Point) to not create repeatedly this part.

Manipulate[

 Grid[{{
    Graphics[{Point[Dynamic@p]}, PlotRange -> 5, Axes -> True]
    }, {
    Dynamic[
     eq = {u1*Cos[u2], u1*Sin[u2]};
     cond = 0 <= u1 <= 10 && 0 <= u2 < 2 Pi;
     nsol = NSolve[{p == eq, cond}, {u1, u2}, Reals] // Quiet;
     nsol
     ,
     TrackedSymbols :> {p},
     SynchronousUpdating -> False
     ]
    }}
  ],
 {{p, {1, 1}}, Locator}]

enter image description here

Related topics:

$\endgroup$
3
  • $\begingroup$ Thank you veeeery much for the references! One question (see may be new edit of question): is it also possible to use the results of NSolve also for further computations and objects (for example an arrow in the graphics)? $\endgroup$ Commented Jan 5, 2017 at 15:31
  • 1
    $\begingroup$ @MauricioLobos With field = {u1 + Cos[u2^2], -Sin[u2]} added to Dynamic and Dynamic@Arrow[{p, p + field /. nsol[[1]]}] added to graphics it seems to work. $\endgroup$
    – Kuba
    Commented Jan 5, 2017 at 15:36
  • $\begingroup$ Awesome! Thanks, got it! $\endgroup$ Commented Jan 5, 2017 at 15:54

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.