Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Description Write a MATLAB function, a .m file, that will simulate a ball bouncing in a square box. The material has the perfect elasticity,

Problem Description

Write a MATLAB function, a .m file, that will simulate a ball bouncing in a square box. The material has the perfect elasticity, no energy is lost for each impact. We will also assume that there is no air resistance so the velocity will stay a constant.

The box has a dimension of 20x20 in the default unit. The origin of the coordinate system is at the center of the box with x being the horizontal axis and y being the vertical axis.Initially, the ball will be placed at the origin.

Your program will ask the user to input the magnitude of the velocity and the angle between the velocity vector and the x-axis. It will then calculate and plot the position of the ball in a graph window. The title of the graph will be your name Homework 2, see the image below.The red dot in the graph window indicates the current position of the ball.

When the ball bounces off one of the walls, the magnitude of the velocity stays the same. However, one of the velocity componentswill change sign. (Unless the ball is hitting the corner.In that case, both velocity components will reverse the sign.) For example, the diagram on the left shows the ball collides with and bounces off the right wall, x=10. In this case:

Vx-after= Vx-before

Vy-after= Vy-before

You can let the program run indefinitely until the user enter cntrl-C or terminate after a sufficient period of time.

Motion of the ball

Initial position: x0=0, y0=0.

Constant Velocity: Vx= Vcos and Vy= Vsin where V and are user inputs

.Position as function of time:

x(t) = x0+ Vxt

y(t) = y0+ Vyt

In an iterative calculation:

x(t+t) = x(t) + Vxt

y(t+t) = y(t) + Vyt

Collision Detection

Since the box is bounded by x=10, y=10, x=-10, and y=-10, a ball will be colliding with one of the walls when x(t)10, x(t)-10, y(t)10, or y(t)-10. When a collision occurs, the ball will be bouncing off the wall.(Make sure the red dot will not go outside the box.)

Pausing the Program

The t in the iterative loop is used to represent the time difference in the real world. For example, if t=0.1, assuming second as our unit, then we are calculating a physical position for every 0.1 second is the physical world. However, this calculation will be done in much less than 0.1 second of the physical time in the computer. We wont even see the point being drawn and erased on the screen if we dont slow down the calculation process. This can be done by injecting a pause(duration)statement at the end of the loop.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions