Question
Matlab repetition problem, please help! Thanks!
The Project: You are asked to use an iterative method to solve the following problem: You are manning an anti-aircraft gun with a muzzle velocity, Vm. An enemy aircraft is approaching on a horizontal path at a range R, an altitude H, and a speed of Vt. What should be the elevation, (angle above the horizontal) of your gun to hit the target? The problem is illustrated in the figure below. Since the projectile is accelerating downward under the influence of gravity, it follows a parabolic path. The aircraft is assumed to be following a straight-line, horizontal path at constant speed. The trajectories of the projectile and the target will intersect at some point (x,y), provided that the launch angle is high enough. For a successful intercept, the projectile and target must reach the intercept point simultaneously.
Explanation / Answer
Following is a MATLAB script that solves the problem for full credits. You can copy it and paste into MATLAB editor. Then you need to save. After saving you can run the script. If you have any problem please comment on the solution. ====================================== %clear command window clc; %clear workspace clear all; %close all figures close all; %Targets T = (1:5); %Range R = [10000; 8000; 11000; 14000; 12500]; %Altitudes H = [3000; 4000; 2500; 3500; 1750]; %Target Velocities V = [250; 325; 225; 350; 275]; %Muzzle Velocity Vm = 1000; %Accelearation due to gravity g = 9.8; %Tolerance of iterative scheme (in degree) tol = 0.01; %Tolerance of iterative scheme (in radian) rtol = pi*tol/180; %Calculated theta values container theta = zeros(size(T')); %Colors of the plots colors = 'kbgmr'; %Legends container of the plots legends = cell(5, 1); %Create figure and hold on so that all plots are in same figure hold on; %Loop over the set of targets for k = T Rt = R(k); %Target Range Ht = H(k); %Target Altitude Vt = V(k); %Target Velocity C = Rt/(Vm+Vt); %Constant for calculation %initial value of thetat (theta for target) thetat = asin((Ht + 0.5*g*C^2)/(Vm*C)); %Steps of iterative scheme steps = 0; while(true) %append next step steps = [steps; steps(end)+1]; %#ok %Constant for calculation C = Rt/(Vm*cos(thetat(end))+Vt); %append next theta for target thetat = [thetat; asin((Ht + 0.5*g*C^2)/(Vm*C))]; %#ok %Check theta for convergence if(abs(thetat(end)-thetat(end-1))