Lab 2

Python Lab 2

 

1. Collect/Questions on Form Letter

2. Assignment Read examples

a=7
b=3
c=a*b-6/2
b=a%b+5
c=c+2*a
a=b/2+1
c=c-b+a
print a,b,c

Challenge

3, Square Root approximation algorithm

4. Find the slope of a line given 2 points

4. How do you make change? English

5. Change Program Assignment

-enter purchase price

-figure tax (8.5%)

-enter amount of money received from purchaser 

- find change

- print out change in number of coins and dollars

- Due Wednesday of Week 3 in MASU.. more on that later

6. Strings: Adding, Parsing

7. Find all permutations of colors for MasterMind

 

VPython Lab 2

1. Graph Program

-Look at code

(x-9)**2/(x+3)(x-7) ??? Fix?

2. Ramp Program

3. Projectile Program

4. Projectile Program Assignment

- enter elevation in degrees.

- Make a 'tube'

- enter magnitude of force

- Wind?

- make ball hit target? Basketball?

* Status: Next Wednesday

 

 

Ramp Program

from visual import *
from visual.graph import *
print "view both output screens, then click on ramp screen to stop. Click again to toggle on/off"
anginp = input("enter angle") #angle of inclination for ramp in degrees
grav = input("enter gravity constant)") #not used at present
scene.autoscale=0 #turn off automatic scaling of graphics

graph = gdisplay(ymin=0 ,xmax=.2,ymax =.1,xmin=0) #set the scale of the graph
curve = gcurve(color=color.yellow) #set the color of the curve line on graph
curve2 = gcurve(color=color.red)
ang = math.pi/180*anginp #turn input from degrees into radians
comp=math.pi/180*(90-anginp) #find and store the compliment of the input angle
ballx=-1.25*cos(comp) #find the initial ball x position offseting for ramp size
bally=1.25*sin(comp) #find the init ball y position
h=1/sin(ang) # h is the hypotenuse unit distance of the ramp
xaxis=h*cos(ang) #find the xaxis of the ramp
floor = box(pos=(0,0,0), axis = (h*cos(ang),1,0),length=18, height=0.5, width=2, color=color.blue)
ballx=ballxinit=ballx+cos(ang)*8.5 #move the x pos of the ball to end of ramp
bally=ballyinit=bally+sin(ang)*8.5 # 8.5 is due to ramp size of 18.. half
ball = sphere(pos=(ballx,bally,0), color=color.red) #make sphere
gvec = arrow(pos = (ballx,bally,0),axis = (0,-grav,0),color=color.green,shaftwidth=.3)
ball.velocity = vector(-sin(comp),-cos(comp),0) #set the vector of force along ramp
dt = 0.01
oldx = ball.pos.x #store the x and y position of the ball. Use to update
oldy = ball.pos.y #the change of the ball position of each dt
oldmag = ball.velocity.mag

scene.mouse.getclick() #stop the program until the user clicks on the screen
while ball.pos.x>-8: #the end of the ramp is at -8 units.. stop when you get there
rate(30) #speed of graphic display
if scene.mouse.clicked: #check to see if the user wants to stop the ball by clicking
scene.mouse.getclick() #clear our the value of the mouse click
scene.mouse.getclick() #wait until the user clicks again to continue


ball.pos = ball.pos + ball.velocity*dt # update the position of the ball along the axis of force
ball.velocity=ball.velocity+ball.velocity*dt #update the velocity value by gravity force
gvec.pos = ball.pos #show arrow along the force of gravity

curve.plot(pos =(oldx-ball.pos.x,oldy-ball.pos.y)) #plot the difference in position over dt, x and y

curve2.plot(pos = ((ballxinit*2-(ball.pos.x+ballxinit))*.01,(ball.velocity.mag/oldmag)*.01))
print ball.velocity.mag, oldmag, ball.velocity.mag/oldmag
oldx = ball.pos.x #update old position of the ball
oldy = ball.pos.y
oldmag = ball.velocity.mag

Graph Program

from visual.graph import *
from visual.factorial import factorial

xmax = input("enter domain range")
xmin = -xmax

myfunction = raw_input("enter function ")
myderivative = raw_input("enter the derivative")

#define a function to approximate and enter its derivative
def f(x): return eval(myfunction)
def g(x): return eval(myderivative)

dx = (xmax-xmin)/512.0 # chop domain into 512 deltas
graph = gdisplay(ymin=xmin, ymax=xmax,xtitle=myfunction)

curve = gcurve(color=color.yellow)
curve2 = gcurve(color=color.red)

#draw the function itself
for x in arange(xmin,xmax,dx): curve.plot(pos=(x,f(x)))
for x in arange(xmin,xmax,dx): curve2.plot(pos=(x,g(x)))

Projectile Program


from visual import *
ball = sphere(pos=(-6,.1,0), color=color.blue,radius=.4)
ball.velocity = vector(6,10,0)
wind=vector(-1,0,0)
d=.01
while ball.pos.y>=0:
rate(50)
print ball.pos.x,ball.pos.y,ball.pos.z
ball.velocity.y = ball.velocity.y-9.8*d
ball.velocity = ball.velocity+wind*d
ball.pos = ball.pos + ball.velocity*d