%Determines the angle and minimum force necessary to hit a target %m = projectile mass %g = force of gravity %R = wind resistance force magnitude %target_coords = target's (x,y,...) coordinates %t = the time matrix controlling the launched_ball model's running time %TOL = the maximum tolerance for the bisection method %theta_min, theta_max = boundary of angles to search for solutions %force_range = [force min, force max] matrix containing the range of forces %within which to search for the solution %Returns: Optimal angle and force function [force, angle] = launched_ball_opt(m, g, R, target_coords, t, TOL, theta_min, theta_max, force_range, interm_plots) %extract the coordinates target_x = target_coords(1); target_y = target_coords(2); %TODO - optimize below. Could probably make this run much faster force = fminbnd(@(Fi) launched_ball_force(m, g, R, target_x, target_y, Fi, t, TOL, theta_min, theta_max, interm_plots),force_range(1) , force_range(2)); theta_opt = launched_ball_opt_angle(m, g, R, target_x, target_y, force, t, TOL, theta_min, theta_max, false); angle = theta_opt; if (isnan(theta_opt)) return end [err, soln_x, soln_y] = sim_launched_ball(m, g, R, target_x, target_y, force, theta_opt, t); %plot solution if ~(interm_plots) hold off end hold on; plot(soln_x, soln_y, 'LineWidth', 4); plot(target_x,target_y, '*r'); axis([0 target_x+10 target_y-50 max(soln_y)+10]); end %Determines the angle needed to hit a target, but returns the corresponding %force needed function [Fi] = launched_ball_force(m, g, R, target_x, target_y, Fi_init, t, TOL, theta_min, theta_max, interm_plots) theta_opt = launched_ball_opt_angle(m, g, R, target_x, target_y, Fi_init, t, TOL, theta_min, theta_max, false); %make itermediate plots if ~(interm_plots) hold off end if ~(isnan(theta_opt)) Fi = Fi_init; return else Fi = Inf; return end end