3

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?

Edit

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!

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Benighted
  • 1,327
  • 1
  • 10
  • 14

2 Answers2

8
Cases[Quiet@{#, 
      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


Edit

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

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • 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? – Benighted Jul 27 '15 at 23:48
  • @spietro What is your simpler example? I don't get you. – Dr. belisarius Jul 28 '15 at 01:01
  • @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. – MarcoB Jul 28 '15 at 01:11
  • 1
    @MarcoB But Weierstrass-P is an even function and Mathematica already has InverseWeierstrassP ... so I still don't get it :) – Dr. belisarius Jul 28 '15 at 01:22
  • @belisarius Agreed (and +1 for exploring multiple methods of attack!). I wonder if OP had not realized the existence of InverseWeierstrassP, or the implication of your example with ArcSin. Anyway, hopefully OP will make his meaning more apparent... – MarcoB Jul 28 '15 at 01:39
  • @belisarius Indeed, I meant that $\sin$ was supposed to be a simpler example. I added an edit above. Thanks! – Benighted Jul 28 '15 at 02:04
  • 2
    @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… – J. M.'s missing motivation Jul 28 '15 at 02:39
  • @Guesswhoitis. I added the Special-Functions tag to attract your kind attention. I believe something wasn't fluid enough in this communication – Dr. belisarius Jul 28 '15 at 02:42
  • Unfortunately, this does not look like one of the things I can resolve in one sitting. I'll look into it when I have a clear head and a ream of letter-size paper nearby. – J. M.'s missing motivation Jul 28 '15 at 03:25
  • @belisarius Your recent updates are oh so close to exactly what I need. I've been mucking around for the last hour or so trying to figure out how to make your code use NSolve instead of FindRoot because this function is periodic so when you say find a root near 0 or 1/2, it's finding the one out of the range $-1/2 < x < 1/2$. I keep getting ugly errors like "Solve was unable to solve the system with inexact coefficients," when I try to use `NSolve.' Any idea how to fix this? – Benighted Jul 28 '15 at 07:05
  • My code is pts = {x /. NSolve[f[x, I/4] == # && -1/2 <= x <= 1/2, x, Reals]} & /@ rr; and from this I get errors – Benighted Jul 28 '15 at 07:07
2

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

Table[{y, 
Abs[Differences[
 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[ ].

J. W. Perry
  • 1,080
  • 1
  • 6
  • 11