Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. clear;
  2.  
  3. % start with a constant kd, use the given default
  4. kd=15:24;
  5. kp=1:10;
  6. % loop through kp
  7. for i=kp-0
  8. for j=kd-14
  9. pd_tanker(kp(i),kd(j),0);
  10. load(['holy_ship_' num2str(kp(i)) '_' num2str(kd(j))]);
  11. perf(i,j) = calc_perf_data(time, psi, desired_heading);
  12. end
  13. end
  14.  
  15. % generate Y from performance data
  16. Y=perf;
  17.  
  18. % this happens to be 10x10 for this example. We'd have to consider
  19. % a different case if we want to expand this
  20. order=length(Y);
  21. % X is the order, conveniently.
  22.  
  23. X = (1:order)';
  24.  
  25. % Now loop through the order, which is also the same as kp values
  26. mse=zeros(1,order);
  27. for i = 1:length(Y) % loop through kd values
  28. Phi = ones(length(X),1);
  29. for j = 1:order % order and kd
  30. Phi = [X.^j Phi];
  31. Theta = Phi\Y(:,i);
  32. Yls = Phi*Theta;
  33. e = Y(:,i)-Yls;
  34. mse(i,j) = (e'*e)/2;
  35. end
  36. end
  37. % Someone smarter than me could probably do this in the upper loop
  38. % But the data is messing with my brain, so i'm taking it externally
  39. % and then getting the min values and then the kd/kp by order.
  40.  
  41. for i=1:length(mse)
  42. % it's square. We'll get the min row. That's KD
  43. % We'll get the min column. That's KP.
  44. % i is the order.
  45. [min_value, index] = min(abs(mse(:,i)));
  46. best_kp(i)=kp(index);
  47. [min_value, index] = min(abs(mse(i,:)));
  48. best_kd(i)=kd(index);
  49. end
  50.  
  51. % plot MSE/order
  52. figure
  53. plot3(mse(1,:), mse(:,1),1:10)
  54. hold on
  55. for i=kd-14
  56. for j=kp-0
  57. plot3(mse(i,:), mse(:,j),1:10)
  58. end
  59. end
  60.  
  61. zlabel('Order');
  62. ylabel('Kd');
  63. xlabel('Kp');
  64.  
  65.  
  66. % plot min
  67. plot(1:order,best_kp,1:order,best_kd)
  68. xlabel('Order')
  69. ylabel('Value');
  70. legend('Best kp', 'Best kd');
  71. title('Order vs best kp; best kd');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement