Guest User

Untitled

a guest
Apr 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.41 KB | None | 0 0
  1. % This is a sample code for the CS3414 project I step 3.
  2.  
  3. %   Students should fill in the launch function to generate the two angles.
  4. %   Here we require that the missile should be launched at t_limit = 0.5
  5. %   second. In the radar system, only information before t_limit is given.
  6. %   Your code should give angles for launching the missile.
  7. %  
  8. %   For more detailed description, please refer to the project assignment
  9. %   document.
  10.  
  11. function [flag, angle1, angle2, t_interception] = project4_sample(plotflag)
  12.  
  13. g = [0; -9.8; 0]; %The gravity is -9.8 in the y direction
  14. t_launch = 0.6; %the time time when your missile fires
  15.  
  16. radarpoints = radar();
  17. v0_missile = 4000;
  18. a0_missile = 2000;
  19.  
  20. tic;
  21. %for i = 1:100
  22.     [successflag, angle1, angle2] = launchmissile(v0_missile, a0_missile, radarpoints);
  23. %end
  24. toc;
  25.  
  26. if successflag == 0
  27.     fprintf('Control system decided not to launch the missile since it cannot intercept!\n');
  28. else
  29.    
  30.     t = t_launch; % Initiate the launch time
  31.     w = [cos(angle1)*cos(angle2) ; sin(angle1); cos(angle1)*sin(angle2)]; %set the launch angle
  32.     v0 = v0_missile*w; % set the initial speed of the missile
  33.     a0 = a0_missile*w; % set the acceleration of the missile
  34.    
  35.     x_missile = [0; 0; 0]; % set initial location of missile
  36.    
  37.     x = plane_position(t_launch); % set attacking missile's location when your missile starts
  38.    
  39.     h = 1e-5; % set time increasement. This value may causes error in the judgement. So we set it to be small.
  40.     if (plotflag == 1)
  41.       figure;
  42.       axis([0 10000 0 10000 -1000 1000]);
  43.       plotcount = 1;
  44.       plotdata = zeros(4, 100);
  45.       plotdata(1:3, 1) = x;
  46.       plotdata(4:6, 1) = x_missile;
  47.    
  48.       plot3(plotdata(1, plotcount), plotdata(2, plotcount), plotdata(3, plotcount), 'r');
  49.       hold on
  50.       plot3(plotdata(4, plotcount), plotdata(5, plotcount), plotdata(6, plotcount), 'b');
  51.       M(1) = getframe;
  52.     end
  53.    
  54.     while x(1) > 0 & x(2) > 0 & (norm(x-x_missile) > 1 )
  55.         t = t + h;
  56.         launchtime = t - t_launch;
  57.         x_missile = v0 *launchtime + 0.5*launchtime^2*(a0 + g); % missile's location
  58.         x =  plane_position(t);  % attacking missile's location
  59.    
  60.         if (plotflag ==1) && (t > plotcount*0.1)
  61.             plotcount = plotcount + 1;
  62.             plotdata(1:3, plotcount) = x;
  63.             plotdata(4:6, plotcount) = x_missile;
  64.             plot3(plotdata(1, 1:plotcount), plotdata(2, 1:plotcount), plotdata(3, 1:plotcount), 'r-');
  65.             hold on;
  66.             plot3(plotdata(4, 1:plotcount), plotdata(5, 1:plotcount), plotdata(6, 1:plotcount), 'b-');
  67.             hold on;
  68.             plot3(plotdata(1, plotcount), plotdata(2, plotcount), plotdata(3, plotcount), 'r*');
  69.             hold on;
  70.             plot3(plotdata(4, plotcount), plotdata(5, plotcount), plotdata(6, plotcount), 'b*');
  71.             M(plotcount) = getframe;
  72.         end
  73.     end
  74.     if (plotflag == 1)
  75.         plotcount = plotcount + 1;
  76.             plotdata(1:3, plotcount) = x;
  77.             plotdata(4:6, plotcount) = x_missile;
  78.             plot3(plotdata(1, 1:plotcount), plotdata(2, 1:plotcount), plotdata(3, 1:plotcount), 'r-');
  79.             hold on;
  80.             plot3(plotdata(4, 1:plotcount), plotdata(5, 1:plotcount), plotdata(6, 1:plotcount), 'b-');
  81.             hold on;
  82.             plot3(plotdata(1, plotcount), plotdata(2, plotcount), plotdata(3, plotcount), 'r*');
  83.             hold on;
  84.             plot3(plotdata(4, plotcount), plotdata(5, plotcount), plotdata(6, plotcount), 'b*');
  85.             M(plotcount) = getframe;
  86.     end
  87.     if x(1) > 0 & x(2) > 0
  88.         flag = 1;
  89.         t_interception = t;
  90.         fprintf('Intercepted! at time %f\n', t);
  91.     else
  92.         flag = 0;
  93.         t_interception = NaN;
  94.         fprintf('Missile was launched but did not intercept. \n');
  95.     end
  96.    
  97. end
  98. end
  99.  
  100. function x = plane_position(t)
  101.  
  102. c = [10000 -500 -100 -20 -10 -1; 10000 50 -10 2 -2 -1; 500 10 -1 0 0 0];
  103. x = c(:, 6);
  104. for i = 5:-1:1
  105.   x = x*t + c(:, i);
  106. end
  107.  
  108. end
  109.  
  110. function [x] = radar()
  111.  
  112. t = 0:0.1:0.5;
  113. x = zeros(6, 3);
  114.  
  115. for i = 1:6
  116.   x(i, :) = plane_position(t(i));
  117. end
  118.  
  119. end
  120.  
  121.  
  122. function [location velocity] = missile_estimation(t, radarpoints)
  123.  
  124. t_array = 0:0.1:0.5;
  125. t_array = t_array';
  126. x = radarpoints;
  127.  
  128. for i=2:1:6
  129.     for j=6:-1:i
  130.         x(j,:) = (x(j,:)-x(j-1,:))./(t_array(i+j-1)-t_array(i));
  131.     end
  132. end
  133.  
  134. location = x(6,:);
  135. velocity = x(6,:);
  136. for k=5:-1:1
  137.     location(1,:) = x(k,:) + location(1,:).*(t-t_array(k));
  138.     velocity(1,:) = velocity(1,:).*(t-t_array(k-1))+location(1,:);
  139. end
  140. end
  141.  
  142. function [x] = partialPivot(A, b)
  143. b=b.*-1;
  144. n=3;
  145. for k = 1:n-1
  146.     [col_max index] = max(abs(A(k:n,k)));
  147.     index = index + k-1;
  148.     if index ~= k
  149.         tempA = A(k,k:n);
  150.         A(k,k:n) = A(index,k:n);
  151.         A(index,k:n) = tempA;
  152.         tempb = b(k);
  153.         b(k) = b(index);
  154.         b(index) = tempb;
  155.     end
  156.     A(k+1:n,k) = A(k+1:n,k)/A(k,k);
  157.     for i = k+1:n
  158.         A(i,k+1:n) = A(i,k+1:n) - A(i,k)*A(k,k+1:n);
  159.     end
  160.     b(k+1:n) = b(k+1:n) - A(k+1:n,k)*b(k);
  161. end
  162.  
  163. x = zeros(n,1);
  164. x(n) = b(n)/A(n,n);
  165. for i = n-1:-1:1
  166.     x(i)=(b(i)-A(i,i+1:n)*x(i+1:n))/A(i,i);
  167. end
  168.  
  169. end
  170.  
  171. function [successflag, angle1, angle2] = launchmissile(v0_missile, a0_missile, radarpoints)
  172.  
  173. successflag = 1;
  174.  
  175. g = [0; -9.8; 0];
  176. w = pi/4;
  177. v = 0;
  178. t_launch = 0.6;
  179.  
  180.  
  181. angle1 = 0.95085;
  182. angle2 = 0.07355;
  183.  
  184. end
Add Comment
Please, Sign In to add comment