Vpython-grafer
Hejsan!
Jag är helt ny med Vpython och jag fick en uppgift som lyder såhär:
Folj introduktionen till VPython som finns pa sidan: http://vpython.
org/contents/docs/VPython_Intro.pdf. For att lara sig ett nytt bibliotek
ar det viktigt att bryta upp helheten i sma steg som ar latta att
felsoka, precis som i detta exempel.
2. Koden i VPython ar anvandbar for att kvalitativt illustrera simuleringens
forlopp i realtid. For fysikalisk data ar det daremot nodvandigt med grafer
som kvantitativt beskriver storheter, t.ex. positionens komponenter som
funktioner av tiden. For att lagga till en graf i VPython kan visual.graph
anvandas, se funktionen gcurve(...). Ett kanske vanligare alternativ ar
att spara datan och gora en graf i efterhand med t.ex. matplotlib, se
matplotlib.pyplot.plot(...) funktionen, som senare kan importeras i en
rapport eller artikel. Producera minst en lamplig graf for att beskriva
simuleringens resultat i en rapport.
Jag har gjort uppgift 1, det är alltså uppgift 2 jag har fastnat på. Kan någon hjälpa mig snälla?
Såhär långt har jag kommit med uppgift 1:
from visual import *
ball = sphere(pos=(-5,0,0), radius=0.5, color=color.cyan)
ball2 = sphere(pos=(5,0,0), radius=1, color=(1,0.7,0.7))
wallR = box(pos=(6,0,0), size=(0.2,12,12), color=color.green)
wallL = box(pos=(-6,0,0), size=(0.2,12,12), color=color.green)
wallU = box(pos=(0,6,0), size=(12,0.2,12), color=color.blue)
wallD = box(pos=(0,-6,0), size=(12,0.2,12), color=color.blue)
wallB = box(pos=(0,0,-6), size=(12,12,0.2), color=color.red)
ball.velocity = vector(25,5,15)
ball2.velocity = vector(10,2,5)
ball.acc = 2.5
deltat = 0.005
t = 0
vscale = 0.1
vscale2 = 0.5
varr = arrow(pos=ball.pos, axis=vscale*ball.velocity, color=color.yellow)
varr2 = arrow(pos=ball2.pos, axis=vscale*ball2.velocity, color=color.magenta)
ball.trail = curve(color=ball.color)
ball2.trail = curve(color=ball2.color)
scene.autoscale = False
while True:
rate(100)
if ball.pos.x +0.5 > wallR.pos.x:
ball.velocity.x = -ball.velocity.x
if ball.pos.x -0.5 < wallL.pos.x:
ball.velocity.x = -ball.velocity.x
if ball.pos.y +0.5 < wallU.pos.y:
ball.velocity.y = -ball.velocity.y
if ball.pos.y -0.5 > wallD.pos.y:
ball.velocity.y = -ball.velocity.y
if ball.pos.z -0.5 < wallB.pos.z:
ball.velocity.z = -ball.velocity.z
if ball.pos.z +0.5 > 6:
ball.velocity.z = -ball.velocity.z
ball.pos = ball.pos + ball.velocity*deltat
varr.pos = ball.pos
varr.axis = vscale*ball.velocity
ball.trail.append(pos=ball.pos)
t = t + deltat
if ball2.pos.x +1 > wallR.pos.x:
ball2.velocity.x = -ball2.velocity.x
if ball2.pos.x -1 < wallL.pos.x:
ball2.velocity.x = -ball2.velocity.x
if ball2.pos.y +1 < wallU.pos.y:
ball2.velocity.y = -ball2.velocity.y
if ball2.pos.y -1 > wallD.pos.y:
ball2.velocity.y = -ball2.velocity.y
if ball2.pos.z -1 < wallB.pos.z:
ball2.velocity.z = -ball2.velocity.z
if ball2.pos.z +1 > 6:
ball2.velocity.z = -ball2.velocity.z
ball2.pos = ball2.pos + ball2.velocity*deltat
varr2.pos = ball2.pos
varr2.axis = vscale2*ball2.velocity
ball2.trail.append(pos=ball2.pos)
t = t + deltat
och i uppgift 2 har jag kommit:
import matplotlib.pyplot as plt
fig = plt.figure(facecolor='white')
plt.title('XY plot')
ax = fig.add_subplot(111)
ax.set_xlabel('x')
ax.set_ylabel('y')
x=[1,2,3,4,5,6,7,8,9,10]
y=[2,3,2,4,3,5,4,6,7,7]
TextWindow = "label 1"
plt.plot(x,y,c='blue',label=TextWindow)
plt.legend(loc='upper right')
plt.show()
Jag är oerhört tacksam för svar!!!