ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 30 Sep 2012 04:10:01 -0500How to plot field lines in sagehttps://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?
**Edit**: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$
Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$
The field line plot should look similar to this one:
![field line plot example](http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif)
I think in mathematica this is called a stream plot: http://reference.wolfram.com/mathematica/ref/StreamPlot.htmlFri, 28 Sep 2012 11:23:45 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/Answer by achrzesz for <p>I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?</p>
<p><strong>Edit</strong>: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$</p>
<p>Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$</p>
<p>The field line plot should look similar to this one:</p>
<p><img alt="field line plot example" src="http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif"/></p>
<p>I think in mathematica this is called a stream plot: <a href="http://reference.wolfram.com/mathematica/ref/StreamPlot.html">http://reference.wolfram.com/mathemat...</a></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14083#post-id-14083In the attached file one can see the maxima version
(code in my previous answer)
[sdfplot.pdf](/upfiles/13489572206305881.pdf)
Sat, 29 Sep 2012 12:24:16 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14083#post-id-14083Answer by kcrisman for <p>I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?</p>
<p><strong>Edit</strong>: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$</p>
<p>Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$</p>
<p>The field line plot should look similar to this one:</p>
<p><img alt="field line plot example" src="http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif"/></p>
<p>I think in mathematica this is called a stream plot: <a href="http://reference.wolfram.com/mathematica/ref/StreamPlot.html">http://reference.wolfram.com/mathemat...</a></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14084#post-id-14084People interested in this question will want to perhaps help out in making [our ticket for streamline plots](http://trac.sagemath.org/sage_trac/ticket/10775) a reality.Sat, 29 Sep 2012 15:10:15 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14084#post-id-14084Answer by calc314 for <p>I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?</p>
<p><strong>Edit</strong>: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$</p>
<p>Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$</p>
<p>The field line plot should look similar to this one:</p>
<p><img alt="field line plot example" src="http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif"/></p>
<p>I think in mathematica this is called a stream plot: <a href="http://reference.wolfram.com/mathematica/ref/StreamPlot.html">http://reference.wolfram.com/mathemat...</a></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14081#post-id-14081I don't think there's a way to automate this totally and avoid putting in all of the initial conditions. Here is another version.
var('x y')
E1=vector([x,3*y])/(sqrt(x^2+y^2))^(3/2)
E2=E1.subs(x=x-1)
F=(E1-E2)/norm(E1-E2)
int_conds=[[0.05,0.001],[0.05,0.005],[0.05,-0.001],[0.05,-0.005]]
solns=[desolve_odeint(F,init,srange(0,1.5,0.05),[x,y]) for init in int_conds]
nhalf=len(solns[0])/3
a=[arrow(solns[i][nhalf],solns[i][nhalf+1]) for i in range(0,len(solns))]
b=map(lambda x: line(x,thickness=2),solns)
show(sum(a)+sum(b)+point((0,0),size=50)+point((1,0),size=50)+plot_vector_field(F,(x,-0.1,1.1),(y,-.5,.5)),axes=false)
![image description](/upfiles/13489314166878159.png)Sat, 29 Sep 2012 05:10:30 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14081#post-id-14081Answer by calc314 for <p>I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?</p>
<p><strong>Edit</strong>: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$</p>
<p>Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$</p>
<p>The field line plot should look similar to this one:</p>
<p><img alt="field line plot example" src="http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif"/></p>
<p>I think in mathematica this is called a stream plot: <a href="http://reference.wolfram.com/mathematica/ref/StreamPlot.html">http://reference.wolfram.com/mathemat...</a></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14079#post-id-14079Thanks for the edit. See if something like this does what you want.
var('x y')
E1=vector([x,y])/(sqrt(x^2+y^2))^3
E2=E1.subs(x=x-1)
F=(E1-E2)
tstop=0.4
step=0.005
int_conds=[[0.5,0.1*k] for k in [1..3,-3..-1]]
solns=sum([line(desolve_odeint(F,init,srange(0,tstop,step),[x,y]),thickness=2) for init in int_conds])
solns2=sum([line(desolve_odeint(F,init,srange(0,-1*tstop,-1*step),[x,y]),thickness=2) for init in int_conds])
F=(E1-E2)/norm(E1-E2)
solns+solns2+plot_vector_field(F,(x,-0.2,1.2),(y,-1,1))Sat, 29 Sep 2012 02:59:39 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14079#post-id-14079Comment by sagefan for <p>Thanks for the edit. See if something like this does what you want.</p>
<pre><code>var('x y')
E1=vector([x,y])/(sqrt(x^2+y^2))^3
E2=E1.subs(x=x-1)
F=(E1-E2)
tstop=0.4
step=0.005
int_conds=[[0.5,0.1*k] for k in [1..3,-3..-1]]
solns=sum([line(desolve_odeint(F,init,srange(0,tstop,step),[x,y]),thickness=2) for init in int_conds])
solns2=sum([line(desolve_odeint(F,init,srange(0,-1*tstop,-1*step),[x,y]),thickness=2) for init in int_conds])
F=(E1-E2)/norm(E1-E2)
solns+solns2+plot_vector_field(F,(x,-0.2,1.2),(y,-1,1))
</code></pre>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18978#post-id-18978Sorry I had two typos in my Edit, which I fixed just now, see above. You should not normalize F! Perhaps you could include those changes in your examples. Thanks.Sat, 29 Sep 2012 05:21:04 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18978#post-id-18978Comment by sagefan for <p>Thanks for the edit. See if something like this does what you want.</p>
<pre><code>var('x y')
E1=vector([x,y])/(sqrt(x^2+y^2))^3
E2=E1.subs(x=x-1)
F=(E1-E2)
tstop=0.4
step=0.005
int_conds=[[0.5,0.1*k] for k in [1..3,-3..-1]]
solns=sum([line(desolve_odeint(F,init,srange(0,tstop,step),[x,y]),thickness=2) for init in int_conds])
solns2=sum([line(desolve_odeint(F,init,srange(0,-1*tstop,-1*step),[x,y]),thickness=2) for init in int_conds])
F=(E1-E2)/norm(E1-E2)
solns+solns2+plot_vector_field(F,(x,-0.2,1.2),(y,-1,1))
</code></pre>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18979#post-id-18979Thanks, that's almost what I want. The missing point is that I want not only one or three lines but something like I added in the original post above.Sat, 29 Sep 2012 03:28:25 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18979#post-id-18979Comment by calc314 for <p>Thanks for the edit. See if something like this does what you want.</p>
<pre><code>var('x y')
E1=vector([x,y])/(sqrt(x^2+y^2))^3
E2=E1.subs(x=x-1)
F=(E1-E2)
tstop=0.4
step=0.005
int_conds=[[0.5,0.1*k] for k in [1..3,-3..-1]]
solns=sum([line(desolve_odeint(F,init,srange(0,tstop,step),[x,y]),thickness=2) for init in int_conds])
solns2=sum([line(desolve_odeint(F,init,srange(0,-1*tstop,-1*step),[x,y]),thickness=2) for init in int_conds])
F=(E1-E2)/norm(E1-E2)
solns+solns2+plot_vector_field(F,(x,-0.2,1.2),(y,-1,1))
</code></pre>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18967#post-id-18967I've updated my code a good bit to include your edits and some solving backwards and forwards from the initial condition.Sun, 30 Sep 2012 04:10:01 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18967#post-id-18967Answer by calc314 for <p>I want to plot the field lines of an electric field, take for example that of an electric dipole. How can I do this in sage?</p>
<p><strong>Edit</strong>: Perhaps my question was formulated too much in physicist slang... Mathematically I just want to plot the integral curves of a vector field for example: let $\vec{r}(x,y) = (x,y)$, $\vec{E_1} = \frac{\vec{r}}{|r|^{3}}$ and $\vec{E_2}(x,y) = \vec{E_1}(x-1,y)$</p>
<p>Then I want to plot the field lines of $\vec{E} = \vec{E_1} - \vec{E_2}$</p>
<p>The field line plot should look similar to this one:</p>
<p><img alt="field line plot example" src="http://labman.phys.utk.edu/phys222/modules/m1/images/dipole.gif"/></p>
<p>I think in mathematica this is called a stream plot: <a href="http://reference.wolfram.com/mathematica/ref/StreamPlot.html">http://reference.wolfram.com/mathemat...</a></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14075#post-id-14075I'm not sure whether you already have equations to use or not. How you plot these all depends, of course, on how the equations are given. But, here is something. Using information in section D.73.3 of this [webpage](http://www.eng.fsu.edu/~dommelen/quantum/style_a/nt_maxders.html#sec:nt_maxders) on quantum mechanics, we need to plot ${(x^2+z^2)}^{(3/2)}=c x^2$ for various values of $c$. The following code produces a plot.
var('x z')
sum([implicit_plot((x^2+z^2)^(3/2)==c*x^2,(x,-5,5),(z,-5,5)) for c in [1,2,3,4]])
![image description](/upfiles/13488691897926463.png)Fri, 28 Sep 2012 11:53:20 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?answer=14075#post-id-14075Comment by sagefan for <p>I'm not sure whether you already have equations to use or not. How you plot these all depends, of course, on how the equations are given. But, here is something. Using information in section D.73.3 of this <a href="http://www.eng.fsu.edu/~dommelen/quantum/style_a/nt_maxders.html#sec:nt_maxders">webpage</a> on quantum mechanics, we need to plot ${(x^2+z^2)}^{(3/2)}=c x^2$ for various values of $c$. The following code produces a plot.</p>
<pre><code>var('x z')
sum([implicit_plot((x^2+z^2)^(3/2)==c*x^2,(x,-5,5),(z,-5,5)) for c in [1,2,3,4]])
</code></pre>
<p><img alt="image description" src="/upfiles/13488691897926463.png"/></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18982#post-id-18982That are the equipotential surfaces and not the field lines. You can assume that I know an analytic expression for the electric field strength at every point.Fri, 28 Sep 2012 21:40:28 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18982#post-id-18982Comment by calc314 for <p>I'm not sure whether you already have equations to use or not. How you plot these all depends, of course, on how the equations are given. But, here is something. Using information in section D.73.3 of this <a href="http://www.eng.fsu.edu/~dommelen/quantum/style_a/nt_maxders.html#sec:nt_maxders">webpage</a> on quantum mechanics, we need to plot ${(x^2+z^2)}^{(3/2)}=c x^2$ for various values of $c$. The following code produces a plot.</p>
<pre><code>var('x z')
sum([implicit_plot((x^2+z^2)^(3/2)==c*x^2,(x,-5,5),(z,-5,5)) for c in [1,2,3,4]])
</code></pre>
<p><img alt="image description" src="/upfiles/13488691897926463.png"/></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18981#post-id-18981Can you post the expression?Sat, 29 Sep 2012 01:37:47 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18981#post-id-18981Comment by sagefan for <p>I'm not sure whether you already have equations to use or not. How you plot these all depends, of course, on how the equations are given. But, here is something. Using information in section D.73.3 of this <a href="http://www.eng.fsu.edu/~dommelen/quantum/style_a/nt_maxders.html#sec:nt_maxders">webpage</a> on quantum mechanics, we need to plot ${(x^2+z^2)}^{(3/2)}=c x^2$ for various values of $c$. The following code produces a plot.</p>
<pre><code>var('x z')
sum([implicit_plot((x^2+z^2)^(3/2)==c*x^2,(x,-5,5),(z,-5,5)) for c in [1,2,3,4]])
</code></pre>
<p><img alt="image description" src="/upfiles/13488691897926463.png"/></p>
https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18980#post-id-18980See my editSat, 29 Sep 2012 02:29:04 -0500https://ask.sagemath.org/question/9365/how-to-plot-field-lines-in-sage/?comment=18980#post-id-18980