[PYTHON] PyODE Tutorial 1

PyODE sample execution example

I tried to execute "Tutorial 1" of Honke PyODE-sama site.

・ Click here for the article on "Tutorial 3" (https://qiita.com/emuai/items/840c776f146ac12ac416)

This "Tutorial 1" just gives an initial velocity to one object and sees how it looks. In the sense of "checking the operation of PyODE", the result display is only the value display to the standard output.

In this article, I tried adding a plot display with Matplotlib.

Code (all)

↓ This is the original code plus Matplotlib plot call and plot array creation.

Tutorial-1_plot.py


# pyODE example 1: with MPL-plot
    
import ode

# Create a world object
world = ode.World()
world.setGravity( (0,-9.81,0) )

# Create a body inside the world
body = ode.Body(world)
M = ode.Mass()
M.setSphere(2500.0, 0.05)
M.mass = 1.0
body.setMass(M)

body.setPosition( (0,2,0) )
body.addForce( (0,200,0) )

# Do the simulation...
total_time = 0.0
dt = 0.04

import numpy as np
nt = 100
txyzuvw = np.zeros( (7,nt+1) )
tn=0
while total_time<2.0:
    x,y,z = body.getPosition()
    u,v,w = body.getLinearVel()
    print( "%1.2fsec: pos=(%6.3f, %6.3f, %6.3f)  vel=(%6.3f, %6.3f, %6.3f)" % \
          (total_time, x, y, z, u,v,w) )
    if tn <= nt:
        txyzuvw[0][tn]=total_time
        txyzuvw[1][tn]=x
        txyzuvw[2][tn]=y
        txyzuvw[3][tn]=z
        #txyzuvw[4][tn]=u
        #txyzuvw[5][tn]=v
        #txyzuvw[6][tn]=w

    world.step(dt)
    total_time+=dt

    tn += 1

end_tn = tn

import matplotlib.pyplot as plt
# MPL-Plot 
plt.plot( txyzuvw[0][0:end_tn], txyzuvw[2][0:end_tn], label='Vertical position')
plt.xlabel('time [s]')
plt.ylabel('Vertical position [m]')
plt.savefig('./y.png')

↑ 20200506: Correction that the start position could not be recorded

result

(Please install numpy and matplotlib in advance.) When you run ... y.png

↑ This graph image (PNG) is saved as the current. It is the time change of the height of the object thrown in the vertical direction (Y coordinate).

Recommended Posts

PyODE Tutorial 2
PyODE Tutorial 1
PyODE Tutorial 3
sqlalchemy tutorial
Python tutorial
TensorFlow tutorial tutorial
Quantopian Tutorial LESSON 10
RabbitMQ Tutorial 5 (Topic)
Quantopian Tutorial LESSON 8
Quantopian Tutorial LESSON 1, 2
Quantopian Tutorial LESSON 6
Python Django Tutorial (2)
Python tutorial summary
RabbitMQ Tutorial 6 (RPC)
django tutorial memo
Ryu tutorial Addendum
Python Django Tutorial (8)
Python Django Tutorial (6)
Start Django Tutorial 1
Quantopian Tutorial LESSON 9
Quantopian Tutorial LESSON 5
Quantopian Tutorial LESSON 3
Quantopian Tutorial LESSON 7
5 minute gRPC tutorial
Python Django Tutorial (7)
Python Django Tutorial (1)
Quantopian Tutorial LESSON 4
Python Django tutorial tutorial
Try using PyODE
Quantopian Tutorial LESSON 11
Python Django Tutorial (3)
RabbitMQ Tutorial 4 (Routing)
zipline Beginner Tutorial
[Translation] hyperopt tutorial
Python Django Tutorial (4)