[Tutor] hi

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Aug 8 16:13:59 CEST 2013


On 7 August 2013 22:43, Vick <vick1975 at orange.mu> wrote:
> Hi,
>
> It just plot one line. How to print several lines?

Just do:

line1, = plot([], [])
line2, = plot([], [])

And then

line1.set_data(x1, y1)
line2.set_data(x2, y2)

and so on.

> I've attached your example and I've corrected my own 3d code which works now
> but it is not an animation. So how can I plot it using the code you gave me?

Just change the data that you use for the line.

> By the way, I think you know that I'm trying to plot for n-body problem?
> Have you made a python code for the n-body problem?

No I haven't but I have written code for computing numerical solutions of ODEs.

> Mine works, but I'm just
> having trouble with 3d plotting of the orbits.

Once you have the lists of x, y, and z coordinates for the orbit just call:

plot(x, y, z)

(after creating an axes with projection='3d' and importing mplot3d).

What's not working for you?

> And also I have searched the
> web on how to integrate conservation of energy as you suggested to me, but I
> couldn't find any article that talks about 1st order ODEs for energy, and
> the annotation is different from mine.

I think you misunderstood. What I meant was if you implement your own
routine for solving ODEs you need to check its accuracy. In a 2-body
problem you would have an analytic solution in closed form. You can
then check the error between your numerical solution and the analytic
one. Investigating how this error scales with the step size dt enables
you to confirm the order of your integrator. The reason I suggested
this was that the original code you sent implemented the Runge-Kutta
algorithm incorrectly and I wanted you to check your code properly
before assuming it works.

In the case of the N-body problem you don't have an analytic solution
but, assuming Hamiltonian dynamics, energy is conserved. You can
compute the energy of your system at each state and compare how
constant it is over time in the output from your integrator. This
gives you a proxy for not having an analytic solution that you can use
to check the accuracy of your integrator: if the energy of your system
is not (approximately) maintained during integration then your
integration must be inaccurate.

> PS: Also how do you make the plot run only once without having to do it over
> and over again?

I'm not sure TBH. I normally use the animation API to save video files
for presentations.

You may be more interested in the approach here rather than using the
animation API:
http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib


Oscar


More information about the Tutor mailing list