%Determines the optimal angle to fire a projectile given a specified force. % %m = mass of the projectile/ball %g = gravity %R = resistance force %Fi_init = input force magnitude (force is always an impulse) %theta_min, theta_max = interval of angles between which the optimal angle %will be searched for %t = time for simulation. If specified as integer, dictates running time of %simulation. If specified as array, dictates time points to evaluate model %during simulation %TOL = if abs(midp-theta_min) < TOL, stop searching for a root, where midp %is the midpoint b/w theta_min & theta_max %interm_plots = boolean indicating whether to plot intermediate plots % %remark: returns negative answer if no soln found function [theta_opt] = launched_ball_opt_angle(m, g, R, target_x, target_y, Fi_init, t, TOL, theta_min, theta_max, interm_plots) %set values of global vars global soln_x soln_y; %determine the needed theta to hit the target theta_opt = bisection(@(theta) sim_launched_ball_err(m, g, R, target_x, target_y, Fi_init, theta, t, interm_plots), theta_min, theta_max, TOL, 1000); if abs(theta_opt-theta_max) < TOL || abs(theta_opt-theta_min) < TOL %|| abs(x_soln_coord-target_x) > abs(target_x*0.1) %fprintf('\nNo solution found') axis([0 target_x+50 target_y-100 max(soln_y)+50]); theta_opt = NaN; else hold on plot(soln_x, soln_y, 'k','LineWidth', 3); axis([0 target_x+10 0 max(soln_y)+10]); %axis([0 target_x+10 target_y-50 max(soln_y)+10]); end %plot target plot(target_x,target_y, '*r'); end function [err] = sim_launched_ball_err(m, g, R, target_x, target_y, Fi_init, theta, t, interm_plots) global soln_x soln_y; [err,soln_x,soln_y] = sim_launched_ball(m, g, R, target_x, target_y, Fi_init, theta, t); %make itermediate plots if (interm_plots) hold on plot(soln_x, soln_y) end return end