Given the function $y=\sin x$ defined over the region $-\pi \leq x \leq \pi$, I need to implement a "do loop" such that I sweep over 100 or so points $-1 \leq y \leq 1$ and find precisely the two $x$ values which map to this $y$ under $\sin x$. For example, with $y=1/8$, I have the following code:

 NSolve[Sin[x] == 1/8 && -Pi <= x <= Pi, x, WorkingPrecision -> 20]

which outputs:

{{x -> 0.12532783116806539687}, {x -> 3.0162648224217278416}}

Given these two points, call them $x_{1}$ and $x_{2}$ I want to plot a point $f(1/8) = |x_{1}-x_{2}|$. In other words, I want to sweep over 100 or so $-1 \leq y \leq 1$ and plot $|x_{1}-x_{2}|$ at each of these points.

So, I'm wondering what the most efficient way to do this would be? In particular, I'm worried about storing the variables

 {{x -> 0.12532783116806539687}, {x -> 3.0162648224217278416}}

How can I store these variables within the loop or how would I call them? Perhaps the loop can output an array of my $y$ values and an array of $|x_{1}-x_{2}|$ values and I can trivially plot them from there?


So for my actual case of interest I have a function which essentially looks similar in form to $\sin x$, but is messier:

$$\lambda(x) = -\frac{1}{2}\frac{\theta'_{3}(\pi\, x\ |\ \tau)}{\theta_{3}(\pi\, x\ |\ \tau)}$$

Where these are the Jacobi theta functions. Mathematica takes the input EllipticTheta[3, Pi*x, Exp[I*Pi*tau]]. Like I said, this behaves similarly to $\sin$ over the region $[-1/2,1/2]$. So, what I'd like to do is, for a given $\tau$ that won't change, sweep through values $a \in [-\lambda_{\rm{max}}, \lambda_{\rm{max}}]$ and for each such value, find the two $x$ values which map to $a$ under $\lambda(x)$.

Given these two numbers, call them $x_{1}$ and $x_{2}$, I then would like to compute,

$\quad \quad \wp(2x_{1} + 1 + \tau \ | \ 1,\, \tau)-\wp(2x_{2}+1+\tau \ | \ 1,\, \tau)$

I'm getting comfortable with NSolve and FindMax, but sweeping over 100 or so $a \in [-\lambda_{\rm{max}},\, \lambda_{\rm{max}}]$ and storing and plotting, that's way over my head!

  • $\begingroup$ Try to avoid explicit looping in Mathematica. Use list operations instead. See for example mathematica.stackexchange.com/a/18396/193 $\endgroup$ Jul 27 '15 at 23:08
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Jul 27 '15 at 23:38
  • 1
    $\begingroup$ spietro: it is commendable that you tried to present your problem using a simpler, minimal case. Maybe you could also expand on the actual problem you are working on, as you hinted in the comments, that includes a Weierstrass-P function, to make sure that the solutions proposed are actually applicable to your real problem. $\endgroup$
    – MarcoB
    Jul 28 '15 at 1:14
  • 1
    $\begingroup$ @spietro It's very difficult to answer your edit without concrete functions. $\endgroup$ Jul 28 '15 at 2:07
  • 2
    $\begingroup$ Please stop posting TeX unless necessary. It doesn't help $\endgroup$ Jul 28 '15 at 2:10
      Abs[Subtract @@ (x /. 
          NSolve[Sin[x] == # && -Pi <= x <= Pi, x,  WorkingPrecision -> 20])]} & /@ 
                                                                Range[-1, 1, 2/100], 
      {_Rational, _Real}] // ListPlot

Mathematica graphics

Of course you may do

Plot[Abs[Subtract @@ (x /. Solve[Sin[x] == y && -Pi <= x <= Pi, x])], {y, -1, 1}]

Mathematica graphics

Or even better:

Plot[Pi - Abs@ArcSin@y, {y, -1, 1}]

Mathematica graphics


Based on our chat session, this is my best guess on what you want:

f[x_, t_] := -EllipticThetaPrime[3, Pi x, Exp[I t Pi]]/
                   EllipticTheta[3, Pi x, Exp[I t Pi]]/2
t1 = I/4;
xf = NArgMax[{f[x, t1], 0 < x < 1}, x];
inv = WeierstrassInvariants[{1, t1}];
f1[x_, t_] := WeierstrassP[2 x + 1 + t, inv]

pt[val_] := f1[(x /. FindRoot[f[x, t1] == val, {x, 0}]), t1] - 
            f1[(x /. FindRoot[f[x, t1] == val, {x, 0.5}]), t1]

Plot[pt[x], {x, 0, xf}]

Mathematica graphics

  • $\begingroup$ Thank you; I came up with a simpler example so as to explain my problem easier above. If I actually want to take the difference of Weierstrass-$\wp$ at the two points $x_{1}$ and $x_{2}$ we find, is that easy to generalize to? Can I replace your Subract with a more concrete functional form? $\endgroup$
    – Benighted
    Jul 27 '15 at 23:48
  • $\begingroup$ @spietro What is your simpler example? I don't get you. $\endgroup$ Jul 28 '15 at 1:01
  • $\begingroup$ @belisarius I think OP means that the Sin[x] function discussed in his question is the simpler example he had come up with, but his real target function is Weierstrass-P. $\endgroup$
    – MarcoB
    Jul 28 '15 at 1:11
  • 1
    $\begingroup$ @MarcoB But Weierstrass-P is an even function and Mathematica already has InverseWeierstrassP ... so I still don't get it :) $\endgroup$ Jul 28 '15 at 1:22
  • 2
    $\begingroup$ @Marco and bel, the branch cut structure of $\wp^{(-1)}$ is slightly messier than that of $\arcsin$, tho. At worst, OP may have to do everything in terms of EllipticF[]. The prospect is algebraically tear-inducing… $\endgroup$
    – J. M.'s torpor
    Jul 28 '15 at 2:39

You can get the set, these are ordered pairs, as you describe $(y,|x_2-x_1|)$ with the code:

 x /. NSolve[Rationalize[Sin[x] == y] && -Pi <= x <= Pi, x, 
   WorkingPrecision -> 20]]][[1]]}, {y, -.99, .99, .01}]

The only problem is that over this interval, there is only one solution at $y=\pm 1$. You see I have omitted those two values. The output here is just a table. Plot with ListPlot[ ].


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.