%Simulate Launched Ball Method %This method runs the simulink model launched_ball and determines the %horizontal distance between the target & the launched projectile. % %m = mass of the projectile/ball %g = gravity %R = resistance force %Fi = input force magnitude (force is always an impulse) %theta = angle at which to launch the ball %t_final = the length of time to run the simulation function [error,x,y] = sim_launched_ball(m, g, R, target_x, target_y, Fi, theta, t) sim('launched_ball', t, simset('SrcWorkspace', 'current')); x = squeeze(sim_x.signals.values); y = squeeze(sim_y.signals.values); %determine if the projectile hit the target, otherwise return the distance for i = (length(y)):-1:1 if (y(i) > target_y) if (i == length(x)) %prevent indexing value outside x array error = target_x - x(i); else error = target_x - x(i+1); end return end end error = target_x - x(1); %if no value in y is ever greater than the target y, must set a default error value end