From: Bob felleo on
i have a program that shows floating redballs i need a matlab program that when two balls hit each other, there is a perfectly elastic collision, if you could change it to be the apportion answer i would be very great full

%redball.m
classdef redball < handle
properties
x; % x position
y; % y position
vx; % velocity in x
vy; % velocity in y
r; % radius
end
properties(Constant)
minx = -10;
maxx = 10;
miny = -10;
maxy = 10;
end

methods
function obj = redball( )
obj.x = 18*rand(1) - 9;
obj.y = 18*rand(1) - 9;
obj.vx = .9*rand(1)-.45;
obj.vy = .9*rand(1)-.45;
obj.r = rand(1)*.9 + .1;
end
function [xpts ypts] = getpts(obj)
t = 0:0.1:(2*pi);
xpts = obj.r*cos(t) + obj.x;
ypts = obj.r*sin(t) + obj.y;
end
function obj = move(obj)
obj.x = obj.x + obj.vx;
if((obj.x-obj.r)<=obj.minx | (obj.x+obj.r)>=obj.maxx);
obj.vx = -1*obj.vx;
if(obj.x<0)
obj.x = -9.99+obj.r;
else
obj.x = 9.99-obj.r;
end
end
obj.y = obj.y + obj.vy;
if((obj.y-obj.r)<=obj.miny | (obj.y + obj.r)>=obj.maxy);
obj.vy = -1*obj.vy;
if(obj.y<0)
obj.y = -9.99+obj.r;
else
obj.y = 9.99-obj.r;
end
end
end
end
end

%redball2.m
clear
clear
clc
for j = 1:40
v(j) = redball;
end

while(1)
hold on;
for j = 1:40
v(j).move;
[xp yp] = v(j).getpts;
plot(xp,yp,'ro');
axis([-10 10 -10 10])
end
pause(.01)
hold off;
plot([0],[0]);
end