Skip to main content
deleted 12 characters in body
Source Link
VisualMelon
  • 7.6k
  • 23
  • 46

I would like to explain why my code has a better implementation of the problem. one thing to understand is that all possible collisions will be over when the right side block is moving right and the left block is also moving right with a lower velocity. this situation will continue till infinity.

Also, we dont need the position parameter at all as we only need to check velocity change to know a collision. The case when the left block is moving towards the wall has also been considered in the last if statement where i reverse its velocity. I am new in this forum and if any changes are needed, just leave a note and will do the same.

I am a beginner to programming and can code only in MATLAB. I found this problem interesting and hence tried solving it.

I would like to explain why my code has a better implementation of the problem. one thing to understand is that all possible collisions will be over when the right side block is moving right and the left block is also moving right with a lower velocity. this situation will continue till infinity.
also, we dont need the position parameter at all as we only need to check velocity change to know a collision. The case when the left block is moving towards the wall has also been considered in the last if statement where i reverse its velocity. I am new in this forum and if any changes are needed, just leave a note and will do the same.
I am a beginner to programming and can code only in MATLAB. I found this problem interesting and hence tried solving it.

%Written by Shubham Wani, Mechanical Undergrad at NIT Trichy.
%Set the mb(stands for Mass B) variable to powers of 100
%COLLIDING BLOCKS COMPUTE PI
%3BLUE1BROWN @ YOUTUBE
%Takes about 0.043737 seconds for calculations for 100000000 kg Mass B
tic
clearvars;
clear ;
clear clc;
ma = 1;
%set mb to various powers of 100
mb = 10000000000;   
va(1)= double(0);
vb(1)= double(-10);
n=1;
while (true)
    **%check if it is the last possible collision**
    if (vb(n)>0 && va(n)<vb(n))
        break;
    end
    **%Calculate new velocities after the collision**
    va(n+1)=(ma*va(n)+mb*vb(n)+mb*(vb(n)-va(n)))/(ma+mb);
    va(n+1);
    vb(n+1)=(ma*va(n)+mb*vb(n)+ma*(va(n)-vb(n)))/(ma+mb);
    vb(n+1);
    n=n+1;
    **%if mass A is moving towards left, invert its speed.**
    if (va(n)<0)
        va(n+1)=-va(n);
        vb(n+1)=vb(n);
        n=n+1;
    end
end
**%Number of collisions=n-1 as indexing starts from 1**
disp(n-1);
toc
I would like to explain why my code has a better implementation of the problem. one thing to understand is that all possible collisions will be over when the right side block is moving right and the left block is also moving right with a lower velocity. this situation will continue till infinity.
also, we dont need the position parameter at all as we only need to check velocity change to know a collision. The case when the left block is moving towards the wall has also been considered in the last if statement where i reverse its velocity. I am new in this forum and if any changes are needed, just leave a note and will do the same.
I am a beginner to programming and can code only in MATLAB. I found this problem interesting and hence tried solving it.

%Written by Shubham Wani, Mechanical Undergrad at NIT Trichy.
%Set the mb(stands for Mass B) variable to powers of 100
%COLLIDING BLOCKS COMPUTE PI
%3BLUE1BROWN @ YOUTUBE
%Takes about 0.043737 seconds for calculations for 100000000 kg Mass B
tic
clearvars;
clear ;
clear clc;
ma = 1;
%set mb to various powers of 100
mb = 10000000000;   
va(1)= double(0);
vb(1)= double(-10);
n=1;
while (true)
    **%check if it is the last possible collision**
    if (vb(n)>0 && va(n)<vb(n))
        break;
    end
    **%Calculate new velocities after the collision**
    va(n+1)=(ma*va(n)+mb*vb(n)+mb*(vb(n)-va(n)))/(ma+mb);
    va(n+1);
    vb(n+1)=(ma*va(n)+mb*vb(n)+ma*(va(n)-vb(n)))/(ma+mb);
    vb(n+1);
    n=n+1;
    **%if mass A is moving towards left, invert its speed.**
    if (va(n)<0)
        va(n+1)=-va(n);
        vb(n+1)=vb(n);
        n=n+1;
    end
end
**%Number of collisions=n-1 as indexing starts from 1**
disp(n-1);
toc

I would like to explain why my code has a better implementation of the problem. one thing to understand is that all possible collisions will be over when the right side block is moving right and the left block is also moving right with a lower velocity. this situation will continue till infinity.

Also, we dont need the position parameter at all as we only need to check velocity change to know a collision. The case when the left block is moving towards the wall has also been considered in the last if statement where i reverse its velocity. I am new in this forum and if any changes are needed, just leave a note and will do the same.

I am a beginner to programming and can code only in MATLAB. I found this problem interesting and hence tried solving it.

%Written by Shubham Wani, Mechanical Undergrad at NIT Trichy.
%Set the mb(stands for Mass B) variable to powers of 100
%COLLIDING BLOCKS COMPUTE PI
%3BLUE1BROWN @ YOUTUBE
%Takes about 0.043737 seconds for calculations for 100000000 kg Mass B
tic
clearvars;
clear ;
clear clc;
ma = 1;
%set mb to various powers of 100
mb = 10000000000;   
va(1)= double(0);
vb(1)= double(-10);
n=1;
while (true)
    **%check if it is the last possible collision**
    if (vb(n)>0 && va(n)<vb(n))
        break;
    end
    **%Calculate new velocities after the collision**
    va(n+1)=(ma*va(n)+mb*vb(n)+mb*(vb(n)-va(n)))/(ma+mb);
    va(n+1);
    vb(n+1)=(ma*va(n)+mb*vb(n)+ma*(va(n)-vb(n)))/(ma+mb);
    vb(n+1);
    n=n+1;
    **%if mass A is moving towards left, invert its speed.**
    if (va(n)<0)
        va(n+1)=-va(n);
        vb(n+1)=vb(n);
        n=n+1;
    end
end
**%Number of collisions=n-1 as indexing starts from 1**
disp(n-1);
toc
Source Link

I would like to explain why my code has a better implementation of the problem. one thing to understand is that all possible collisions will be over when the right side block is moving right and the left block is also moving right with a lower velocity. this situation will continue till infinity.
also, we dont need the position parameter at all as we only need to check velocity change to know a collision. The case when the left block is moving towards the wall has also been considered in the last if statement where i reverse its velocity. I am new in this forum and if any changes are needed, just leave a note and will do the same.
I am a beginner to programming and can code only in MATLAB. I found this problem interesting and hence tried solving it.

%Written by Shubham Wani, Mechanical Undergrad at NIT Trichy.
%Set the mb(stands for Mass B) variable to powers of 100
%COLLIDING BLOCKS COMPUTE PI
%3BLUE1BROWN @ YOUTUBE
%Takes about 0.043737 seconds for calculations for 100000000 kg Mass B
tic
clearvars;
clear ;
clear clc;
ma = 1;
%set mb to various powers of 100
mb = 10000000000;   
va(1)= double(0);
vb(1)= double(-10);
n=1;
while (true)
    **%check if it is the last possible collision**
    if (vb(n)>0 && va(n)<vb(n))
        break;
    end
    **%Calculate new velocities after the collision**
    va(n+1)=(ma*va(n)+mb*vb(n)+mb*(vb(n)-va(n)))/(ma+mb);
    va(n+1);
    vb(n+1)=(ma*va(n)+mb*vb(n)+ma*(va(n)-vb(n)))/(ma+mb);
    vb(n+1);
    n=n+1;
    **%if mass A is moving towards left, invert its speed.**
    if (va(n)<0)
        va(n+1)=-va(n);
        vb(n+1)=vb(n);
        n=n+1;
    end
end
**%Number of collisions=n-1 as indexing starts from 1**
disp(n-1);
toc

the comments are self explanatory.