1. Engineering
2. Computer Science
3. can you please program in matlab m6 clear clc m240120360all...

# Question: can you please program in matlab m6 clear clc m240120360all...

###### Question details

Can you please program in MatLab

#M6---------

clear

clc

M=[240,120,360];%all th masses

Vi=[30,15,-45]%all the vel0cities before they collieded

%----------------------------------------------------------------------
%i will be making 2 carts colide by trail an error
%I will be checking if momentum and kinetic energy is perserved
%this will prove that the bodies collided
%-----------Collison a bettween cart 2 and cart3 ------------------
Va=Vi;% Va values of vi that preserve the unchanged values

Va(2)=FinalVelocity(M(2),M(3),Vi(2),Vi(3));

Va(3)=FinalVelocity(M(3),M(2),Vi(3),Vi(2))

[CheckPa, CheckKEa] = check_p_KE_total(M,Va,M,Vi)
%-----------Collison b bettween cart 1 and cart2 ---------------------
Vb=Va;% Vb values of va that preserve the unchanged values

Vb(1)=FinalVelocity(M(1),M(2),Va(1),Va(2));

Vb(2)=FinalVelocity(M(2),M(1),Va(2),Va(1))

[checkPb,CheckKEb]=check_p_KE_total(M,Vb,M,Vi)
%----------Collison c bettween cart 2 and 3 ------------------------------
Vc=Vb; % Va values of vb that preserve the unchanged values

Vc(2)=FinalVelocity(M(2),M(3),Vb(2),Vb(3));

Vc(3)=FinalVelocity(M(3),M(2),Vb(3),Vb(2))

[checkPc,CheckKEc]=check_p_KE_total(M,Vc,M,Vi)
%----------Collison d bettween cart 1and2 -----------------------------
Vd=Vc; % Vd values of vc that preserve the unchanged values

Vd(1)=FinalVelocity(M(1),M(2),Vc(1),Vc(2));

Vd(2)=FinalVelocity(M(2),M(1),Vc(2),Vc(1))

[checkPd,CheckKEd]=check_p_KE_total(M,Vd,M,Vi)

%-------------expelantation----------------------------------------------
%Ans-there is 4 collisions becuase the final velocities of the carts fourth
%collison makes it impossible for another collstion.cart 1 and 3 are going
%in a oppisite direction and not faceing each other. cart1 and2 are going
%in the same direction but cart1 is ahead of cart2and going faster.
%cartcart 2 and 3 are moving in opposit drirctions and not faceing each
%other.
%Ans-- the change of momentum and Kinetic energey is 0

function Vf =FinalVelocity(M1,M2,V1,V2);
DelM=M1-M2;
SumM=M1+M2;
Vf=(DelM/SumM)*V1+(2*M2/SumM)*V2;
end

function [pT,KET]=p_KE_total(m,v)
pT = sum(m.*v); %total mom
KET=sum(1/2*m.*v.^2) %total K energy
end

function[checkp, checkKE] = check_p_KE_total(m,v,m0,v0)
[pT, KET] = p_KE_total(m, v);
[pT0, KET0] = p_KE_total(m0,v0);
checkp = pT-pT0;
checkKE = KET - KET0;
end