MATLAB: Hey i’m trying to simulate a bouncing ball but i wasn’t able to simulate it on viscircles function. i get Dimensions of matrices being concatenated are not consistent. problem everytime

simulating bouncing ball

if true
function main
global chart;
chart = 0;
tspan = [0,15];
initial_conditions = [100 0];
options = odeset('events',@events, 'RelTol', 1e-5, 'MaxStep',1e-2);
hold on;
durum = 0;
while durum ~= 1
[t,z] = ode45(@bouncing,tspan,initial_conditions,options);
if chart == 0
disp('tochdown');
%plot(t,z(:,1),'b');
viscircles([3,z(:,1)],5);
else
disp('liftup');
%plot(t,z(:,1),'r');
viscircles([3,z(:,1)],5);
end
if t(end) == tspan(2) durum =1 ; else tspan(1) = t(end);
end chart = 1 – chart; initial_conditions = z(end,:); end
end
function dz = bouncing(t,z) global chart; dz = zeros(2,1); g = 9.81; k = 40; c = 1; m = 1; switch chart case 1 dz(1) = z(2); dz(2) = g-k*z(1)-c*z(2);
case 0
dz(1) = z(2);
dz(2) = -g;
end
end
function [value, isterminal, direction] = events(t,z) global chart; switch chart case 1 value = z(1); isterminal = 1; direction = 1;
case 0
value = z(1);
isterminal = 1;
direction = -1;
end
end
end

Best Answer

z(:,1) is a column vector. The syntax [3,z(:,1)] is trying to create a matrix with 2 columns in which the second is multiple elements but the first column is a single element. The two columns need to be the same size.
If what you wanted was to use 3 for every entry in the first column then you need to make multiple copies of that 3 such as by using repmat(3,size(z,1),1) It would seem to me that using time would be more appropriate than a constant value.