Question

Using Python

a) Add a function to the Projectle class below so it can print out the current status seconds, position, and total velocity (

In []: from math import sin, cos, radians class Projectile: simulates the flight of simple projectiles near the earths surf

0 0
Add a comment Improve this question Transcribed image text
Answer #1

from math import sin, cos, radians

class Projectile:

"""Simulate the flight of a simple projectiles near the earth's surface

ignoring wind's resistance. Tracking is done in two dimensions, height (y) and distance (x)"""

def __init__(self, angle, velocity, height=0):

""" Create a Projectile with given launch angle, initial velocity and height"""

self.xpos= 0.0

self.seconds = 0

self.ypos = height

theta = radians(angle)

self.xvel = velocity * cos(theta)

self.yvel = velocity * sin(theta)

def update(self, time):

"""Update the state of this projectile to move it time seconds farther into the flight"""

self.xpos = self.xpos + self.xvel * time

yvel1 = self.yvel - 9.8 * time

self.ypos = self.ypos + time * (self.yvel + yvel1)/2.0

self.yvel = yvel1

self.seconds += time

# Check if maximum height reached

if(round(self.yvel) == 0 and self.yvel > 0 ):

print(f'After %.2f seconds, maximum height at %.2f m.'%(self.seconds, self.ypos))

def __str__(self):

"""Prints the details about the projectile"""

return f'After %.2f seconds, position is X: %.2f m, Y: %.2f m, velocity is %.2f m/s' %(self.seconds,self.xpos, self.ypos, self.xvel)

p = Projectile(47, 40)

for _ in range(75):

p.update(0.05)

print(p)

Python 3.7.4 (default, Jul [GCC 6.3.0 20170516] on linux After 2.95 seconds, maximum height at 43.66 m After 3.75 seconds, po

Add a comment
Know the answer?
Add Answer to:
Using Python a) Add a function to the Projectle class below so it can print out...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT