Question: Write a Matlab function nextCollision() to determine when a single particle will next collide with a wall as 1 function [t_next, a, b] = nextCollision(r,

Write a Matlab function nextCollision() to determine when a single particle will next collide with a wall as

1 function [t_next, a, b] = nextCollision(r, u, a, b)with the following properties:

The inputs r and u are the position and velocity vectors of the particle.

The inputs a and b indicate which particle (as variable a) was previously involved in a collision with a wall (as variable b). Since youre only dealing with a single particle for this part of the simulation, a=1always. You can label the walls any way you want (in subsequent parts of the project youll use variable bto represent walls or particles, but for now it just represents walls.)

Within the function, calculate how long it will be until the particle collides with each wall assuming the particle continues on its current trajectory. Use the formula

t = distance to a wall/ speed in the direction of that wall

To determine the next time of collision with each wall. For example, to calculate the time at which particle 1 collides with the right wall (which exists at x = 1),

t = 1 r1x / u1x

Youll have to come up with similar equations for the other three walls.

Use the input values a and b to assign a value of Inf to the time calculated for the previous particle-wall collision pair. This will prevent the simulation from getting stuck.

Within the function find the minimum time of all particle-wall collision times calculated with the formula above and return this minimum time as t_next (negative, imaginary, and/or NaN indicate that there is no future collision associated with that particular particle-wall pair).

The outputs a and b indicates which particle (as variable a) will be involved in a collision with a wall (as variable b). The collision between outputs a and b is the one associated with t_next: the very next collision is the one between outputs a and b and it will occur at some time in the future given by t_next. Always return a=1 since theres only one particle, and return a value of b indicating which wall is involved in the next collision.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!